Java command line arguments in --key=value format

Try the -D option, allows to set key=value pair:

run command; note there are no space between -Dkey

  • java -Dday=Friday -Dmonth=Jan MainClass

In your code:

String day = System.getProperty("day");
String month = System.getProperty("month");

As of now, there is no way to convert --key=value to Map instead of String.

public static void main(String[] args) {

    HashMap<String, String> params = convertToKeyValuePair(args);

    params.forEach((k, v) -> System.out.println("Key : " + k + " Value : " + v));

}

private static HashMap<String, String> convertToKeyValuePair(String[] args) {

    HashMap<String, String> params = new HashMap<>();

    for (String arg: args) {

        String[] splitFromEqual = arg.split("=");

        String key = splitFromEqual[0].substring(2);
        String value = splitFromEqual[1];

        params.put(key, value);

    }

    return params;
}

Hope this helps!