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…
November 5th, 2010 at 23:04
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!
November 19th, 2010 at 11:16
Works perfectly. Thanks!!
March 24th, 2011 at 21:36
Awesome, this helped so much!
April 14th, 2011 at 17:22
Awesome trick !!!
June 7th, 2011 at 23:34
Thanks. Top search result.
July 21st, 2011 at 12:54
Excellent! It helped me so much.
October 12th, 2011 at 03:41
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
October 15th, 2011 at 10:19
Very cool, thanks a lot !!!
November 11th, 2011 at 14:45
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
March 20th, 2012 at 09:47
You are really brillient……
It working in all platforms
July 20th, 2012 at 20:49
Awesome, thank you! This is indeed the reason I love Java!
September 7th, 2012 at 09:26
Can anyone provide the full code,to use this,i am a bit confused.
September 8th, 2012 at 20:46
Awesome-o!
Thanks a lot, this trick should be treated with care i guess, but it certainly helps in so many cases!!
September 9th, 2012 at 16:13
You made my day, period.
September 15th, 2012 at 23:43
Just unbelievable, works like a charm for me.
September 28th, 2012 at 08:32
Thanks for publishing this very neat little trick.
October 21st, 2012 at 14:08
Really nice one !!
I’ve tried to set the path it didn’t work, then I googled the problem and found you, lucky !!!!!
Thanks
November 7th, 2012 at 10:48
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
November 7th, 2012 at 15:16
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…
November 25th, 2012 at 04:05
Thanks! Great tip!