即使在命令行中定义,系统属性也为空(system property is null even if it is defined in command line)

编程入门 行业动态 更新时间:2024-10-28 22:27:52
即使在命令行中定义,系统属性也为空(system property is null even if it is defined in command line)

我正在调试大型系统中的问题。 我能够在相当基本的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.JavaApplication8

All 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 [...]

更多推荐

本文发布于:2023-07-05 10:02:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1036121.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:命令行   为空   属性   定义   系统

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!