我编写程序使用applet通过rxtxComm.jar访问串口,我更改了java.policy,这样applet就可以访问没有签名的串口。当程序运行时 Windows,它是有效的。但是在ubuntu上有一些execptions( java.lang.reflect.InvocationTargetException和“npobject上的错误调用方法”)。为了解决这个问题,我花了很多次谷歌,我无法解决。是否有人有同样的问题?这是关于这个程序的代码。
print.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 :
当ubuntu为:
时,第一行和第二行与ubuntu不同答案 0 :(得分: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";