This post shows how to Export a Java Project in Eclipse Neon to a JAR file.
If the JAR
(Java Archive) file contains classes with main methods
that have a signature public static void main(String[] args)
, the Java application can
then be executed with the java command.
Export a Java Project in Eclipse Neon
- Using the Java Perspective, right-click on the Java Project node in the Package Explorer to open its context menu.

- Select Export and select the JAR file export wizard under the Java node and click Next.

- On the JAR File Specification form, set the export destination for the JAR file and click Finish.

- The JAR file, here named
JARExportTest.jar
is now available in the specified directory.

Run the JAR File with the Java Command
- In this example, the JAR file contains one Java class with one main method:
package com.pgx.java;
public class HelloWorld {
public static void main(String[] args) {
// TODO Auto-generated method stub
String[] words = new String[] {"Hello ", "World", "!"};
for (String s : words) {
System.out.print(s);
}
}
}
- Open a terminal on Mac OS or open the command prompt on Windows and go to the JAR file location.
MyMac:Java Admin$ pwd
/Users/Admin/Desktop/Java
MyMac:Java Admin$ ls -lha *Export*.jar
-rw-r--r-- 1 Admin 1368638373 1.7K Jan 13 16:18 JARExportTest.jar
MyMac:Java Admin$
- The
HelloWorld
main class can be executed with the following command:
java -cp JARExportTest.jar com.pgx.java.HelloWorld
- The output is shown in the console:
MyMac:Java Admin$ java -cp JARExportTest.jar com.pgx.java.HelloWorld
Hello World!
Create a JAR Manifest File in Eclipse Neon
- Above, the class
HelloWorld
containing the main method to execute is explicitly given in the java command. - Alternatively, a Manifest file can be used to specify an application’s entry point.
- When exporting the JAR file, select Next on the JAR File Specification form.

- Click Next again on the JAR Packaging Options form.

- The next form will show the JAR Manifest Specification options.

- Check Save the manifest in the workspace and use the Browse button to set a location.
- Select the Main class, here it is
HelloWorld
using the Browse button. - Finally, click in Finish to export the Java project to a JAR file that also contains a manifest file.
- The manifest file is automatically generated and included in the Java project in Eclipse Neon.

-
The JAR file can now be executed without specifying a main method. The java command will pick
the one provided in the manifest file. Make sure to use the option
-jar
instead of-cp
.
java -jar JARExportTest.jar
MyMac:Java Admin$ java -jar JARExportTest.jar
Hello World!