Create JAR file in Java & Eclipse

[ad name=”AD_INBETWEEN_POST”] 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
Code language: HTML, XML (xml)

View contents of a JAR file

jar tf JAR_FILE_NAME e.g. jar tf MyApp1.jar
Code language: HTML, XML (xml)

View contents with detail of a JAR file

jar tvf JAR_FILE_NAME e.g. jar tvf MyApp1.jar
Code language: HTML, XML (xml)
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
Code language: HTML, XML (xml)

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
Code language: HTML, XML (xml)

Update a JAR file

jar uf JAR_FILE_NAME FILE_NAMES_FROM_JAR_FILE e.g. jar uf MyApp1.jar Test1.class
Code language: HTML, XML (xml)

Executing a JAR file

java -jar JAR_FILE_NAME e.g. java -jar MyApp.jar
Code language: HTML, XML (xml)

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
Code language: HTML, XML (xml)
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
Code language: HTML, XML (xml)

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.

You may also like...

53 Comments

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

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

  3. Anil says:

    Thanks Viral :) Most coolest and best way explanation.

  4. Ozan says:

    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 says:

      Ignore this error

  5. charles says:

    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 says:

      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:[email protected])

  6. nikhil says:

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

  7. Vikas says:

    Thanks Viral for sharing this information.

  8. suddheer says:

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

  9. Archana says:

    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

  10. Jimmy says:

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

  11. BINEET PANDEY says:

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

  12. pro pro grammer says:

    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

  13. devendra says:

    oh thanks i found itrs so much easy via this stuff

  14. Abhishek says:

    thanks

  15. Shoshi says:

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

  16. Badd says:

    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 says:

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

  17. Badd says:

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

  18. Emmanuel says:

    Thanks for the material; it has been helpful to me

  19. Neelabh says:

    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 says:

      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!

  20. Ranj says:

    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

  21. shad says:

    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

  22. sathish says:

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

  23. abc says:

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

    • Abc says:

      instal java sdk and set path variable for jdk

  24. Trinath says:

    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

  25. sumeet says:

    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.

  26. Muhammad helal says:

    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.

  27. 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.

  28. Ramesh says:

    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.

  29. soo says:

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

  30. munaver says:

    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 says:

      Probably the MANIFEST.MF.

  31. bideshi says:

    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

  32. amir says:

    Thanks, it was very helpful

  33. Nithin says:

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

  34. Zack says:

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

  35. Pavan Gupta says:

    Jar command is not working for me

  36. Angad says:

    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

  37. sannidhi says:

    Thanks much for sharing!

  38. zaheer says:

    Thanks Bro done…….

  39. Anoosh says:

    Thanks !!!

  40. bial says:

    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

  41. Ryan says:

    Thanks ,

    Where do u work Viral?

  42. Rahul says:

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

  43. Joseph says:

    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

  44. Santhanu says:

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

  45. mona says:

    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.

  46. kamlesh says:

    How could i convert .jar into .exe?

  47. alvin kelliher says:

    Useful suggestions ! I learned a lot from the analysis ! Does someone know if my business might locate a blank SSA-7050-F4 copy to edit ?

Leave a Reply

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