Create JAR file in Java & Eclipse

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.



Tags: , ,

52 Comments

  • Ravi Shiraguppi 21 May, 2009, 11:53

    Very good I learned jar file creation in one shot, Oh in eclipse its so easy !!!

  • Viral Patel 25 May, 2009, 21:31

    @ravi: thanks for the comment. you may want to subscribe for the RSS feed or Email to get latest articles by email :)

  • Anil 15 January, 2010, 17:18

    Thanks Viral :) Most coolest and best way explanation.

  • Ozan 26 March, 2010, 18:22

    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

    • Andrew 22 February, 2014, 18:39

      Ignore this error

  • charles 17 April, 2010, 14:57

    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

    • Kale 13 April, 2011, 20:26

      Hey Charles,

      I too am having your issue (now about a year later) and am wondering if you ever got it resolved. I know it has been some time ago i’m just hoping that (as I hope to) you have been able to retain what it was that you did to solve the issue.

      I am currently getting the same/similar error:

      Exception in thread “main” java.lang.NoClassDefFoundError: javax/Mail/Address
      Caused by: java.lang.ClassNotFoundException: javax.mail.Address

      The application runs just fine within JGrasp (java compiler) as well as command line ‘javac’ compilation. As soon as I try to compile it into a *.jar file and run it using: “java -jar filename.jar” it fails with the above error.

      Thanks in advance,

      Kale
      (mailto:michelsk@live.com)

  • nikhil 21 May, 2010, 17:14

    Hello friend.
    Its really very good tutorial for me. Its very easy.
    Thanks.

  • Vikas 28 June, 2010, 7:12

    Thanks Viral for sharing this information.

  • suddheer 7 September, 2010, 3:43

    Thanx for teaching “creating a jar file in eclipse so easily…..”

  • Archana 11 September, 2010, 16:33

    Hi

    I need to know how to convert a WSDL file to XSD using Eclipse. If yes can you give me the steps to convert the same. Thanks in advance

  • Jimmy 11 October, 2010, 20:09

    really nice and I learned jar file creation in one shot, as Java beginner I am not very familiar with Jar, thanks

  • BINEET PANDEY 15 November, 2010, 21:22

    i am very happy after reading this tutorial ;
    thank you.
    BINEET PANDEY ,MEGASOFT PATNA,BORING ROAD.

  • pro pro grammer 2 December, 2010, 22:04

    i made a game with images but i dosent include the images whwn i export it someone please help

    it only exports the source not the resorces

  • devendra 20 January, 2011, 22:21

    oh thanks i found itrs so much easy via this stuff

  • Abhishek 27 February, 2011, 12:41

    thanks

  • Shoshi 1 March, 2011, 19:54

    I want to work with XCAML from eclipse. Can anyone help me please regarding this matter? How can I connect XACML with Java?

  • Badd 18 May, 2011, 14:22

    I solved the problem, for the external jar files, i had the same error with a database mysql connector jar file , so i thought how the thing should work, well my guess was that when eclipse is creating a jar file, its like a rar file, it doesnt alows the conntent to be accesed by the new jar file, but when we add external jars, Eclipse can acsess it, had just had 1 jar external file, so i cliksed on my project, selected to “import” chose “Archive file” and choseed the external jar file in my case “mysqlconnector.jar” file, so eclipse unziped it and added all classfiles from the mysqlconnector.jar” to my project adding a series of file structure in it, also added the mainfest fail for the “mysqlconnector.jar”.

    When made the jar file, it all worked! So the problem is when a jar file try to acses another jar file witch is in the first one, it cant access the class files, so it cant find the class what you need to made it done.

    Simply extract the external jar files to your project and then create a jar file of your projeck, mine did work, yours should work too!

    Btw, i dint made a jar file like in this turturial, i made some different options, like “Export all output folders”, “Export java sorce files and resources” ” export refactorings” , you need to save the description and create a new mainfest file for this jar fail, in myu case that was it!

    Good luck!

    • sayan 4 July, 2014, 1:45

      THANK YOU !!! I was stuck for hours !!

  • Badd 18 May, 2011, 14:24

    sorry for grammar, wrote it wery fast and my english is not so good :D i hope you get the point

  • Emmanuel 1 June, 2011, 23:46

    Thanks for the material; it has been helpful to me

  • Neelabh 2 September, 2011, 16:00

    I am working on a simple mobile game in java.
    finally, i am done with the running code in eclipse, but unable to deploy it in an executable jar file, since my game contains two files first.java(extends MIDlet) and second.java(GameCanvas) and none of them having “main” in it, all are inherited abstract methods…..
    i have tried various ways to create the same but each time stuck at manifest entries where main class is to be defined.
    if i try to do this using eclipse export wizard, i am not able to find any launch configuration, the dropdown list comes empty…

    somebody help!

    • Zack 23 May, 2012, 6:11

      OOH! I know! You need to call the methods outside of the classes, or make a private class and make that call the methods of it inside of that one. Just make sure that you call that method in another class to make it run… Hope this works!

  • Ranj 24 September, 2011, 5:10

    I need to know how to create a JAR file from.java which uses command line arguments?

    I have an image processing java program which takes the path to the image as command line argument. Can you please tell me how to convert this to an executable in eclipse?

    thanks in advance

  • shad 15 October, 2011, 13:51

    Oh! very nice
    I prepared calculator with java eclipse and thought to prepare an executable file
    and I gone through steps
    Its really working and I prepared My java Calculator as execulatble file.

    thanks alot

  • sathish 8 November, 2011, 21:08

    just click on project or src ,right click on any one of it select export and in that select executable jar and save it ………

  • abc 8 November, 2011, 21:17

    while creating jar file i got this error..jar is not recognised as an internal or external command.plz help

    • Abc 11 December, 2011, 10:10

      instal java sdk and set path variable for jdk

  • Trinath 14 December, 2011, 10:45

    hii guys,

    i need to know how to configure java and mysql to eclipse..
    or
    i want to know weather by default eclipse is connected to java and mysql

  • sumeet 15 December, 2011, 18:32

    Can any one tell me how can we update an existing jar file, means i want to add single file into existing jar file, and then want to create new jar from that.

  • Muhammad helal 3 January, 2012, 0:03

    I want to make something strange 4 my mobile phone which can make it more comfortable 4 me.that’s why i need your help as i’m as an apprentice.

  • Mirta - Recipes Cookbook 20 February, 2012, 7:11

    Please help me – I created the jar perfectly, no errors, no warnings and I located the jar in my file system; but whenever I tried to execute it, no matter if click or console it give me the error:

    C:\www>java giaconda.jar
    Exception in thread “main” java.lang.NoClassDefFoundError: giaconda/jar
    Caused by: java.lang.ClassNotFoundException: giaconda.jar
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    Could not find the main class: giaconda.jar. Program will exit.

    I know I need to insert the class in the classpath, I run the command in order to do it but nothing happens, I dont know if I am doing it fine. I am getting a bit desperate here :’( A little help will be appreciated.

  • Ramesh 29 February, 2012, 16:16

    while converting a wsdl file to java class in eclipse after specifying the out path when i am clicking in the finish button ” java.lang.reflect.InvocationTargetException” is produced .I am using windowsXp.

  • soo 21 March, 2012, 13:18

    hi
    while i useing this jarfile (created by eclipse) idont know what’s the name of import?

  • munaver 9 April, 2012, 8:24

    hi i’m creating jar file using eclipse and also specified the entry point main class when i’m executing that jar file it shows error message stated as “could not find main class.program will exit ” please help me to create jar file in eclipse ……….thanks in advance..

    • Zack 23 May, 2012, 6:06

      Probably the MANIFEST.MF.

  • bideshi 24 April, 2012, 11:00

    Can u help me out i am in stuck in code. i want to modify css file from reading a value from xml file than write that modified file in to hard drive. modified like font-size, font-color? Thanks in advance and appreciate your help

  • amir 30 April, 2012, 14:24

    Thanks, it was very helpful

  • Nithin 16 May, 2012, 11:54

    that was very clear and understanble thanks for the matrial viral patel

  • Zack 23 May, 2012, 6:05

    HAHAHA! That was SO easy! Now I can continue to learn to make shtuff…

  • Pavan Gupta 7 June, 2012, 22:35

    Jar command is not working for me

  • Angad 20 June, 2012, 14:30

    Hi the blog is good. But the major drawback here is it takes a very long time to load. Go for more B/W or reduce ads and images on a page

  • sannidhi 24 July, 2012, 14:59

    Thanks much for sharing!

  • zaheer 6 August, 2012, 3:58

    Thanks Bro done…….

  • Anoosh 6 September, 2012, 12:26

    Thanks !!!

  • bial 11 November, 2012, 13:16

    Dear,
    i cant generate the Jar file… it gives warning. but the application is working fine.
    actually i imported the project from JBuilder and i wanted to work on eclipse.
    thanks to help me in this.. why it is not generating,
    Regards

  • Ryan 13 December, 2012, 19:12

    Thanks ,

    Where do u work Viral?

  • Rahul 17 March, 2013, 12:14

    Finaly helped me.. I got it
    Good work sir and helpes many.. Like us.

  • Joseph 22 March, 2013, 19:49

    Your steps to create a jar file within Eclipse seem to do just that, but it appears when i try to run this jar file I created using your steps I get the error: “Failed to load Main-Class manifest attribute from FileConverter.jar”.
    I’m not sure if this is the problem, but…
    Does Eclipse create the Manifest file, or do I need to create the manifest file in eclipse before creating the jar?

    Regards,
    Joe

  • Santhanu 19 September, 2013, 17:27

    Hi,
    I want to know how to run the created jar in eclipse. Is there any way to run the Jar in eclipse?

  • mona 14 April, 2014, 19:27

    I have followed the steps of creation of jar file in eclipse,but an error is being displayed “Review the licenses associated with the libraries.This operation does not copy signature files from original libraries to the jar file.”I have used Open CV library.Please help me in removing this error.

  • kamlesh 14 May, 2014, 18:25

    How could i convert .jar into .exe?

Leave a Reply

Your email address will not be published. Required fields are marked *

Note

To post source code in comment, use [code language] [/code] tag, for example:

  • [code java] Java source code here [/code]
  • [code html] HTML here [/code]

Current day month ye@r *