开发者

Provide DLL path to System.loadLibrary on export

I want to export my Java application which uses JNI interface, and loads a DLL via System.loadLibrary("dllName").

The DLL file is present inside Java Project folder as well as in C drive, one of places where JVM will search for DLL at runtime.

Problem: When I export this project out as a Jar and give it to client, client should be able to run the tool without hassles of entering a Dll file. I can't think开发者_如何学JAVA of accomplishing this via alternative way; to provide absolute path by using System.load("path:\\") because I don't know where the user would download the Jar file to.


The following snippet will load the DLL regardless of the working directory if it's loacted in the same directory as the JAR file:

CodeSource codeSource = MainClass.class.getProtectionDomain().getCodeSource();
File jarFile = new File(codeSource.getLocation().toURI().getPath());
File parentDir = jarFile.getParentFile();
File dllFile = new File(parentDir, "my.dll");
System.load(dllFile.getPath());


You need to put the DLL in the same path that the application is running, in a system path or add its path the PATH variable before starting the app.


Have a look at Runtime.load.

Loads the specified filename as a dynamic library. The filename argument must be a complete path name. From java_g it will automagically insert "_g" before the ".so" (for example Runtime.getRuntime().load("/home/avh/lib/libX11.so");).

First, if there is a security manager, its checkLink method is called with the filename as its argument. This may result in a security exception.

This is similar to the method loadLibrary(String), but it accepts a general file name as an argument rather than just a library name, allowing any file of native code to be loaded.

The method System.load(String) is the conventional and convenient means of invoking this method.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜