我正在调试大型系统中的问题。 我能够在相当基本的Java功能中找到问题 - 应用程序以-D选项开始指定某些系统属性。 但是在运行时,所述属性未设置且其值为null。
试图重现这个问题,我创建了以下非常简单的应用程序:
public static void main(String[] args) { String propName = "sysprop"; String propValue = System.getProperty(propName); System.out.println(propName + "=" + propValue); }我使用以下命令行启动应用程序,令我惊讶的是得到了相同的结果:
java -cp javaapplication8.jar javaapplication8.JavaApplication8 -Dsysprop =“some value”
sysprop = NULL
显然我错过了一些明显的东西(或者缺乏一些基本的知识),但它是什么?
谢谢。
I am debugging a problem in a large system. I was able to locate the problem in a rather basic java feature - the application is started with the -D option to specify some system properties. During run-time though, said properties are not set and their values are null.
Trying to reproduce the issue I created the following very simple application:
public static void main(String[] args) { String propName = "sysprop"; String propValue = System.getProperty(propName); System.out.println(propName + "=" + propValue); }I start the application with the following command line and to my surprise got the same result:
java -cp javaapplication8.jar javaapplication8.JavaApplication8 -Dsysprop="some value"
sysprop=null
Apparently I am missing something obvious (or lack some basic knowledge) but what is it?
Thank you.
最满意答案
您在类名后面指定-D ,因此它被视为常规命令行参数,以传递给应用程序本身。 你会看到,如果你打印出args的值。
改为运行此应用程序:
java -cp javaapplication8.jar -Dsysprop="some value" javaapplication8.JavaApplication8所有适用于环境而不是应用程序的参数都应该在类名之前。 如果你只运行java ,你会看到像这样的使用帮助:
Usage: java [-options] class [args...] (to execute a class) or java [-options] -jar jarfile [args...] (to execute a jar file) where options include [...]You're specifying -D after the classname, so it's being treated as a regular command line argument to be passed on to the application itself. You would see that if you printed out the values in args.
Run the app like this instead:
java -cp javaapplication8.jar -Dsysprop="some value" javaapplication8.JavaApplication8All arguments which are meant to apply to the environment rather than the application should come before the classname. If you run just java, you'll see usage help like this:
Usage: java [-options] class [args...] (to execute a class) or java [-options] -jar jarfile [args...] (to execute a jar file) where options include [...]更多推荐
发布评论