2011-11-03 3 views
3

나는 며칠 동안 내 머리카락을 찢어 내고 있는데, 지금은 왜 이런 일이 결코 일어나지 않는지를 알아 내려고 노력하고 있습니다! 첫째, 여기 내 구성입니다 :자바 애플릿에서 직렬 포트에 데이터를 쓰려고하는 중입니까?

윈도우 7 64
JDK 7 86
JRE 씬에서 제공하는 7 개 86
파이어 폭스 86
레일 3
자바 설정은있는 그 "차세대 플러그인"ISN 어떤 식 으로든 다시 활성화됩니다 !!!)

처음에는 RXTX를 시도했지만 "클래스를 찾을 수 없음"오류가 계속 발생했습니다. 나는 이제 winjcom으로 옮겼다. 내가 지금 받고있는 오류는 java.security.AccessControlException: access denied ("java.lang.RuntimePermission" "loadLibrary.winjcom")입니다.

나는 또한 내 서버 로그가 내가 사용하는 브라우저에 따라 다르다는 것을 알게되었습니다. Firefox를 사용하면 브라우저가 애플릿을로드하려고 시도 할 때 (즉, 애플릿로드가 실패 할 때 더 이상 다운로드되지 않는) GET 로그가 표시되지 않습니다. IE9에서 시도하면 "PortWriter.class"GET 로그를 제외하고 모든 로그가 존재합니다 ... 이는 어떤 이유로 검색되지 않는다는 것을 의미합니다.

JNLP 사용을 피할 때 보안 경고 팝업이 표시되고 "전송"메서드를 실행할 때 보안 액세스 오류를 제외하고 오류가 없습니다. 그러나 내가 JNLP를 사용할 때, IE는 벌금을로드하고 여전히 오류를 준다. 그러나 닫을 때 충돌이 난다. (나는 iexplorer 프로세스를 끝내야한다.) Firefox는 페이지를로드하지 않고 진행 막대에서 멈 춥니 다.

업데이트 - 제가 보안 ​​정책 파일을 통해 보안을 우회하면 애플릿이 작동합니다. 그러나 JNLP는 작동하지 않을 것입니다. 애플릿 태그를 실행할 때 일반적으로 보안 오류가 발생하는 이유라고 생각합니다. JNLP 파일에 직접 액세스 할 때 "PortWriter"클래스를 찾을 수 없다는 오류가 발생합니다. 내 항아리에 문제가 있습니까? 필자는 다른 항아리의 폴더 레이아웃이 디렉토리 구조가 패키지 레이아웃 (예 : 패키지가 com.myname.serialport.PortWriter 인 경우 "com \ myname \ serialport \ PortWriter.jar")과 정확히 일치하는 것으로 나타났습니다. . 그러나 MY JAR 레이아웃은 실제 폴더 레이아웃 (예 : "D : \ Files \ Websites \ pos \ assets \ java \ PortWriter.jar")을 복제합니다. 이것이 오류의 원인입니까? 항아리 내용 (매니페스트 파일 포함)을 루트와 일치하도록 수동으로 변경했지만 어쩌면 내가 잘못한 것일 수도 있습니다. JaNeLa가 확인한 최신 변경 사항을 설명하기 위해이 문제의 JNLP 레이아웃을 업데이트했습니다. I을 어떻게

<applet id="SerialPort" width="1" height="1" codebase="/assets/" code="PortWriter.class" archive="PortWriter.jar"> 
     <param name="jnlp_href" value="PortWriter.jnlp" /> 
    </applet> 

:

import com.engidea.comm.CommPort; 
import com.engidea.comm.CommPortIdentifier; 
import com.engidea.comm.SerialPort; 
import com.engidea.comm.SerialPortEvent; 
import com.engidea.comm.SerialPortEventListener; 
import com.engidea.win32jcom.WinjcomIdentifier; 
import java.io.*; 
import java.util.Iterator; 
import java.util.List; 
import java.applet.*; 
import java.security.*; 

/* 
    WinJcom is a native interface to serial ports in java. 
    Copyright 2007 by Damiano Bolla, [email protected] 

    This library is free software; you can redistribute it and/or 
    modify it under the terms of the GNU Library General Public 
    License as published by the Free Software Foundation; either 
    version 2 of the License, or (at your option) any later version. 
    This can be used with commercial products and you are not obliged 
    to share your work with anybody. 

    This library is distributed in the hope that it will be useful, 
    but WITHOUT ANY WARRANTY; without even the implied warranty of 
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 
    Library General Public License for more details. 

    You should have received a copy of the GNU Library General Public 
    License along with this library; if not, write to the Free 
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
*/ 

/** 
* Simple class that can list system ports and allow IO 
*/ 
public class PortWriter extends Applet 
    { 

    private CommPortIdentifier portIdentifier; 
    private SerialPort serport; 

    public void init() {System.out.println("Booting...");} 
    @SuppressWarnings("unchecked") 
    public void Sends(String port, String message) throws Exception 
    { 

    final String com_port = port; 
    final String send_message = message; 

    AccessController.doPrivileged(new PrivilegedAction() { 
    public Object run() { 

     System.out.println("Begin..."); 
     portIdentifier = new WinjcomIdentifier(0); 
     System.out.println("Selecting Port..."); 
     selectComport(com_port); 
     new Thread(new PortReader()).start(); 
     System.out.println("Sending..."); 
     typeSendBytes(send_message); 

    return true;} 
    }); 
    } 

    private void typeSendBytes(String message) 
    { 
    try 
     { 
     System.out.println("Trying To Send..."); 
     BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); 
     String aStr = ""; 
     while (aStr != null) 
     { 
     aStr = message + "\r\n"; 
     // WARNING: be careful, you shoulod select the encoding ! 
     // This will timeout if you have FLOW CONTROL and theline is stuck ! 
     byte []buf = aStr.getBytes(); 
     serport.write(buf,0,buf.length); 
     } 
     } 
    catch (Exception exc) 
     { 
     exc.printStackTrace(); 
     } 
    } 

    private SerialPort openPort (String portName) 
    { 
    try 
     { 
     CommPort aPort = portIdentifier.getCommPort(portName); 
     aPort.open(); 
     return (SerialPort)aPort; 
     } 
    catch (Exception exc) 
     { 
     System.out.println("exc="+exc); 
     exc.printStackTrace(); 
     } 

    return null; 
    } 

    private void selectComport (String portName) 
    { 
    try 
     { 
     serport = openPort(portName); 
     serport.setSerialPortParams(9600,8, SerialPort.STOPBITS_2, SerialPort.PARITY_NONE); 
     serport.enableReceiveTimeout(20000); 
     serport.setEventListener(new EventListener()); 

     serport.notifyOnDSR(true); 
     serport.notifyOnCarrierDetect(true); 
     serport.notifyOnCTS(true); 
     } 
    catch (IOException exc) 
     { 
     System.out.println("Exc="+exc); 
     exc.printStackTrace(); 
     } 
    } 


private final class EventListener implements SerialPortEventListener 
    { 
    public void serialEvent(SerialPortEvent ev) 
    { 
    System.out.println("Got event="+ev); 
    } 
    } 


private final class PortReader implements Runnable 
    { 
    public void run() 
    { 
    try 
     { 
     // This will timeout if nothing is received in the specified time. 
     byte []buff = new byte[1]; 
     while ( serport.read(buff,0,buff.length) > 0) 
     { 
     // NOTE: you should be checking the encoding ! 
     System.out.print(new String(buff)); 
     } 
     } 
    catch (Exception exc) 
     { 
     exc.printStackTrace(); 
     } 
    } 
    } 


} 

... 그리고 내 JNLP 파일 :

<?xml version="1.0" encoding="utf-8"?> 
<jnlp codebase="http://localhost/assets/" href="PortWriter.jnlp"> 
    <information> 
     <title>RS232 Communication Applet</title> 
     <vendor>My Company</vendor> 
     <description>RS232 Applet for communicating with POS Display Pole</description> 
     <offline-allowed /> 
    </information> 
    <security> 
    <all-permissions/> 
    </security> 
    <update check="background" /> 
    <resources> 
    <jar href="PortWriter.jar" part="com" main="true" /> 
    <jar href="winjcom.jar" part="com" /> 
    <nativelib href="jcomport.jar" part="com" /> 
    </resources> 
    <applet-desc 
     name="RS232 Communication Applet" 
     main-class="PortWriter" 
     width="1" height="1" /> 
</jnlp> 

... 그리고 내 HTML

내 된 .java 파일입니다 이 물건을 작동 시키면 되겠습니까? 나는 Java newb이지만 작업 솔루션을 얻고 싶습니다. 이것은 Rails에서 만들고있는 회사의 POS를위한 것입니다.

최종 파일은 다음과 같습니다

/자산/자바 /에서 서버에서

:
1) jcomport.jar (서명되지 ...)
2) PortWriter.class (및 모든 관련 클래스 파일)
3) PortWriter.jar
4) PortWriter.% 자바 홈 %에 서명하지/
1) /lib/ext/jcomport.jar()
2)에서 로컬 HD에 JNLP

은 자바의 패키지를 사용하기 위해서는

+0

JNLP가 잘못되었습니다. [JaNeLA] (http://pscode.org/janela/)를 사용하여 검증 해보십시오. –

+0

나는 그랬고, 결과적으로 나의 대답에 명시된 레이아웃에 도달했다 ... 반드시 그런 것은 아니었다. – JakeTheSnake

답변

2

잘 거룩한 영원한 쓰레기. 파이어 폭스에서 애플릿이 작동하도록하기 위해 원래 Java 콘솔에서 "차세대 플러그인"옵션을 끄기로 결정한 후에 실수로 JNLP를 사용하지 않게되었습니다. 나는 최후의 수단 노력으로 뭔가를 시험해 보았지만 아무것도 발견하지 못했습니다. 다시 켜면 jnlp는 IE에서 완벽하게 실행됩니다. 파이어 폭스는 물론 POS (jsprint addon 때문에)를 사용해야하는 브라우저입니다 ... 물론 작동하지 않습니다. JNLP 파일을 다운로드하지 않거나 오류가있는 경우 문제가 발생합니다. 그냥 달려있다. 아무것도 나타나지 않지만 내 서버 로그는 모든 이미지와 HTML이 다운로드되고 있음을 나타냅니다. 아니 jnlp 또는 jar 파일 중 하나. 따라서 차세대 플러그인을 끄고 보안 문제가 있거나 전원을 켜고 Firefox를 사용할 수 없게됩니다. 정책 파일을 변경하지 않고도 내가 할 수있는 일이 있습니까?

FWIW, 해결책으로 표시하고 최신 문제에 대한 새로운 스레드를 시작하겠습니다. 여기 (파이어 폭스 제외) 저주받은 일이 작업을 진행하는 데 필요한 모든의 가장 업데이트 된 내용입니다 :


첫째로는, 경로 및 CLASSPATH 환경 변수가 Windows에서 설정되어 있는지 확인합니다. PATH는 C:\Program Files (x86)\Java\jdk1.7.0_01\bin이어야합니다 (다른 버전 인 경우 자신의 jdk 디렉토리로 변경하십시오). CLASSPATH는 자바 클래스를 만들 때마다 있어야합니다. 내 것은 D:\Files\Java\Classes입니다. PATH 변수를 설정하지 않으면 모든 디렉토리에서 'java', 'javac'또는 'jarsigner'를 실행할 수 없습니다. jdk의 bin 디렉토리에 있어야합니다. 시스템 변수가 아닌 사용자 계정에 대해 다음 변수를 만듭니다. 이미 이러한 이름의 env 변수가있을 수 있습니다.


다음으로, 애플릿에 서명, 키 스토어를 만들 :

keytool -genkey -dname "cn=My Company, ou=Whatever, o=My Company, c=CA" -alias mycompany -keystore "D:\Files\Java\Certificates\certfile" -storepass MY_PASSWORD -validity 180 

확인 온라인 자습서를 사용하여 이러한 각 인수가 무엇인지 확인합니다.


.BAT 파일 .java 파일에서 필요한 .jar 파일을 쉽게 만들고 서명했습니다. 그냥 바로 가기를 만들거나 손으로 당신이 된 .java 파일

@echo off 

set certificate_password=MY_PASSWORD 
set certificate_alias=myalias 
set package=mycompany 
set class_file=PortWriter 
rem class_path is where my .java file resides (...\java\Classes\mycompany\PortWriter.java) 
set class_path=D:\Files\Java\Classes 
set certificate_loc=D:\Files\Java\Certificates\certfile 
rem final_loc is the assets folder where the .jar file will reside 
set final_loc=D:\Files\Websites\pos\app\assets\java 

cd "%class_path%" 
rem Change to "D:" otherwise Windows won't *actually* change directories from C: to D: 
D: 

javac -Xlint:unchecked "%package%\%class_file%.java" 
pause 
jar cfm "%final_loc%\%class_file%.jar" "%package%\Mainfest.txt" "%package%\*.class" 
pause 
del "%package%\*.class" 
jarsigner -keystore "%certificate_loc%" -storepass %certificate_password% "%final_loc%\%class_file%.jar" %certificate_alias% 
pause 

Mainfest.txt을 변경할 때마다이 작업을 수행해야하는 .BAT 파일을 대신 실행 (캐리지 리턴이 있는지 확인 JNLP에서 주 클래스를 지정하려면이 Manifest.txt 파일이 필요하지 않습니다.) : Main-Class : mycompany.PortWriter


자바 파일 :

package mycompany; 

import com.engidea.comm.CommPort; 
import com.engidea.comm.CommPortIdentifier; 
import com.engidea.comm.SerialPort; 
import com.engidea.comm.SerialPortEvent; 
import com.engidea.comm.SerialPortEventListener; 
import com.engidea.win32jcom.WinjcomIdentifier; 
import java.io.*; 
import java.util.Iterator; 
import java.util.List; 
import java.applet.*; 
import java.security.*; 

/* 
    WinJcom is a native interface to serial ports in java. 
    Copyright 2007 by Damiano Bolla, [email protected] 

    This library is free software; you can redistribute it and/or 
    modify it under the terms of the GNU Library General Public 
    License as published by the Free Software Foundation; either 
    version 2 of the License, or (at your option) any later version. 
    This can be used with commercial products and you are not obliged 
    to share your work with anybody. 

    This library is distributed in the hope that it will be useful, 
    but WITHOUT ANY WARRANTY; without even the implied warranty of 
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 
    Library General Public License for more details. 

    You should have received a copy of the GNU Library General Public 
    License along with this library; if not, write to the Free 
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
*/ 

/** 
* Simple class that can list system ports and allow IO 
*/ 
public class PortWriter extends Applet { 

    private CommPortIdentifier portIdentifier; 
    private SerialPort serport; 

    public static void main(String[] args) {} 
    public void init() { 
    System.out.println("Booting RS232 Java Applet..."); 
    } 

    public void Sends(String port, String message) { 

    final String com_port = port; 
    final String send_message = message; 

    AccessController.doPrivileged(new PrivilegedAction<Object>() { 
     public Object run() { 

     try { 
      portIdentifier = new WinjcomIdentifier(0); 
      try { 
      selectComport(com_port); 
      new Thread(new PortReader()).start(); 
      try { 
       System.out.println(com_port + ": " + send_message); 
       typeSendBytes(send_message); 
      } catch (Exception e) { 
       System.out.println("Couldn't send data to " + com_port); 
      } 
      } catch (IOException e) { 
      System.out.println("Couldn't connect to " + com_port); 
      } 
     } catch (Exception e) { 
      System.out.println(e); 
     } 
     return null; 
     } 
    }); 
    } 

    private void typeSendBytes(String message) { 
    try { 
     BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); 
     String aStr = ""; 
     if (aStr != null) { 
     aStr = message + "\r\n"; 
     // WARNING: be careful, you shoulod select the encoding ! 
     // This will timeout if you have FLOW CONTROL and theline is stuck ! 
     byte []buf = aStr.getBytes(); 
     serport.write(buf,0,buf.length); 
     } 
    } catch (Exception exc) { 
     exc.printStackTrace(); 
    } 
    } 

    private SerialPort openPort (String portName) throws IOException { 
    try { 
     CommPort aPort = portIdentifier.getCommPort(portName); 
     aPort.open(); 
     return (SerialPort)aPort; 
    } 
    catch (Exception exc) { 
     //System.out.println("exc="+exc); 
     //exc.printStackTrace(); 
     throw exc; 
    } 
    } 

    private void selectComport (String portName) throws IOException { 

    try { 
     serport = openPort(portName); 
     serport.setSerialPortParams(9600,8, SerialPort.STOPBITS_2, SerialPort.PARITY_NONE); 
     serport.enableReceiveTimeout(20000); 
     serport.setEventListener(new EventListener()); 

     serport.notifyOnDSR(true); 
     serport.notifyOnCarrierDetect(true); 
     serport.notifyOnCTS(true); 
    } catch (IOException exc) { 
     //System.out.println("Exc="+exc); 
     //exc.printStackTrace(); 
     throw exc; 
    } 

    } 


private final class EventListener implements SerialPortEventListener 
    { 
    public void serialEvent(SerialPortEvent ev) 
    { 
    System.out.println("Got event="+ev); 
    } 
    } 


private final class PortReader implements Runnable 
    { 
    public void run() 
    { 
    try 
     { 
     // This will timeout if nothing is received in the specified time. 
     byte []buff = new byte[1]; 
     while ( serport.read(buff,0,buff.length) > 0) 
     { 
     // NOTE: you should be checking the encoding ! 
     System.out.print(new String(buff)); 
     } 
     } 
    catch (Exception exc) 
     { 
     exc.printStackTrace(); 
     } 
    } 
    } 


} 

JNLP 파일 :

<?xml version="1.0" encoding="utf-8"?> 
<jnlp> 
    <information> 
     <title>RS232 Communication Applet</title> 
     <vendor>My Company</vendor> 
     <description>RS232 Applet for communicating with POS Display Pole</description> 
     <offline-allowed /> 
    </information> 
    <security> 
    <all-permissions/> 
    </security> 
    <resources> 
    <j2se version="1.7.0+" /> 
    <jar href="PortWriter.jar" part="com" main="true" /> 
    <jar href="jcomport.jar" part="com" /> 
    <nativelib href="winjcom.jar" part="com" /> 
    </resources> 
    <applet-desc 
     name="RS232 Communication Applet" 
     main-class="mycompany.PortWriter" 
     width="1" height="1" /> 
</jnlp> 

HTML :

<applet id="SerialPort" width="1" height="1" codebase="/assets/"> 
    <param name="jnlp_href" value="PortWriter.jnlp"> 
</applet> 

winjcom.dll 파일을 "jarred"하고 동일한 certfile로 서명했습니다. winjcom이 .jar 파일의 루트에 있도록 동일한 디렉토리에 cd해야합니다. 또한 winjcom의 저자가 제공 한 jcomport.jar 파일을 가져 와서 동일한 certfile로 다시 서명했습니다. 즉, 모든 .jar 파일은 동일한 certfile에 의해 서명되었습니다.

나는 이것이 내가했던 것처럼 많은 어려움을 겪고있는 사람을 돕기를 바랍니다.

0

을 /bin/winjcom.dll 장소 PortWriter.java 자바 내부/X/Y/Z (권장되는)과 제 라인을 가지고

x.y.z.PortWriter 
:

package x.y.z; 

어디에나 클래스 이름으로 사용

서버에서는 PortWriter 클래스가있는 jar 파일 만 필요합니다. DLL 사용없이 Test.java를 추가하여 작동하는지 확인할 수 있습니다.

+0

이것은 (여전히 클래스를 찾지 못한다.) 새로운 jar 파일을 제외하고 assets 폴더에서 모든 파일을 제거했다. JNLP를 단지 jar 파일을 참조하도록 변경하고 'archive'파일을 삭제했다. 속성을 내 애플릿 태그에서. 그냥 그것을 찾을 수 없다는 말했어! 항아리 폴더 디렉터리 내 로컬 파일 시스템 btw 모방 여전히 경우입니다. 클래스 경로 옵션을 'javac'설정 시도했지만 때 이동 (클래스 경로 옵션은 없습니다), 하위 디렉토리를 앞에서 설명한 방식으로 만듭니다. – JakeTheSnake

+0

부록 : Windows 환경 변수 "PATH"(Java 디렉토리에) 및 "CLASSPATH"(패키지 디렉터리) 내가 찾고 있던 jar 구조를 만듭니다. 여전히 클래스를 찾는 일은 계속 : \ – JakeTheSnake

+0

또한 클래스 파일을 직접 다운로드하려고하기 때문에 클래스를 찾지 못할 것입니다. 그것은 j를보고 있지 않다. ar 파일을 찾으십시오 (서버 로그를 보았습니다). – JakeTheSnake

관련 문제