So far I've learned the following about the java.library.path property:
(Please correct me if I've misunderstood any of the above)
My motivation:
I want to modify the value of java.library.path seen by a Java application from the framework I've set up to invoke the Java application. I want to do this not by setting the java.library.path property directly, but instead by modifying the system path variable that it maps to. I'd like a clean way to do this that doesn't have ugly OS-specific code or leave out edge cases if possible.
My question:
Is there a way to ask the local Java implementation what environment variable java.library.path maps to?
Then, in a shell script, I'd be able to write something along the lines of:
path_var = get_library_path_variable # the magic function I want to call ${path_var} = /my/custom/path:${${path_var}}
If you have single native libs in your project, there is another way you can do it. Go to Project properties->Java Build Path->Source. You'll find a list of source-folders. Each entry under the the Source tab has Native library locations.
Furthermore the Java library path contains the directories /lib/ and /usr/lib/ per default. According to the Filesystem Hierachy Standard the /lib/ directory should contain essential shared libraries and kernel modules. The /usr/lib/ directory should contain libraries for programming and packages.
java. library. path is a System property, which is used by Java programming language, mostly JVM, to search native libraries, required by a project. Similar to PATH and Classpath environment variable, java.
The library path environment variable tells Java™ applications that run on AIX® and Linux®, such as the JVM, where to find shared libraries. The location of shared libraries is important when they are located in a different directory from the directory that is specified in the header section of the program.
This isn't a totally unreasonable question, but there's no good answer, so for posterity, I'll make an attempt at explaining why you got stuck, and why it won't work.
java.library.path
isn't guaranteed to be set from an environment variable at all. You can specify what you want it to be with -Djava.library.path=
. More than likely, this is what you really want to do anyway. That's why the option exists.
It turns out (on windows at least), that the environment variable you're looking for isn't just used unmolested. Try this code.
package com.stackoverflow; import java.util.Map; public class LibPathFinder { public static void main(String[] args) { String javaLibPath = System.getProperty("java.library.path"); Map<String, String> envVars = System.getenv(); System.out.println(envVars.get("Path")); System.out.println(javaLibPath); for (String var : envVars.keySet()) { System.err.println("examining " + var); if (envVars.get(var).equals(javaLibPath)) { System.out.println(var); } } } }
You'll note that when it runs, the first two things it prints are different. If Java is using the windows PATH
variable, it's fiddling with the value first. I gave up investigating what was going on. The point was, there wasn't an environment variable that exactly matched the java.library.path
. I didn't try on Linux or OSX, your mileage may vary
It's really not very nice to mess with somebody's environment variables like this. They're used for the entire shell, so you're committing your users to have your shared library on their environment, but only sometimes. The only real reason to change java.library.path
is to add native libraries. If you're using native libraries, then you have OS-specific code already (it has to be compiled for the platform, right?), so you've really already given up the fight for "no platform-specific edge cases". The best thing to do is to put your native library in a place where the system path (whatever that may be) will already find it, or add your library's path to it permanently with an installer of some kind. If you don't want to do either of those things, then I'd suggest using a variation of @malat's code, printing the real java.library.path
, and then appending your path to that result in your script, and then use the -D
option to set it for the real program run.
On my linux box, here is what I would do:
$ cat GetSystemProperty.java import java.util.Properties; import java.util.Enumeration; public class GetSystemProperty { public static void main(String args[]) { if( args.length == 0 ) { Properties p = System.getProperties(); Enumeration keys = p.keys(); while (keys.hasMoreElements()) { String key = (String)keys.nextElement(); String value = (String)p.get(key); System.out.println(key + " : " + value); } } else { for (String key: args) { System.out.println(System.getProperty( key )); } } } } $ javac GetSystemProperty.java $ java GetSystemProperty java.library.path /usr/lib/jvm/java-6-openjdk/jre/lib/amd64/server:/usr/lib/jvm/java-6-openjdk/jre/lib/amd64:/usr/lib/jvm/java-6-openjdk/jre/../lib/amd64:/usr/java/packages/lib/amd64:/usr/lib/jni:/lib:/usr/lib
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With