Wednesday, February 15, 2017

How to run a jar file in command prompt?

First Solution

java -jar <myJar>.jar

Second Solution(Jon Skeet): 

You need to specify a Main-Class in the jar file manifest.
Oracle's tutorial contains a complete demonstration, but here's another one from scratch. You need two files:
Test.java:
public class Test
{
    public static void main(String[] args)
    {
        System.out.println("Hello world");
    }
}
manifest.mf:
Manifest-version: 1.0
Main-Class: Test
Note that the text file must end with a new line or carriage return. The last line will not be parsed properly if it does not end with a new line or carriage return.
Then run:
javac Test.java
jar cfm test.jar manifest.mf Test.class
java -jar test.jar
Output:
Hello world

Resource Link:

  1. http://stackoverflow.com/questions/5774970/run-jar-file-in-command-prompt
  2. http://stackoverflow.com/questions/1238145/how-to-run-a-jar-file

No comments:

Post a Comment