问题描述
在Java中传递-D参数时,编写命令行然后从代码访问它的正确方法是什么?
例如,我已经尝试过这样写...
-jar 。请尝试以下方法:java -Dtest =true-jar myApplication.jar
从命令行帮助:
-options] -jar jarfile [args ...]
换句话说,现在将把 -Dtest =true作为传递给 main 的参数之一,而不是作为JVM参数。
(你应该也可以删除引号,但它可能很好工作 - 它可能取决于你的shell。 )
When passing a -D parameter in Java, what is the proper way of writing the command-line and then accessing it from code?
For example, I have tried writing something like this...
if (System.getProperty("test").equalsIgnoreCase("true")) { //Do something }And then calling it like this...
java -jar myApplication.jar -Dtest="true"But I receive a NullPointerException. What am I doing wrong?
解决方案I suspect the problem is that you've put the "-D" after the -jar. Try this:
java -Dtest="true" -jar myApplication.jarFrom the command line help:
java [-options] -jar jarfile [args...]In other words, the way you've got it at the moment will treat -Dtest="true" as one of the arguments to pass to main instead of as a JVM argument.
(You should probably also drop the quotes, but it may well work anyway - it probably depends on your shell.)
这篇关于正确使用Java -D命令行参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!