我可以找出当前平台上java.library.path映射到的变量吗? [英] Can I find out what variable java.library.path maps to on the current platform?

查看:146
本文介绍了我可以找出当前平台上java.library.path映射到的变量吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到目前为止,我已经了解了有关java.library.path属性的以下内容:

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


  • 在加载本机库时使用它,如反对java类

  • 它的默认值取决于操作系统:

    • 在Windows上,它映射到PATH

    • 在Linux上,它映射到LD_LIBRARY_PATH

    • 在OS X上,它映射到DYLD_LIBRARY_PATH

    • 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)

      我的动机:

      我想修改Java应用程序从我为调用Java应用程序而设置的框架中看到的java.library.path的值。我想这样做不是通过直接设置java.library.path属性,而是通过修改它映射到的系统路径变量。我想要一个干净的方法来做这个没有丑陋的操作系统特定代码或尽可能省略边缘情况。

      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.

      我的问题:

      有没有办法让本地Java实现java.library.path映射到哪个环境变量?

      Is there a way to ask the local Java implementation what environment variable java.library.path maps to?

      然后,在一个shell脚本,我可以写下以下内容:

      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.


      1. java.library.path 。您可以使用 -Djava.library.path = 指定您想要的内容。很可能,这是你真正想要做的事情。这就是选项存在的原因。

      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.

      事实证明(至少在Windows上),您正在寻找的环境变量不仅仅是不受干扰的。试试这段代码。

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

      你请注意,当它运行时,它打印的前两个东西是不同的。如果Java使用的是windows PATH 变量,那么它首先会摆弄该值。我放弃了调查正在发生的事情。关键是,没有一个环境变量与 java.library.path 完全匹配。我没有尝试使用Linux或OSX,你的里程可能会有所不同

      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

      像这样混淆某人的环境变量真的不太好。它们用于整个shell,因此您要求用户在其环境中使用共享库,但有时仅 。更改 java.library.path 的唯一真正原因是添加本机库。如果您正在使用本机库,那么您已经拥有特定于操作系统的代码(必须为平台编译,对吧?),因此您已经放弃了没有特定于平台的边缘情况的争夺。最好的办法是将您的本机库放在系统路径(可能是什么)已经找到的地方,或者使用某种安装程序将库的路径永久添加到其中。如果您不想做其中任何一项,那么我建议使用@ malat代码的变体,打印真正的 java.library.path ,然后将您的路径附加到脚本中的结果,然后使用 -D 选项为实际程序运行设置它。

      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.

      这篇关于我可以找出当前平台上java.library.path映射到的变量吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆