Can I set java.library.path programmatically from java code itself?
The following doesn't work.
System.setProperty("java.library.path", "/blah");
Can I set java.library.path programmatically from java code itself?
The following doesn't work.
System.setProperty("java.library.path", "/blah");
I'm just quoting from the link provided by secmask (https://cedarsoft.com/blog.html), in case the link goes dead:
Changing the system property
java.library.pathlater doesn’t have any effect, since the property is evaluated very early and cached. But the guys over at jdic discovered a way how to work around it. It is a little bit dirty – but hey, those hacks are the reason we all love Java.
System.setProperty("java.library.path", "/path/to/libs");
Field fieldSysPath = ClassLoader.class.getDeclaredField("sys_paths");
fieldSysPath.setAccessible(true);
fieldSysPath.set(null, null);
Explanation:
At first the system property is updated with the new value. This might be a relative path – or maybe you want to create that path dynamically. The Classloader has a static field (
sys_paths) that contains the paths. If that field is set tonull, it is initialized automatically.Therefore forcing that field tonullwill result into the reevaluation of the library path as soon asloadLibrary()is called.