Java - Loading dlls by a relative path and hide them inside a jar

前端 未结 4 852
遇见更好的自我
遇见更好的自我 2020-11-28 11:05

PART 1

I am developing a Java application that should be release as a jar. This program depends on C++ external libraries called by JNI. To load them, I use the me

4条回答
  •  佛祖请我去吃肉
    2020-11-28 11:37

    I don't believe you can load the DLL directly from the JAR. You have to take the intermediary step of copying the DLL out of the JAR. The following code should do it:

    public static void loadJarDll(String name) throws IOException {
        InputStream in = MyClass.class.getResourceAsStream(name);
        byte[] buffer = new byte[1024];
        int read = -1;
        File temp = File.createTempFile(name, "");
        FileOutputStream fos = new FileOutputStream(temp);
    
        while((read = in.read(buffer)) != -1) {
            fos.write(buffer, 0, read);
        }
        fos.close();
        in.close();
    
        System.load(temp.getAbsolutePath());
    }
    

提交回复
热议问题