如何将系统属性传递给 jar 文件 [英] How to pass system properties to a jar file
问题描述
我有一个主类,它需要我使用 -D 选项传递的某些属性.我可以通过将它们作为 VM 选项发送来在我的 IDE 中访问它.
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.
我使用 Maven 将此应用程序打包到一个 jar 文件中,当我尝试以下操作时:
I package this application into a jar file using Maven and when I try the following:
java -jar myjar.jar -Denviroment=dev
或
java -jar myjar.jar "-Denvironment=dev"
未获取环境系统属性.
有什么关于正在发生的事情的指示吗?
Any pointers on what is going on?
推荐答案
在 -jar
之前传递参数.如果在 jar 文件之后传递它们,它们将被解释为命令行参数并传递给 main
中的 String[] args
.喜欢,
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[] args
in main
. Like,
java -Denviroment=dev -jar myjar.jar
这篇关于如何将系统属性传递给 jar 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!