Java 如何将系统属性传递给 jar 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29811377/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 08:38:09  来源:igfitidea点击:

How to pass system properties to a jar file

javamavenpropertiesjarsystem

提问by arjunj

I have a main class that expects certain properties that I pass using the -D option. I can access this in my IDE by sending them as VM options.

我有一个主类,它期望我使用 -D 选项传递某些属性。我可以通过将它们作为 VM 选项发送来在我的 IDE 中访问它。

I package this application into a jar file using Maven and when I try the following:

我使用 Maven 将此应用程序打包到一个 jar 文件中,当我尝试以下操作时:

java -jar myjar.jar -Denviroment=dev

java -jar myjar.jar -Denviroment=dev

or

或者

java -jar myjar.jar "-Denvironment=dev"

java -jar myjar.jar "-Denvironment=dev"

The enviroment system property is not getting picked up.

环境系统属性没有被获取。

Any pointers on what is going on?

关于发生了什么的任何指示?

采纳答案by Elliott Frisch

Pass the arguments before the -jar. If you pass them after the jar file they are interpreted as command line parameters and passed to the String[] argsin main. Like,

-jar.之前传递参数。如果在 jar 文件之后传递它们,它们将被解释为命令行参数并传递给String[] argsin main。喜欢,

java -Denviroment=dev -jar myjar.jar 

回答by Chin

Adding to Elliott's answer, if I just run this then I get an error:

添加到 Elliott 的答案中,如果我只运行它,则会出现错误:

java -Djna.nosys=true -jar jenkins.war

but if I add quotes like this then it works:

但如果我添加这样的引号,那么它的工作原理:

java "-Djna.nosys=true" -jar jenkins.war