Saturday, 30 May 2015

How to create a JAR file in Java

The Java™ Archive (JAR) file format enables you to bundle multiple files into a single archive file. Typically a JAR file contains the class files and auxiliary resources associated with applets and applications.

Usage: jar {ctxui}[vfm0Me] [jar-file] [manifest-file] [entry-point] [-C dir] files ...

Options:
    -c  create new archive
    -t  list table of contents for archive
    -x  extract named (or all) files from archive
    -u  update existing archive
    -v  generate verbose output on standard output
    -f  specify archive file name
    -m  include manifest information from specified manifest file
    -e  specify application entry point for stand-alone application
        bundled into an executable jar file
    -0  store only; use no ZIP compression
    -M  do not create a manifest file for the entries
    -i  generate index information for the specified jar files
    -C  change to the specified directory and include the following file

If any file is a directory then it is processed recursively.
The manifest file name, the archive file name and the entry point name are specified in the same order as the 'm', 'f' and 'e' flags.

jar cvf  <jar-file>  <input-files>

Options :


c  - option indicates that you want to create a JAR file.

v - Produces verbose output on stderr (in version 1.1) or stdout (in version 1.2) while the JAR file is being built. The verbose output tells you the name of each file as it's added to the JAR file.

f  - option indicates that you want the output to go to a file rather than to stdout.

jar-file  - is the name that you want the resulting JAR file to have. You can use any filename for a JAR file. By convention, JAR filenames are given a .jar extension, though this is not required.

input-file(s) - is a space-separated list of one or more files that you want to be placed in your JAR file.
If any of the "input-files" are directories, the contents of those directories are added to the JAR archive recursively.

0(zero) - Indicates that you don't want the JAR file to be compressed.

M - Indicates that the default manifest file should not be produced.

C - To change directories during execution of the command. Version 1.2 only. See below for an example.

m - Used to include manifest information from an existing manifest file. The format for using this option is:

jar cmf <existing-manifes>t <jar-file> <input-files>

jar cvf <myjarName>.jar <input-Files or Directory>


Example 1: to archive two class files into an archive called classes.jar:
       jar cvf classes.jar Foo.class Bar.class

Example 2: use an existing manifest file 'mymanifest' and archive all the
           files in the foo/ directory into 'classes.jar':
       jar cvfm classes.jar mymanifest -C foo/ .


No comments:

Post a Comment