Java: Passing combination of named and unnamed parameters to executable jar/main method
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.
![sun_dare](https://i.stack.imgur.com/c639T.jpg?s=256&g=1)
Comments
-
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:
- I can pass, both, named and unnamed arguments;
- I can fetch/handle these arguments with their names;
- 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 almost 10 yearsThank you for you reply. :) what if the value has "=" in that?
-
Mac70 almost 10 yearsIf 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 almost 10 yearswell, 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 almost 10 yearsI had a look at Commons CLI and args4j. Both are good but I am going with args4j. I felt it easier
-
Archangel33 almost 10 yearsargs4j 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 almost 8 yearsJSAP is the way to go... martiansoftware.com/jsap May seem overkill for simple command line applications but it quickly pays off.