gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 22:06:37 28 4
gpt4 key购买 nike

我正在使用运行 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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com