Java: Passing combination of named and unnamed parameters to executable jar/main method

12,948

Solution 1

Apache Commons CLI is what I use to parse java command line arguments. Examples can be found here and can be used to do any of the following option formats:

  • POSIX like options (ie. tar -zxvf foo.tar.gz)
  • GNU like long options (ie. du --human-readable --max-depth=1)
  • Java like properties (ie. java -Djava.awt.headless=true -Djava.net.useSystemProxies=true Foo)
  • Short options with value attached (ie. gcc -O2 foo.c)
  • long options with single hyphen (ie. ant -projecthelp)

Solution 2

Based on @Mac70's answer and a few additions,

private static Map<String, String> map;
private static void makeMap(String[] args) {
    map = new HashMap<>();
    for (String arg : args) {
        if (arg.contains("=")) {
            //works only if the key doesn't have any '='
            map.put(arg.substring(0, arg.indexOf('=')),
                    arg.substring(arg.indexOf('=') + 1));
        }
    }
}

public static void main(String[] args) {
    makeMap(args);

    //.. 
    String param3 = map.get("param3name");
    String param1 = map.get("param1name");
}

If you need anything extensive, you need to look at @Archangel33's answer.

Solution 3

As long as params and names don't contain spaces - you can get all of them, split at "=" key and add key/value pairs to the HashMap. Later you can just get any value you want using key.

Edit: If you want to not add some elements to the map, then you can ignore them if these elements don't contain "=" key.

Share:
12,948
sun_dare
Author by

sun_dare

Developer by profession.

Updated on June 12, 2022

Comments

  • sun_dare
    sun_dare about 2 years

    I want to pass, both, named and unnamed arguments to the main method.

    Currently I am passing arguments as:

     java -jar myfile.jar param1 param2
    

    and handling them as:

    public static void main(String[] args) throws IOException {
        String param1 = args[0];
        String param2 = args[1];
    }
    

    However, I want to pass the arguments in a more dynamic way - namely, so that:

    1. I can pass, both, named and unnamed arguments;
    2. I can fetch/handle these arguments with their names;
    3. I will not be required to pass them in the same order, every time I execute the main method.

    Passing in a way Something like this:

       java -jar myJar param3name=param3 param2name=param2 param1name=param1 param5 param6
    

    and handling in a way something like this:

    public static void main(String[] args) throws IOException {
        //something like
        String param3 = getvaluemethod("param3name");
        String param1 = getvaluemethod("param1name");
         .....
        String param5 = args[n]
        String param6 = args[n+1]
         .....
    }
    

    I am fine to work with some external libraries which would make my work easier.

    I have already seen this and it is not comprehensive.

    Any input on how to accomplish the task?

  • sun_dare
    sun_dare almost 10 years
    Thank you for you reply. :) what if the value has "=" in that?
  • Mac70
    Mac70 almost 10 years
    If you don't want to use any libraries, you can pass parameters in this format: "key"="value" - this way instead of = key you can search for "=" keys (but the whole thing needs more parsing). You can use other key you will not use instead of =, too.
  • Phani Rahul
    Phani Rahul almost 10 years
    well, there is no point in using = as the key if it is allowed in the value part. you can either escape = or use "" like @Mac70 suggested.
  • sun_dare
    sun_dare almost 10 years
    I had a look at Commons CLI and args4j. Both are good but I am going with args4j. I felt it easier
  • Archangel33
    Archangel33 almost 10 years
    args4j is also a good choice. I used CLI because I've always done it that way since I was told to use it in a project a long long time ago by the senior devs. Never really looked at args4j but may now take a look to see what else may be out there.
  • Hugo Zaragoza
    Hugo Zaragoza almost 8 years
    JSAP is the way to go... martiansoftware.com/jsap May seem overkill for simple command line applications but it quickly pays off.