带有rxtx组件的Java applet,用于串行通信

时间:2011-03-31 18:13:10

标签: java applet serial-port rxtx

我正在尝试构建一个可以打开串口并与之通信的applet。我使用rxtxcomm.jar进行串行通信。我有一个小程序,完美地在eclipese环境中工作。我构建了Jar文件并对其进行了签名,但是当在浏览器中运行时,控制台会显示以下内容:

java.lang.ExceptionInInitializerError thrown while loading gnu.io.RXTXCommDriver
Exception in thread "thread applet-zhas_xbeeComm.xtalk-1" java.lang.ExceptionInInitializerError
    at zhas_xbeeComm.Xconnect$1.run(Xconnect.java:46)
    at java.security.AccessController.doPrivileged(Native Method)
    at zhas_xbeeComm.Xconnect.connect(Xconnect.java:40)
    at zhas_xbeeComm.xtalk.init(xtalk.java:22)
    at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.security.AccessControlException: access denied (java.lang.RuntimePermission loadLibrary.rxtxSerial)
    at java.security.AccessControlContext.checkPermission(Unknown Source)
    at java.security.AccessController.checkPermission(Unknown Source)
    at java.lang.SecurityManager.checkPermission(Unknown Source)
    at java.lang.SecurityManager.checkLink(Unknown Source)
    at java.lang.Runtime.loadLibrary0(Unknown Source)
    at java.lang.System.loadLibrary(Unknown Source)
    at gnu.io.CommPortIdentifier.<clinit>(CommPortIdentifier.java:123)
    ... 6 more

我甚至在连接和打开函数周围使用了doPrivileged方法,但它不起作用!请帮忙!! 以下是applet代码的片段:     {     / **打开端口并开始读写* /

的功能
public void connect ( final String portName ) throws Exception
{   

    AccessController.doPrivileged(new PrivilegedAction() {
    public Object run() {
        // privileged code goes here, for example:

        // 1. added try catch for no such port exception;
        try {
            portIdentifier = CommPortIdentifier.getPortIdentifier(portName); //line 46
            } catch (NoSuchPortException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            }

}

1 个答案:

答案 0 :(得分:3)

刚遇到同样的问题。 请确保第一次调用RXTX库是在doPrivileged块中。 如果它将尝试在特权块之前加载库 - 它将因此错误而失败。

其他一些信息: http://hacky.typepad.com/blog/2009/05/using-rxtxcomm-in-applets.html