java 以编程方式(或替代方案)设置 Djava.library.path?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15961483/
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
Setting Djava.library.path programmatically (or alternatives)?
提问by Jire
I am looking to set the VM argument Djava.library.pathprogrammatically. If this can't be done, what are the alternatives (if there are any)?
我希望以编程方式设置 VM 参数Djava.library.path。如果无法做到这一点,有哪些替代方案(如果有)?
采纳答案by Connr
take a look at this java doc http://docs.oracle.com/javase/6/docs/api/java/lang/System.html#setProperty(java.lang.String, java.lang.String)
看看这个 java 文档http://docs.oracle.com/javase/6/docs/api/java/lang/System.html#setProperty(java.lang.String, java.lang.String)
you want to call the setProperty(String, String) method.
你想调用 setProperty(String, String) 方法。
so it would look something like this in your case
所以在你的情况下它看起来像这样
System.setProperty("java.library.path","value_you_want");
回答by Jire
The solution is easy with this method:
使用此方法解决方案很简单:
public static void addLibraryPath(String pathToAdd) throws Exception {
Field usrPathsField = ClassLoader.class.getDeclaredField("usr_paths");
usrPathsField.setAccessible(true);
String[] paths = (String[]) usrPathsField.get(null);
for (String path : paths)
if (path.equals(pathToAdd))
return;
String[] newPaths = Arrays.copyOf(paths, paths.length + 1);
newPaths[newPaths.length - 1] = pathToAdd;
usrPathsField.set(null, newPaths);
}
回答by Evgeniy Dorofeev
java.library.path
is used when you load a dynamic library with System.loadLibrary(String libname). System.load(String filename) uses full file name and does not need java.library.path
.
java.library.path
使用 System.loadLibrary(String libname) 加载动态库时使用。System.load(String filename) 使用完整的文件名,不需要java.library.path
.