如何使用.dll制作可执行jar文件 - RXTX

时间:2015-02-01 18:44:02

标签: java dll serial-port executable-jar rxtx

我的程序应通过RS232进行通信,因此我使用RXTX中的.jar和两个.dll。最后,我想从一个.jar文件中运行它。

为了解决这个问题,我使用了this教程。但是如果我从Eclipse运行程序(或从控制台导出后),我会得到这个例外:

  

java.lang.UnsatisfiedLinkError:加载gnu.io.RXTXCommDriver时抛出的java.library.path中没有rxtxSerial   线程" main"中的例外情况java.lang.UnsatisfiedLinkError:java.library.path中没有rxtxSerial

以下是我的代码的最小示例

private static final String LIB = "lib/";
private final static String RXTXPARALLEL = "rxtxParallel";
private final static String RXTXSERIAL = "rxtxSerial";

static {
    try {
        System.loadLibrary(RXTXSERIAL);
        System.loadLibrary(RXTXPARALLEL);
        } catch (UnsatisfiedLinkError e) {
            loadFromJar();
        }
    }

public static void main(String[] args) {
    //RS232 is this class
    RS232 main = new RS232();
    main.connect("COM15");

}

private static void loadFromJar() {
    String path = "AC_" + new Date().getTime();
    loadLib(path, RXTXPARALLEL);
    loadLib(path, RXTXSERIAL);
}


private static void loadLib(String path, String name) {
    name = name + ".dll";
    try {
        InputStream in = ResourceLoader.load(LIB + name);
        File fileOut = new File(System.getProperty("java.io.tmpdir") + "/"
            + path + LIB + name);

        OutputStream out = FileUtils.openOutputStream(fileOut);
        IOUtils.copy(in, out);
        in.close();
        out.close();
        System.load(fileOut.getAbsolutePath());
    } catch (Exception e) {
        e.printStackTrace();
    }
}

private void connect(String portName) {

    CommPortIdentifier portIdentifier;
    try {
        //Here the exception is thrown
        portIdentifier = CommPortIdentifier.getPortIdentifier(portName);
    } catch (NoSuchPortException exc) {
        exc.printStackTrace();
        return;
    }
  //... some other code
}

有没有办法获得可执行的.jar文件?

1 个答案:

答案 0 :(得分:1)

您有几个选择。尝试复制运行时文件夹中的.dll文件,并在程序的每个启动时覆盖文件。第二个选项是复制修复文件夹中的文件,并将文件夹的路径添加到MS Windows中的环境变量中。您也可以在每次开始时覆盖文件。

另一种可能性是在runntime中将临时文件夹添加到MS Windows环境变量。但请注意此解决方案,有关详细信息,请参阅this帖子。

static {
    try {
        System.loadLibrary(RXTXSERIAL);
        System.loadLibrary(RXTXPARALLEL);
    } catch (UnsatisfiedLinkError exc) {
        initLibStructure();
    }
}


private static void initLibStructure() {

    try {
        //runntime Path
        String runPath = new File(".").getCanonicalPath();

        //create folder
        File dir = new File(runPath + "/" + LIB);
        dir.mkdir();

        //get environment variables and add the path of the 'lib' folder
        String currentLibPath = System.getProperty("java.library.path");
        System.setProperty("java.library.path",
                currentLibPath + ";" + dir.getAbsolutePath());

        Field fieldSysPath = ClassLoader.class
                .getDeclaredField("sys_paths");
        fieldSysPath.setAccessible(true);
        fieldSysPath.set(null, null);

        loadLib(runPath, RXTXPARALLEL);
        loadLib(runPath, RXTXSERIAL);

    } catch (Exception e) {
        e.printStackTrace();
    }
}

private static void loadLib(String path, String name) {
    name = name + ".dll";
    try {
        InputStream in = ResourceLoader.load(LIB + name);
        File fileOut = new File(path + "/" + LIB + name);

        OutputStream out = FileUtils.openOutputStream(fileOut);
        IOUtils.copy(in, out);
        in.close();
        out.close();
        System.load(fileOut.getAbsolutePath());
    } catch (Exception e) {
        e.printStackTrace();
    }
}