Monday, February 21, 2011

Passing Parameters to Jar file

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

Thanks,

Krisp

From stackoverflow
  • 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.

  • To pass arguments to the jar, java -jar myjar.jar one two

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

    String one = args[0];
    String two = args[1];

  • is it possible to do this without the java -jar bit at the start?

    I can run jar files that don't have parameters just by typing in the name of the jar file at the command prompt. But it doesn't work when parameters are added.

0 comments:

Post a Comment