System.loadLibrary() returns exception from static block

霸气de小男生 提交于 2020-01-06 18:03:48

问题


I have created a Java app which loads one Windows DLL from the static initialization block. The code snippet is given below:

// MyTestJava class starts
class MyTestJava
{

static
    {
        System.loadLibrary("MyLib");
    }
} // MyTestJava class def ends here

I heard that for kinds of library initializations performed from static block, JVM should search in java.library.path. So I set library path for JVM as follows and put MyLib.dll under c:\Libs folder.

options[1].optionString = "-Djava.library.path=C:\\Libs"; 

Unfortunately I'm getting a pesky Unsatisfied Link Error in my Java app.


回答1:


Not clear what options[1].optionString = "-Djava.library.path=C:\\Libs"; means. You should pass java.library.path as a java arg

java -Djava.library.path=C:\Libs MainClass



回答2:


First call getEnv and check if "java.library.path" actually refers to your library. Else You can try giving absolute path of file by using

static
{
System.load("C:\\Libs");
}


来源:https://stackoverflow.com/questions/16728586/system-loadlibrary-returns-exception-from-static-block

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