Reputation: 1
I want to load an external library (eg:\Lic\64bit) which contains some DLL files inside it to JVM at runtime. Can't use Reflection and FFM (Java Foreign Function Memory API) as well.
I tried with some ways and still not loading the library properly.
Finally I found that there is a private static final variable called JAVA_LIBRARY_PATH
. So that I can't modify that as it is a final variable.
Already tried the below implementation.
private static void loadAllDLLs(String libraryPathToAdd) {
final String[] userPath = initializePath("java.library.path");
String ps = System.getProperty("path.separator");
//check if the path to add is already present
for (String path : userPath) {
if (path.equals(libraryPathToAdd)) {
return;
}
}
StringBuilder pathBuilder = new StringBuilder();
for (String pathToAdd : userPath) {
pathBuilder.append(pathToAdd).append(ps);
}
pathBuilder.append(libraryPathToAdd);
// Update java.library.path once with all paths
String finalPath = pathBuilder.toString();
System.setProperty("java.library.path", finalPath);
String[] newUserPath = initializePath("java.library.path");
for (String up : newUserPath) {
System.out.println("New User Path ----> " + up);
}
}
But then I tried to verify whether the library is successfully loaded using
private static void loadLibraries() {
String libraryPath = System.getProperty("java.library.path");
System.out.println("java.library.path: " + libraryPath);
try {
System.loadLibrary("webcam-capture-driver-native-64");
System.out.println("Library loaded successfully.");
} catch (UnsatisfiedLinkError e) {
System.err.println("Failed to load library: " + e.getMessage());
}
}
System.out.println("java.library.path: " + libraryPath);
shows that the library containing "webcam-capture-driver-native-64" is added to java.library.path
property. But when going to load the library by using System.loadLibrary("webcam-capture-driver-native-64");
There shows the exception as couldn't find the library.
Does anybody already faced this or have any idea to handle this?
Upvotes: 0
Views: 148
Reputation: 1216
The second answer to the question java.lang.UnsatisfiedLinkError no *****.dll in java.library.path states that the JVM is reading the library paths only once, meaning that even if you modify it at runtime, the JVM won't update it, and will still use the one loaded at startup.
The same answer also points to the blog post http://fahdshariff.blogspot.com/2011/08/changing-java-library-path-at-runtime.html that explains a solution to this problem (programatically resetting the field where the library path is stored, in order to "force" the JVM to re-read it).
This might not work for newer JDK versions anymore, as ClassLoader.class.getDeclaredFields()
will return an empty list. Depending on your JDK version, the solution above might work or not.
A more "actual" solution would be to directly specify the path to your .dll
file (including its name) by using:
System.load("path/to/dir/lib.dll");
... or simply just stick with the run command:
java -Djava.library.path=/path/to/dir
Upvotes: 1