[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
Code language: HTML, XML (xml)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
Code language: HTML, XML (xml)jar tf JAR_FILE_NAME e.g. jar tf MyApp1.jar
View contents with detail of a JAR file
Note that we have used v (verbose) option to see the detail of JAR.Code language: HTML, XML (xml)jar tvf JAR_FILE_NAME e.g. jar tvf MyApp1.jar
Extract content of JAR file
Code language: HTML, XML (xml)jar xf JAR_FILE_NAME e.g. jar xf MyApp1.jar
Extract specific file from JAR file
Code language: HTML, XML (xml)jar xf JAR_FILE_NAME FILE_NAME(S)_FROM_JAR_FILE e.g. jar xf MyApp1.jar Test1.class
Update a JAR file
Code language: HTML, XML (xml)jar uf JAR_FILE_NAME FILE_NAMES_FROM_JAR_FILE e.g. jar uf MyApp1.jar Test1.class
Executing a JAR file
Code language: HTML, XML (xml)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.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.Code language: HTML, XML (xml)Manifest-Version: 1.0 Main-Class: MyMainClass
Code language: HTML, XML (xml)jar cvfm MyApp.jar MANIFEST.MF FILE_NAMES_OR_DIRECTORY_NAME
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
Ignore this error
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
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])
Hello friend.
Its really very good tutorial for me. Its very easy.
Thanks.
Thanks Viral for sharing this information.
Thanx for teaching “creating a jar file in eclipse so easily…..”
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
really nice and I learned jar file creation in one shot, as Java beginner I am not very familiar with Jar, thanks
i am very happy after reading this tutorial ;
thank you.
BINEET PANDEY ,MEGASOFT PATNA,BORING ROAD.
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
oh thanks i found itrs so much easy via this stuff
thanks
I want to work with XCAML from eclipse. Can anyone help me please regarding this matter? How can I connect XACML with Java?
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!
THANK YOU !!! I was stuck for hours !!
sorry for grammar, wrote it wery fast and my english is not so good :D i hope you get the point
Thanks for the material; it has been helpful to me
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!
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!
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
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
just click on project or src ,right click on any one of it select export and in that select executable jar and save it ………
while creating jar file i got this error..jar is not recognised as an internal or external command.plz help
instal java sdk and set path variable for jdk
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
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.
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.
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.
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.
hi
while i useing this jarfile (created by eclipse) idont know what’s the name of import?
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..
Probably the MANIFEST.MF.
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
Thanks, it was very helpful
that was very clear and understanble thanks for the matrial viral patel
HAHAHA! That was SO easy! Now I can continue to learn to make shtuff…
Jar command is not working for me
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
Thanks much for sharing!
Thanks Bro done…….
Thanks !!!
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
Thanks ,
Where do u work Viral?
Finaly helped me.. I got it
Good work sir and helpes many.. Like us.
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
Hi,
I want to know how to run the created jar in eclipse. Is there any way to run the Jar in eclipse?
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.
How could i convert .jar into .exe?
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 ?