Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java.library.path setting programmatically

Can I set java.library.path programmatically from java code itself?

The following doesn't work.

    System.setProperty("java.library.path", "/blah");
like image 643
Fakrudeen Avatar asked Aug 02 '11 09:08

Fakrudeen


People also ask

What is the default java library path?

Its default value depends on the operating system: On Windows, it maps to PATH. On Linux, it maps to LD_LIBRARY_PATH. On OS X, it maps to DYLD_LIBRARY_PATH.

Where is java library path in Linux?

Use java -XshowSettings:properties to show the java. library. path (and others) value. Thanks Jose, this helped me.


2 Answers

Maybe this will help: Setting "java.library.path" programmatically

When messing around with JNI, one has to set the java.library.path accordingly. Unfortunately the only way is to add a system property before the application is started:

java -Djava.library.path=/path/to/libs  

Changing the system property later 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 to null, it is initialized automatically. Therefore forcing that field to null will result into the reevaluation of the library path as soon as loadLibrary() is called…

like image 99
secmask Avatar answered Sep 22 '22 13:09

secmask


No you can't. This property is a read only value. You can change it at JVM launchin time with:

-Djava.library.path=your_path

If you want to load a library from a specific location, you can use System.load(libraryPath) instead with the full path to the library.

like image 44
Manuel Selva Avatar answered Sep 21 '22 13:09

Manuel Selva