Setting "java.library.path" programmatically

When messing around with JNI, one have 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…


20 Responses to “Setting "java.library.path" programmatically”

  • Sparkletron Says:

    Very cool! Probably the best Java trick I’ve seen in years, and particularly useful with respect to cross-platform development. Linux, for example, typically does not consider the location where an app is launched as the “current working directory”, and therefore does not include this location in java.library.path. Contrast this to Mac and Windows, which always considers the location where an app is launched as the user.dir. This trick allows a single executable JAR file to deal with native libs in a consistent manner. Thank you!

  • Darude Says:

    Works perfectly. Thanks!!

  • DV Says:

    Awesome, this helped so much!

  • Meenu Says:

    Awesome trick !!!

  • Eoin Says:

    Thanks. Top search result.

  • MockerTim Says:

    Excellent! It helped me so much.

  • Happy Soul Says:

    Oh My god, I can’t believe it worked. :)
    I have been looking for something like this for the past two months. I almost cried when it worked :) :)

  • Dmitry Says:

    Very cool, thanks a lot !!!

  • stef Says:

    whaaa… it worked :-)
    i got really crazy about that damn library loading stuff. I was just giving up hope, and then i found this…

    Thank you very much man… i love java, but sometimes grrr… haha :-)

  • Harikrushna Says:

    You are really brillient……
    It working in all platforms

  • stuchl4n3k Says:

    Awesome, thank you! This is indeed the reason I love Java!

  • Naina Says:

    Can anyone provide the full code,to use this,i am a bit confused.

  • HAL9K Says:

    Awesome-o!
    Thanks a lot, this trick should be treated with care i guess, but it certainly helps in so many cases!!

  • alex Says:

    You made my day, period.

  • me Says:

    Just unbelievable, works like a charm for me.

  • Warwick Says:

    Thanks for publishing this very neat little trick.

  • Jean-Charles Roger Says:

    Really nice one !!

    I’ve tried to set the path it didn’t work, then I googled the problem and found you, lucky !!!!!

    Thanks

  • Chinoy Says:

    Keep running into this exception..

    can not access a member of class java.lang.ClassLoader with modifiers “private static”

    Any help? using jdk 1.7.0

  • Johannes Schneider Says:

    Did you call “setAccessible(true)”?
    I never tried it with Java 7. So I don’t know for sure that it still works. I wouldn’t expect any changes, but who knows…

  • Kevin S. CLarke Says:

    Thanks! Great tip!

Leave a Reply