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

安稳与你 提交于 2019-12-18 03:03:38

问题


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

回答1:


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.




回答2:


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


来源:https://stackoverflow.com/questions/9551588/can-i-find-out-what-variable-java-library-path-maps-to-on-the-current-platform

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!