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}}
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.