Create JAR file in Java & Eclipse
- By Viral Patel on December 15, 2008
- How-To, Java, Tutorial
Let us see how to create a JAR file using Java’s jar command as well as using Eclipse IDE. The JAR file format is based on the popular ZIP file format. Usually these file are used for archiving and distribution the files and implementing various libraries, components and plug-ins in java applications. Compiler and JVMs (Java Virtual Machine) can understand and implement these formats for java application.
JAR file using Java commands
Following are few commands that can be used to create/view/modify/execute a JAR file using Java command line utilities and JVM.
Create a JAR file
jar cf JAR_FILE_NAME FILE_NAMES_OR_DIRECTORY_NAME e.g. jar cf MyApp1.jar C:\JavaProject\MyApp
View contents of a JAR file
jar tf JAR_FILE_NAME e.g. jar tf MyApp1.jar
View contents with detail of a JAR file
jar tvf JAR_FILE_NAME e.g. jar tvf MyApp1.jar
Note that we have used v (verbose) option to see the detail of JAR.
Extract content of JAR file
jar xf JAR_FILE_NAME e.g. jar xf MyApp1.jar
Extract specific file from JAR file
jar xf JAR_FILE_NAME FILE_NAME(S)_FROM_JAR_FILE e.g. jar xf MyApp1.jar Test1.class
Update a JAR file
jar uf JAR_FILE_NAME FILE_NAMES_FROM_JAR_FILE e.g. jar uf MyApp1.jar Test1.class
Executing a JAR file
java -jar JAR_FILE_NAME e.g. java -jar MyApp.jar
Create an executable JAR file
In order to create an executable JAR, one of the classes that we include in our JAR must be a main class.
Create a text file called MANIFEST.MF using any text editor and copy following content in it.
Manifest-Version: 1.0 Main-Class: MyMainClass
Where MyMainClass is the name of the class that contents main method. Also note that you have to specify fully qualified class name here.
Use following command to create an executable JAR file.
jar cvfm MyApp.jar MANIFEST.MF FILE_NAMES_OR_DIRECTORY_NAME
JAR file using Eclipse IDE
Creating JAR file using Eclipse IDE is pretty much easy. Follow the simple steps.
Right click on your project, which you want to create a JAR file of. And select Export from the context menu.

Select JAR file from Java folder and click Next.

Provide the Destination path and click on Finish to create the JAR.
Get our Articles via Email. Enter your email address.



Very good I learned jar file creation in one shot, Oh in eclipse its so easy !!!
@ravi: thanks for the comment. you may want to subscribe for the RSS feed or Email to get latest articles by email :)
Thanks Viral :) Most coolest and best way explanation.
Hi guys,
When i tried with the same approach:
I receive “Exported with compile warnings” error.Anyone face with this issue?
Also,i tried with once more “java -jar MapFormatter.jar file” in the cmd.But still got error
Waiting your responses
Hi,
In a program i wrote I’m using some non standard java libraries (to send an e-mail), so I had to add external jars to my build path under eclipse. When I run the program under eclipse it does run properly. However when I create a jar file for my project and run my program (using java -jar my_program.jar) it throws the following Exception:
Exception in thread “main” java.lang.NoClassDefFoundError: javax/mail/Address
at arp.Main.main(Main.java:167)
Caused by: java.lang.ClassNotFoundException: javax.mail.Address
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:323)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:268)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:336)
It looks like when I exported my project as a jar file it did not include the classes contained in the external jars that I had to my build path…
Would anyone know how to solve that problem?
Thanks
Hello friend.
Its really very good tutorial for me. Its very easy.
Thanks.
Thanks Viral for sharing this information.