How do I pass parameters to a jar file at the time of execution?

Jar

Jar 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

The JAVA Documentation says:

> 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'

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionkrispView Question on Stackoverflow
Solution 1 - JarRejiView Answer on Stackoverflow
Solution 2 - JarXn0vv3rView Answer on Stackoverflow
Solution 3 - Jaruser1420526View Answer on Stackoverflow
Solution 4 - JarBachan JosephView Answer on Stackoverflow
Solution 5 - JarSuperNovaView Answer on Stackoverflow