How do I pass parameters to a jar file at the time of execution?
JarJar Problem Overview
How do I pass parameters to a JAR file at the time of execution?
Jar Solutions
Solution 1 - Jar
To pass arguments to the jar:
java -jar myjar.jar one two
You can access them in the main() method of "Main-Class" (mentioned in the manifest.mf
file of a JAR).
String one = args[0];
String two = args[1];
Solution 2 - Jar
> java [ options ] -jar file.jar [ > argument ... ]
and
> ... Non-option arguments after the > class name or JAR file name are passed > to the main function...
Maybe you have to put the arguments in single quotes.
Solution 3 - Jar
You can do it with something like this, so if no arguments are specified it will continue anyway:
public static void main(String[] args) {
try {
String one = args[0];
String two = args[1];
}
catch (ArrayIndexOutOfBoundsException e){
System.out.println("ArrayIndexOutOfBoundsException caught");
}
finally {
}
}
And then launch the application:
java -jar myapp.jar arg1 arg2
Solution 4 - Jar
java [ options ] -jar file.jar [ argument ... ]
if you need to pass the log4j properties file use the below option
-Dlog4j.configurationFile=directory/file.xml
java -Dlog4j.configurationFile=directory/file.xml -jar <JAR FILE> [arguments ...]
Solution 5 - Jar
Incase arguments have spaces in it, you can pass like shown below.
java -jar myjar.jar 'first argument' 'second argument'