使用 SimpleCommandLinePropertySource 提取属性在 Java 中不起作用

Extracting properties using SimpleCommandLinePropertySource does not work in Java

正在尝试使用 SimpleCommandLinePropertySource(arg) 提取命令行参数

public static void main(String [] args)
{
    SimpleCommandLinePropertySource argument = new SimpleCommandLinePropertySource(arg);
}

当我打印参数时,打印的是:

SimpleCommandLinePropertySource {name='commandLineArgs'}

这就是我从 eclipse 中调用主要 class 的方式。

javadoc for SimpleCommandLinePropertySource 状态

Working with option arguments

Option arguments must adhere to the exact syntax:

--optName[=optValue]

That is, options must be prefixed with "--" and may or may not specify a value. If a value is specified, the name and value must be separated without spaces by an equals sign ("="). The value may optionally be an empty string.

您提供的 属性 参数为

 -key=value

这不是 SimpleCommandLinePropertySource 所期望的语法,因此它会忽略它。

以指定的格式提供参数,然后您可以正常检索解析的属性,即。通过 getProperty.