How to create java jar files?

Posted by Joys of Programming on in Java

You would have seen or heard about the java jar file. Java jar file corresponds to the archived form of java classes (normally with the extension .jar).


jar = java archive

It is not mandatory that a jar file must contain only the class (*.class) files. It can contain directories, images, class files or other media content. For our further discussion on the topic, we will consider the example given here. We will first create an archive of the class files.


$ javac tutorials/sutils/StringUtils.java #compile StringUtils.java. StringUtils.class is available in tutorials/sutils

$ javac tutorials/wmessage/WelcomeMessage.java #compile WelcomeMessage.java. WelcomeMessage.class is available in tutorials/wmessage/

$ jar cf tutorials.jar tutorials/sutils/*.class tutorials/wmessage/*.class

You can view the contents of the java jar file. You will see that tutorials.jar will only contain the above two classes.

So the option cf with the command jar creates an archive.

As said above, it is not necessary that jar must contain only class files. It can contain directrories, images, or any other media or (configuration) filesĀ  used by the java classes.


$ mkdir tutorials/media
$ jar cf tutorialswithmedia.jar tutorials/sutils/*.class tutorials/wmessage/*.class tutorials/media

Check the contents of tutorialwithmedia.jar

Tags: , , , ,

Comments:

Leave a Reply

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

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Copyright © 2009-2012 Joys of Programming All rights reserved.
Desk Mess Mirrored v1.8.1 theme from BuyNowShop.com.