正确使用Java -D命令行参数 [英] Proper usage of Java -D command-line parameters

查看:2366
本文介绍了正确使用Java -D命令行参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java中传递-D参数时,编写命令行然后从代码访问它的正确方法是什么?



例如,我已经尝试过这样写...

  if(System.getProperty(test)。equalsIgnoreCase(true))
{
//做某事
}
/ pre>

然后这样调用...

  java -jar myApplication.jar -Dtest =true

但是我收到一个NullPointerException。

我怀疑你的问题是你把-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.jar

From 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命令行参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆