I want to load an external library (eg:Lic64bit) 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?
Anjana is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.