NetBeans java.lang.UnsatisfiedLinkError:加载gnu.io.RXTXCommDriver时抛出的java.library.path中没有rxtxSerial

时间:2015-11-28 20:24:05

标签: java netbeans rxtx

我正在尝试运行下面给出的代码:

   package serialiotest;

    import gnu.io.*;
    import java.io.*;
    import java.util.*;

    public class SerialIOTest 
    {
    public static void main(String[] args) 
        {
        System.out.println("Starting");
        java.util.Enumeration<CommPortIdentifier> portEnum 
                = CommPortIdentifier.getPortIdentifiers();

        while (portEnum.hasMoreElements())
            {
            CommPortIdentifier portIdentifier
                    = portEnum.nextElement();

            System.out.println(portIdentifier.getName() + " - "
                + getPortTypeName(portIdentifier.getPortType()));
            }
        } 

    static String getPortTypeName (int portType )
        {
        switch(portType)
            {
            case CommPortIdentifier.PORT_I2C:
                return "I2C";
            case CommPortIdentifier.PORT_PARALLEL:
                return "pParallel";
            case CommPortIdentifier.PORT_RAW:
                return "RAW";
            case CommPortIdentifier.PORT_RS485:
                return "RS485";
            case CommPortIdentifier.PORT_SERIAL:
                return "Serial";
            default:
                return "unknown type";
            }
        }
    }

生成的错误消息是:

      run:
      Starting
      java.lang.UnsatisfiedLinkError:  
           no rxtxSerial in ava.library.path    
           thrown while loading gnu.io.RXTXCommDriver
      Exception in thread "main" java.lang.UnsatisfiedLinkError: 
           no rxtxSerial in java.library.path
           at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1864)
           at java.lang.Runtime.loadLibrary0(Runtime.java:870)
           at java.lang.System.loadLibrary(System.java:1122)
           at gnu.io.CommPortIdentifier.<clinit>
              (CommPortIdentifier.java:123)
           at serialiotest.SerialIOTest.main(SerialIOTest.java:16)
           Java Result: 1
           BUILD SUCCESSFUL (total time: 1 second)

构建输出是:

          ant -f /home/crazyprof/NetBeansProjects/serialIOTest   
                -Dnb.internal.action.name=rebuild clean jar
          init:
    deps-clean:
    Updating property file: /home/crazyprof/NetBeansProjects/serialIOTest
       /build/built-clean.properties
    Deleting directory /home/crazyprof/NetBeansProjects/serialIOTest
       /build
    clean:
    init:
    deps-jar:
    Created dir: /home/crazyprof/NetBeansProjects/serialIOTest/build
    Updating property file: /home/crazyprof/NetBeansProjects/serialIOTest
      /build/built-jar.properties
    Created dir: /home/crazyprof/NetBeansProjects/serialIOTest/build  
      /classes
    Created dir: /home/crazyprof/NetBeansProjects/serialIOTest/build
      /empty
    Created dir: /home/crazyprof/NetBeansProjects/serialIOTest/build
      /generated-sources/ap-source-output
    Compiling 1 source file to /home/crazyprof/NetBeansProjects
      /serialIOTest/build/classes
    Note: /home/crazyprof/NetBeansProjects/serialIOTest/src/serialiotest
      /SerialIOTest.java uses unchecked or unsafe operations.
    Note: Recompile with -Xlint:unchecked for details.
    compile:
    Created dir: /home/crazyprof/NetBeansProjects/serialIOTest/dist
    Copying 1 file to /home/crazyprof/NetBeansProjects/serialIOTest/build
    Copy libraries to /home/crazyprof/NetBeansProjects/serialIOTest
          /dist/lib.
    Building jar: /home/crazyprof/NetBeansProjects/serialIOTest
      /dist/serialIOTest.jar
    To run this application from the command line without Ant, try:
    java -jar "/home/crazyprof/NetBeansProjects/serialIOTest
       /dist/serialIOTest.jar"
    jar:
    BUILD SUCCESSFUL (total time: 23 seconds)

我是Ubuntu 14.04的新手,所以答案必须相当详细。

1 个答案:

答案 0 :(得分:0)

错误很明显rxtx本机库不在你的java.library.path中。有很多方法可以将它们添加到java.library.path中:

  • 您可以通过将正确的* .so文件放在文件夹中,然后将-Djava.library.path=folder添加到您的java命令来添加它们。
  • 您可以将* .so文件放在JAVA_HOME / lib /(amd64 / i386)文件夹中。虽然我不推荐这个,因为它很乱。

如果您已经这样做了,可以通过在* .so上执行ldd命令来检查您的库是否存在某些依赖性。