-Djava.library.path = ...等效于System.setProperty(" java.library.path",...) [英] Is -Djava.library.path=... equivalent to System.setProperty("java.library.path", ...)

查看:199
本文介绍了-Djava.library.path = ...等效于System.setProperty(" java.library.path",...)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我加载了一个放在 ./ lib 中的外部库。这两个解决方案是否设置java.library.path等效?

I load an external library that is placed in ./lib. Are these two solutions to set the java.library.path equivalent?


  1. 执行jar时在控制台中设置路径:

  1. Set path in console when executing jar:

java -Djava.library.path=./lib -jar myApplication.jar


  • 在加载库之前设置代码中的路径:

  • Set path in the code before loading library:

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


  • 如果相等 em>,为什么在第二个解决方案中Java可以找到库而第一个没问题呢?

    If they are equivalent, why in the second solution can Java not find the library while the first one is ok?

    如果不是,有没有办法在代码中设置路径?

    If not, is there a way the set the path in the code?

    推荐答案

    一般来说,两种方法都具有相同的净效果,因为系统属性 java.library.path 设置为值 ./ lib

    Generally speaking, both approaches have the same net effect in that the system property java.library.path is set to the value ./lib.

    但是,某些系统属性仅在特定时间点进行评估,例如JVM的启动。 如果 java.library.path 属于这些属性(并且您的实验似乎表明了这一点),那么使用第二种方法将没有明显的效果除了在 getProperty()的未来调用中返回新值。

    However, some system properties are only evaluated at specific points in time, such as the startup of the JVM. If java.library.path is among those properties (and your experiment seems to indicate that), then using the second approach will have no noticeable effect except for returning the new value on future invocations of getProperty().

    根据经验,使用 -D 命令行属性适用于所有系统属性,而 System.setProperty()仅适用于非属性仅在启动期间检查。

    As a rule of thumb, using the -D command line property works on all system properties, while System.setProperty() only works on properties that are not only checked during startup.

    这篇关于-Djava.library.path = ...等效于System.setProperty(" java.library.path",...)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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