How to make an executable JAR file?

后端 未结 5 1318
孤独总比滥情好
孤独总比滥情好 2020-11-21 23:41

I have a program which consists of two simple Java Swing files.

How do I make an executable JAR file for my program?

5条回答
  •  旧巷少年郎
    2020-11-21 23:57

    A jar file is simply a file containing a collection of java files. To make a jar file executable, you need to specify where the main Class is in the jar file. Example code would be as follows.

    public class JarExample {
    
        public static void main(String[] args) {
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    // your logic here
                }
            });
        }
    }
    

    Compile your classes. To make a jar, you also need to create a Manifest File (MANIFEST.MF). For example,

    Manifest-Version: 1.0
    Main-Class: JarExample
    

    Place the compiled output class files (JarExample.class,JarExample$1.class) and the manifest file in the same folder. In the command prompt, go to the folder where your files placed, and create the jar using jar command. For example (if you name your manifest file as jexample.mf)

    jar cfm jarexample.jar jexample.mf *.class
    

    It will create executable jarexample.jar.

提交回复
热议问题