How to run a JAR file

前端 未结 11 2153
长发绾君心
长发绾君心 2020-11-22 04:18

I created a JAR file like this:

jar cf Predit.jar *.*

I ran this JAR file by double clicking on it (it didn\'t work). So I ran it from the

11条回答
  •  长情又很酷
    2020-11-22 04:43

    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
    

提交回复
热议问题