Creating JAR files

24 December 2007

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.

del.icio.us:Creating JAR files  digg:Creating JAR files  spurl:Creating JAR files  wists:Creating JAR files  simpy:Creating JAR files  newsvine:Creating JAR files  blinklist:Creating JAR files  furl:Creating JAR files  reddit:Creating JAR files  fark:Creating JAR files  blogmarks:Creating JAR files  Y!:Creating JAR files  smarking:Creating JAR files  magnolia:Creating JAR files  segnalo:Creating JAR files  gifttagging:Creating JAR files

Top Of Page | Trackback

If you found this page useful, consider linking to it. Simply copy and paste the code below into your web site.

It will look like this: Creating JAR files

One Response to “Creating JAR files”

  1. nelu Says:

    Making a jar file from SWT project, in Eclipse:

    1. Create one manifest file in project with following contents:
    Manifest-Version: 1.0
    Class-path: swt.jar
    Main-class: package.MainClass

    2. Export jar file with this manifest file
    a) Right-click on project
    b) Selection Export
    c) Select Java and Jar file
    d) Select the project, classpath and file project
    e) In jar file textbox enter the name for jar file and select options: “Exports generated class files and resources” and “Compress the content of the jar file”
    f) Press Next button
    g) Press Next button
    h) Select “Use existing manifest from workspace” and select the manifest file which you has created
    i) Press Finish button

    3. Put togheter the jar file and swt.jar

    4. Run jar file

    If you want, please send me an email for send you example swt project (source and jar)
    nelucristian2005@yahoo.com

Leave a Reply