为什么 System.getProperties() returns null for -D defined 属性?

Why System.getProperties() returns null for -D defined property?

我是 运行 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(我添加它是为了查看 "something" 是否传递给程序),但我希望 -D 参数设置系统 属性。为什么 "bloody.prop" 为空?

PS: BatchUtils.argsToProperties() 做了你期望它做的事:从命令行解析 -argName "value"argName=值 属性 对。

在 java 命令的 class 参数 传递给你的 main in String[] args 并且没有进入 JVM。

解决方案是像这样重新排列属性:

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

我在网上浏览 http://duckduckgo.com 或谷歌搜索 "null -D defined property" 时,没有发现任何明确说明的地方。我后来在打印所有系统属性和整个参数数组时想到了很多,所以我发帖给其他人。