java, System.loadlibrary(“someDLLFile”) gets unsta

2019-04-15 20:50发布

I have written some JNI hooks into a C++ library and created some DLL files for my java server project. Lets say the DLL and jar files are in the same folder under "C:/server"

I am accessing these DLL files using:

System.loadLibrary("someDLLFile");

in the class that needs the C++ code.

The problem I am running into is when I run this server on my own machine, everything works fine regardless of where I place the "server" folder. But when I give it to a colleague to test, they continually get:

java.lang.UnsatisfiedLinkError no someDLLFile in java.library.path

I want to have the DLL files live in the same folder as the jar files and would prefer not having someone configure their PATH variable.

Why does System.loadLibrary() work on my own machine regardless of the folder's location, but not on another computer?

4条回答
We Are One
2楼-- · 2019-04-15 21:09

It works because the DLL (or a DLL it depends on, i.e. msvcr90.dll or something) are in the PATH on your machine, but not on the other one.

Either set PATH env-var or the java.library.path property to contain the dir with your file, or store your dll where java finds it by default (Many options here, depending on deployment strategy and platform).

查看更多
男人必须洒脱
3楼-- · 2019-04-15 21:11

One option is to specify the directory in the command line when you start the VM:

java -classpath C:\server -Djava.library.path=C:\server somePackage.Main

Another option is to use System.load instead of System.loadLibrary.

URL url = Test.class.getResource("someDLLFile.dll");
String f = new File(url.getFile()).getAbsolutePath();
System.load(f);

The downside is that your program is now dealing with platform-dependent directory names, file extensions etc.

查看更多
【Aperson】
4楼-- · 2019-04-15 21:23

I'm not sure if this is helpful or not, but I have included the following in some projects:

http://forums.sun.com/thread.jspa?threadID=707176

To load native libraries.

And then I just load the bin directory

    String binPath = new File(".").getAbsolutePath() 
                     + System.getProperty("file.separator") + "bin";

   addDir( binPath );

It works pretty well.

But Again, I'm not sure if this is the case or not.

查看更多
叼着烟拽天下
5楼-- · 2019-04-15 21:26

Try downloading depends.exe to see if the dll depends on other dlls on the system or not. If it does, then check the other machine, whether such dlls are present or not in Path.

查看更多
登录 后发表回答