1

我写了一个程序使用applet通过rxtxComm.jar访问串口,我更改了java.policy,所以applet可以在没有签名的情况下访问串口。当程序在Windows上运行时,它是有效的。但是在ubuntu上有一些execptions(java.lang.reflect.InvocationTargetException 和“npobject 上的错误调用方法”)。为了解决这个问题,我用了很多次谷歌,我无法解决。有人有同样的问题吗?这是关于这个程序的代码。

打印.html

function print() {
    var zplText = $("#zplText").val();
    document.printApplet.print(zplText);
}



<textarea rows="5" cols="6" id="zplText"></textarea>
<a href="#" onClick="print()">print</a>
<applet id="printApplet" alt="" codebase=.. code="zpl/ZplPrint.class">
    <PARAM NAME="archive" VALUE="../lib/RXTXcomm.jar">
</applet>

ZplPrint.java

public class ZplPrint extends Applet {
static String zpl;
static CommPortIdentifier portId;
static CommPort commPort;
static SerialPort serialPort;
@SuppressWarnings("rawtypes")
static Enumeration portList;
static OutputStream out;

public void print(String zplText) {
    zpl = zplText;
    if (zpl == null) {
        return;
    }
    byte[] zplByte = zpl.getBytes();

    portList = CommPortIdentifier.getPortIdentifiers();
    while (portList.hasMoreElements()) {
        portId = (CommPortIdentifier) portList.nextElement();
        if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {

            try {

                commPort = portId.open(portId.getName(), 2000);
                serialPort = (SerialPort) commPort;

                serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1,
                        SerialPort.PARITY_NONE);
                out = serialPort.getOutputStream();
                out.write(zplByte);
                commPort.close();
            } catch (PortInUseException e) {
                e.printStackTrace();
            } catch (UnsupportedCommOperationException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (NullPointerException e) {
                e.printStackTrace();
            }

            System.out.println("Find CommPort: " + portId.getName());
        }
    }

}

java.policy

  • 权限 java.lang.RuntimePermission "loadLibrary.rxtxSerial";
  • 权限 java.io.FilePermission "${java.home}${/}lib${/}ext${/}x86${/}rxtxSerial.dll", "read";
  • 权限 java.util.PropertyPermission "gnu.io.log.mode", "read";
  • 权限 java.util.PropertyPermission "gnu.io.SerialPorts", "write,read";
  • 权限 java.util.PropertyPermission "gnu.io.rxtx.SerialPorts", "read,write";
  • 权限 java.util.PropertyPermission "gnu.io.ParallelPorts", "read";
  • 权限 java.util.PropertyPermission "gnu.io.rxtx.ParallelPorts", "read";
  • 权限 java.lang.RuntimePermission "modifyThreadGroup";
  • 权限 java.lang.RuntimePermission "modifyThread";

第一行和第二行与ubuntu不同,当ubuntu是:

  • 权限 java.lang.RuntimePermission "loadLibrary.librxtxSerial";
  • 权限 java.io.FilePermission "${java.home}${/}lib${/}amd64${/}librxtxSerial.so", "read";
4

1 回答 1

1

我已经解决了自己。

   permission java.lang.RuntimePermission "loadLibrary.rxtxSerial";

   permission java.io.FilePermission "${java.home}${/}lib${/}ext${/}amd64${/}librxtxSerial.so", "read";

   permission java.io.FilePermission "${java.home}${/}lib${/}ext${/}librxtxSerial.so", "read";

   permission java.io.FilePermission "${/}dev", "read";

   permission java.util.PropertyPermission "gnu.io.log.mode", "read";

   permission java.util.PropertyPermission "gnu.io.SerialPorts", "write,read";

   permission java.util.PropertyPermission "gnu.io.rxtx.SerialPorts", "read,write";

   permission java.util.PropertyPermission "gnu.io.ParallelPorts", "read";

   permission java.util.PropertyPermission "gnu.io.rxtx.ParallelPorts", "read";

   permission java.lang.RuntimePermission "modifyThreadGroup";

   permission java.lang.RuntimePermission "modifyThread";
于 2013-02-21T09:15:32.723 回答