How to make an executable Jar file in Java
Jar file
A jar file is simply a file containing a collection of java class 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 JavaClassName{ 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
).
Manifest-Version: 1.0
Main-Class: JavaClassName
Place the compiled output class files (JavaClassName
.class,JavaClassName
$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 manifest.mf).
jar helloworld JavaClassName.jar manifest.mf *.class