Search code examples
javaspringargs

Extracting properties using SimpleCommandLinePropertySource does not work in Java


Trying to extract command line arguments using SimpleCommandLinePropertySource(arg)

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

When i print argument, this is printed:

SimpleCommandLinePropertySource {name='commandLineArgs'}

This is how i call the main class from eclipse.

enter image description here


Solution

  • The javadoc for SimpleCommandLinePropertySource states

    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.

    You're providing your property argument as

     -key=value
    

    which is not the syntax SimpleCommandLinePropertySource is expecting and it therefore ignores it.

    Provide the argument in the format specified, then you can retrieve the parsed properties normally, ie. through getProperty.