Java applet with rxtx components for serial communication

隐身守侯 提交于 2019-12-03 22:53:30

问题


I am trying to build an applet that can open a serial port and communicate with the same. I have used rxtxcomm.jar for the serial communications. I have an applet built that works in the eclipese environment perfectly. I built the Jar file and signed the same, but when run in the browser the console shows the foll:

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

I have even used doPrivileged method around the connect and open functions but it aint working! Please help!! Here is a snippet of the code of the applet: { /** Function to open a port and begin reading and writing */

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:


Just had the same problem. Please make sure that the first call to RXTX library is in doPrivileged block. If it will try to load library before privileged block - it will fail with this error.

Some additional info: http://hacky.typepad.com/blog/2009/05/using-rxtxcomm-in-applets.html



来源:https://stackoverflow.com/questions/5504390/java-applet-with-rxtx-components-for-serial-communication

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!