以编程方式设置 Java.library.path

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6909581/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 17:52:26  来源:igfitidea点击:

Java.library.path setting programmatically

javajava-native-interface

提问by Fakrudeen

Can I set java.library.path programmatically from java code itself?

我可以从 java 代码本身以编程方式设置 java.library.path 吗?

The following doesn't work.

以下不起作用。

    System.setProperty("java.library.path", "/blah");

回答by secmask

Maybe this will help: Setting "java.library.path" programmatically

也许这会有所帮助:以编程方式设置“java.library.path”

When messing around with JNI, one has to set the java.library.pathaccordingly. Unfortunately the only way is to add a system property beforethe application is started:

在使用 JNI 时,必须相应地设置java.library.path。不幸的是,唯一的方法是在应用程序启动之前添加系统属性:

java -Djava.library.path=/path/to/libs  

Changing the system property later doesn't have any effect, since the property is evaluated very early and cached. But the guys over at jdic discovered a way how to work around it. It is a little bit dirty – but hey, those hacks are the reason we all love Java…

稍后更改系统属性没有任何影响,因为该属性很早就被评估并缓存。但是 jdic 的人发现了一种解决方法。它有点脏——但是,嘿,这些 hack 是我们都喜欢 Java 的原因......

System.setProperty( "java.library.path", "/path/to/libs" );  
Field fieldSysPath = ClassLoader.class.getDeclaredField( "sys_paths" );  
fieldSysPath.setAccessible( true );  
fieldSysPath.set( null, null );  

Explanation

解释

At first the system property is updated with the new value. This might be a relative path – or maybe you want to create that path dynamically.

首先,系统属性用新值更新。这可能是一个相对路径 - 或者您可能想动态创建该路径。

The Classloader has a static field (sys_paths) that contains the paths. If that field is set to null, it is initialized automatically. Therefore forcing that field to null will result into the reevaluation of the library path as soon as loadLibrary()is called…

类加载器有一个sys_paths包含路径的静态字段 ( )。如果该字段设置为空,则会自动初始化。因此,强制该字段为空将导致在loadLibrary()调用后立即重新评估库路径......

回答by Manuel Selva

No you can't. This property is a read only value. You can change it at JVM launchin time with:

不,你不能。此属性是只读值。您可以在 JVM 启动时更改它:

-Djava.library.path=your_path

If you want to load a library from a specific location, you can use System.load(libraryPath)instead with the full path to the library.

如果要从特定位置加载库,可以使用System.load(libraryPath)代替库的完整路径。

回答by MNos

I'm just quoting from the link provided by secmask (https://cedarsoft.com/blog.html), in case the link goes dead:

我只是引用 secmask ( https://cedarsoft.com/blog.html)提供的链接,以防链接失效:

Changing the system property java.library.pathlater doesn't have any effect, since the property is evaluated very early and cached. But the guys over at jdic discovered a way how to work around it. It is a little bit dirty – but hey, those hacks are the reason we all love Java.

java.library.path稍后更改系统属性没有任何影响,因为该属性很早就被评估并缓存。但是 jdic 的人发现了一种解决方法。它有点脏——但是,嘿,这些 hack 是我们都喜欢 Java 的原因。

System.setProperty("java.library.path", "/path/to/libs");
Field fieldSysPath = ClassLoader.class.getDeclaredField("sys_paths");
fieldSysPath.setAccessible(true);
fieldSysPath.set(null, null);

Explanation:

At first the system property is updated with the new value. This might be a relative path – or maybe you want to create that path dynamically. The Classloader has a static field (sys_paths) that contains the paths. If that field is set to null, it is initialized automatically.Therefore forcing that field to nullwill result into the reevaluation of the library path as soon as loadLibrary()is called.

解释:

首先,系统属性用新值更新。这可能是一个相对路径 - 或者您可能想动态创建该路径。类加载器有一个sys_paths包含路径的静态字段 ( )。如果该字段设置为null,则它会自动初始化。null因此,一旦loadLibrary()调用,强制该字段将导致重新评估库路径。

回答by Sunil Gulabani

Yes it will read the Environment Variables. Following is the code for setting the Environment variable using the ini4j.

是的,它会读取环境变量。以下是使用 ini4j 设置环境变量的代码。

import java.io.IOException;
import org.ini4j.Reg;


public class SettingWinEnvironmentUsing_ini4j {

    public static void main(String args[])
    {
        System.out.println("Setting System Environment Variables");

        Reg reg = new Reg();
        Reg.Key env = reg.add("HKEY_CURRENT_USER\Environment");
        env.put("RR_PROPERTY_PATH", "c:\path");
        try {
            reg.write();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println(env.get("RR_PROPERTY_PATH"));
    }
}

You can find the ini4j jar at

您可以在以下位置找到 ini4j jar

http://cropforge.org/plugins/scmsvn/viewcvs.php/IAPlugin/lib/ini4j-0.5.2-SNAPSHOT.jar?rev=656&root=icisjavatools&view=log

http://cropforge.org/plugins/scmsvn/viewcvs.php/IAPlugin/lib/ini4j-0.5.2-SNAPSHOT.jar?rev=656&root=icisjavatools&view=log

回答by Sunil Gulabani

import java.util.Map;

public class ReadingEnvironment {

    public static void main(String[] args) {
        System.out.println("Reading System Environment Variables:\n");
        // System.out.println(System.getenv());
        Map<String, String> env = System.getenv();
        for (String envName : env.keySet()) {
            System.out.format("%s=%s%n", envName, env.get(envName));
        }
    }
}