Making an executable JAR from the command line

suggest change

To make a jar, you need one or more class files. This should have a main method if it is to be run by a double click.

For this example, we will use:

import javax.swing.*;
import java.awt.Container;

public class HelloWorld {

    public static void main(String[] args) {
        JFrame f = new JFrame("Hello, World"); 
        JLabel label = new JLabel("Hello, World");
        Container cont = f.getContentPane();
        cont.add(label);
        f.setSize(400,100); 
        f.setVisible(true);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

}

It has been named HelloWorld.java

Next, we want to compile this program.

You may use any program you want to do this. To run from the command line, see the documentation on compiling and running your first java program.

Once you have HelloWorld.class, make a new folder and call it whatever you want.

Make another file called manifest.txt and paste into it

Main-Class: HelloWorld
Class-Path: HelloWorld.jar

Put it in the same folder with HelloWorld.class

Use the command line to make your current directory (cd C:\Your\Folder\Path\Here on windows) your folder.

Use Terminal and change directory to the directory (cd /Users/user/Documents/Java/jarfolder on Mac) your folder

When that is done, type in jar -cvfm HelloWorld.jar manifest.txt HelloWorld.class and press enter. This makes a jar file (in the folder with your manifest and HelloWorld.class) using the .class files specified and named HelloWorld.jar. See the Syntax section for information about the options (like -m and -v).

After these steps, go to your directory with the manifest file and you should find HelloWorld.jar

Clicking on it should display Hello, World in a text box.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


Java deployment:
* Making an executable JAR from the command line

Table Of Contents
8 Arrays
10 Maps
11 Strings
25 JAXB
29 Enums
32 Audio
41 Scanner
63 Logging
75 Lists
78 Sets
89 JAX-WS
96 XJC
98 Process
106 Modules
114 Applets
122 JNDI
131 Java deployment
139 JavaBean
141 Literals
144 Packages
150 JMX
153 JShell
159 Sockets
167 Enum Map
175 Hashtable
177 SortedMap