Creating JAR files
by, 11-12-2011 at 06:25 PM (498 Views)
JAR (Java Achieve Files) are compressed files that are used to bundle multiple files into a achieve file so the distribution can be managed easily. In this post, I will write about how to create JAR files.
JDK includes a JAR tool that is used for creating JAR files. Command to use is:
jar cf jar-file input-file(s)
c indicates that we want to create a JAR file.
f indicates that ouput should go to the file
jar-file is the name of the jar file that is to be created
input-file(s) includes the files and folders that are to be achieved.
Using this tool is straightforward. Lets take an example for better understanding.
We have a Java project named University. It has 3 classes namely Teachers, Students and Administrators. It further includes a folder named img that has images being used in the application. We can to pack everything in a jar file.
We can create the require jar file using the following command.
jar cf university.jar *
The asterisk indicates that every file and folder in the current folder should be packed into jar file.
Lets add another option into the command. V is for verbose which means that output is shown in on the console while the jar file is being created.
jar cfv university.jar *
As you know, jar file contains contents in compressed format, hence jar files are ideal for distribution over the network. Sometimes you donít want compression because Java takes some time in uncompressing before using the contents of the jar file. To turn off the compressiong, 0 (zero) option is used as shown in the example below:
jar cfv0 university.jar *
All the above examples will also produce default manifest file. If you donít want to produce the manifest file, you have to use option M (case sensitive).
jar cfMv university.jar *
Of course, creating JAR files using IDE (like Eclipse) is very simple.