Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I find out what variable java.library.path maps to on the current platform?

So far I've learned the following about the java.library.path property:

  • It's used when loading native libraries, as opposed to java classes
  • 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

(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}} 
like image 391
Charlie Avatar asked Mar 04 '12 01:03

Charlie


People also ask

How do I find my Java library path?

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.

What is the default Java library path?

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.

What is Java library path in Windows?

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.

What is Java library path in Linux?

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.


2 Answers

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.

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

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

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

like image 159
Ian McLaird Avatar answered Sep 21 '22 10:09

Ian McLaird


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 
like image 31
malat Avatar answered Sep 22 '22 10:09

malat