25

How can I run a jar file in command prompt and pass arguments to it.

ie: "test.jar -get" would be entered in command prompt/terminal

Sachin Karjatkar
  • 313
  • 3
  • 13
Mario
  • 821
  • 3
  • 9
  • 13
  • You want run above command from eclipse command window? You question is little ambiguious. – kosa Jan 07 '12 at 15:15

1 Answers1

77

For the question

How can i run a jar file in command prompt but with arguments

.

To pass arguments to the jar file at the time of execution

java -jar myjar.jar arg1 arg2

In the main() method of "Main-Class" [mentioned in the manifest.mft file]of your JAR file. you can retrieve them like this:

String arg1 = args[0];
String arg2 = args[1];
Rajesh Pantula
  • 10,061
  • 9
  • 43
  • 52
  • 4
    thank you so much, its been bothering me ***forever*** – Mario Jan 07 '12 at 15:18
  • "no main manifest attribute, in test.jar" – Mario Jan 07 '12 at 15:21
  • then it is not a standalone jar file, it does not have any class with main method. You need to createe a class and use that jar file as library. you need to import it in your class. – Rajesh Pantula Jan 07 '12 at 15:23
  • set the jar in the CLASSPATH variable and import it in your class. call whatever API you want to call. cant help you more than this, until and unless i know what you are trying to do, and what your jar file is – Rajesh Pantula Jan 07 '12 at 15:31
  • How could I pass arguments to the jar file, when using -cp 'a.jar:b.jar'? (-jar is ignored when -cp is set) – Kiril May 01 '14 at 11:59
  • @Kiril when -jar is used, -cp is ignored. the quickest way(if you do not have too many jar files you want to use with -cp), is to not use the -jar, but use -cp instead with the option value of -jar also appended to the end of option values of -cp. Then specify the entry point class. So it would look like: java -cp a.jar:b.jar:YOURJARFILETORUN.jar MAINCLASS arg1 arg2 .. where MAINCLASS is the fully quailified name of the class in which the 'main' method is defined in YOURJARFILETORUN.jar Hope this makes sense – Brian Jul 30 '14 at 14:55
  • what in case of a shell script? Is there any change? – Dhruv Singhal Sep 05 '18 at 15:23