java - 为什么 System.getProperties() 对 -D 定义的属性返回 null?

标签 java jvm-arguments

我正在使用运行 jarball

java -classpath myBatch.jar some.package.MyMainClass \
     -bloodyArgument "I got the argument!" -Dbloody.prop="I got the prop!"

然后在我的主要内容中我有:

Properties argsProps = BatchUtils.argsToProperties(args);
System.out.println(argsProps.getProperty("bloodyArgument"));
System.out.println(System.getProperty("bloody.prop"));

我得到输出:

I got the argument!
null

我可以看到命令行 BloodyArgument (我添加它是为了查看“某些内容”是否传递给程序),但我希望使用 -D 参数来设置系统属性。为什么“bloody.prop”为空?

PS:BatchUtils.argsToProperties() 执行您期望的操作:将命令行中的 -argName "value" 解析为 argName=value 属性对。

最佳答案

java 命令的 class 参数之后的所有内容都被传递到 String[] args 中的 main ,并且不会到达 JVM。

解决方案是重新排列属性,如下所示:

java -classpath myBatch.jar -Dbloody.prop="I got the prop!" \
     some.package.MyMainClass -bloodyArgument "I got the argument!" 
<小时/>

当我在网络上使用http://duckduckgo.com时,我没有找到任何明确说明的地方。或谷歌搜索“null -D 定义的属性”的查询。我后来在打印所有系统属性和整个参数数组时想到了这一点,所以我为其他人发布。

关于java - 为什么 System.getProperties() 对 -D 定义的属性返回 null?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32650597/

相关文章:

java - Spring : how this field in controller is injected?

java - 如何确定是否在 JVM 实例上设置了 -server 选项

Jmeter : How to increase JVM heap space from Jmeter Plugin

java - 自动化 Tomcat 线程/堆转储以进行进一步分析

java - 如何解析Java servlet中的响应SOAP消息?

java - 返回与函数调用者类型相同的对象

java - 使用Java Sockets发送ByteArray(Android编程)

java - 单例模式的例子

java - -Djava.library.path 中的多个目录

java - log4j2 配置中的用户 JVM 参数