How to create a Jar file in command prompt

This article will tell you how to create a jar file using Java’s Jar command in command prompt. Basically jar file format is formed from ZIP file format. Jar files are mainly used for archiving, compression and decompression.

Note : Java should be installed in your system to execute Jar command and also correct java path should be set up before starting.

Using Jar command you can create,view,delete,update/modify,extract and execute the jar files.

1. Creating a Jar file

1.
Format:
jar cf JAR_NAME INPUT_FILENAME(S) OR DIRECTORY_NAME

c – create JAR file
f – Output should go to file rather than stdout (terminal)
JAR_NAME – Name of the Jar file (can give any filename you want). Filename should be given .jar extension
INPUT_FILENAME(S) – One or more files to be included in the JAR file and each file will be separated by a space
DIRECTORY_NAME – If the “input files” are directory, then the contents of the directory (given in the DIRECTORY_NAME) are added to the jar recursively.

Note : c and f should be in any order but should not have any space in between them

E.G:
jar cf sample.jar D:\Java\sample.class

or

jar cf sample.jar D:\Java

D:/Java is a Directory

2.
Format:
jar cvf JAR_NAME INPUT_FILENAME(S) OR DIRECTORY_NAME

v – with “verbose” output which tells the names of all the files that is added to the jar.

E.G:
jar cvf sample.jar D:\Java

added manifest
adding: D:/Java/(in = 0) (out= 0)(stored 0%)
adding: D:/Java/audio/(in = 0) (out= 0)(stored 0%)
adding: D:/Java/audio/amma.wav(in = 46480270) (out= 43382835)(deflated 6%)
adding: D:/Java/audio/sampath.wave.wav(in = 626251) (out= 592538)(deflated 5%)
adding: D:/Java/Images/(in = 0) (out= 0)(stored 0%)
adding: D:/Java/Images/bike22.jpg(in = 48241) (out= 45154)(deflated 6%)
adding: D:/Java/Images/Desert.jpg(in = 845941) (out= 842691)(deflated 0%)
adding: D:/Java/sample.java(in = 94) (out= 89)(deflated 5%)
adding: D:/Java/sample.jar

3.
Format:
jar cvf0 JAR_NAME INPUT_FILENAME(S) OR DIRECTORY_NAME

0 – (zero) Jar file will not be compressed

E.G:
jar cvf0 sample.jar D:\Java

added manifest
adding: D:/Java/(in = 0) (out= 0)(stored 0%)
adding: D:/Java/audio/(in = 0) (out= 0)(stored 0%)
adding: D:/Java/audio/amma.wav(in = 46480270) (out= 46480270)(stored 0%)
adding: D:/Java/audio/sampath.wave.wav(in = 626251) (out= 626251)(stored 0%)
adding: D:/Java/Images/(in = 0) (out= 0)(stored 0%)
adding: D:/Java/Images/bike22.jpg(in = 48241) (out= 48241)(stored 0%)
adding: D:/Java/Images/Desert.jpg(in = 845941) (out= 845941)(stored 0%)
adding: D:/Java/sample.java(in = 94) (out= 94)(stored 0%)
adding: D:/Java/sample.jar

4.
Format:
jar cvfm JAR_NAME MANIFEST_FILENAME INPUT_FILENAME(S) OR DIRECTORY_NAME

m – include external manifest file
MANIFEST_FILENAME – Name of the Manifest filename

Note :
M – include default manifest file (need not mention manifest filename)

E.G:
1.
jar cvfm hello.jar MANIFEST.MF hello.java
added manifest
adding: hello.java(in = 2906) (out= 1025)(deflated 64%)

To view the jar file contents
jar tf hello.jar
META-INF/
META-INF/MANIFEST.MF
hello.java

2.
jar cvfM sample.jar hello.java
adding: hello.java(in = 2906) (out= 1025)(deflated 64%)

2. Viewing the contents of a jar file

Format:
jar tf JAR_NAME

t – Display the table of contents of the Jar file
f – Contents of the Jar file should be display on the command line

E.G:
jar tf sample.jar

META-INF/
META-INF/MANIFEST.MF
D:/Java/
D:/Java/audio/
D:/Java/audio/amma.wav
D:/Java/audio/sampath.wave.wav
D:/Java/Images/
D:/Java/Images/bike22.jpg
D:/Java/Images/Desert.jpg
D:/Java/sample.java

3. Viewing the contents in detail of a jar file

Format:
jar tvf JAR_NAME

v – with “verbose” output

E.G:

jar tvf sample.jar

0 Fri Mar 22 18:18:18 IST 2013 META-INF/
68 Fri Mar 22 18:18:18 IST 2013 META-INF/MANIFEST.MF
0 Wed Mar 20 18:00:14 IST 2013 D:/Java/
0 Wed Mar 20 17:13:24 IST 2013 D:/Java/audio/
46480270 Sun May 02 20:13:38 IST 2004 D:/Java/audio/amma.wav
626251 Wed Nov 26 17:46:00 IST 1997 D:/Java/audio/sampath.wave.wav
0 Wed Mar 20 17:26:26 IST 2013 D:/Java/Images/
48241 Sat Jun 28 06:44:54 IST 1986 D:/Java/Images/bike22.jpg
845941 Tue Jul 14 11:02:30 IST 2009 D:/Java/Images/Desert.jpg
94 Wed Mar 20 17:11:28 IST 2013 D:/Java/sample.java

4. Extracting the contents of a jar

1.
Format:
jar xf JAR_NAME

x – Extract from the Jar file (mentioned in the JAR_NAME)
f – Extracted files should be displayed on the command line
JAR_NAME – Name of the jar file from which the files to be extracted

E.G:
jar xf sample.jar

2.
Format:
jar xvf JAR_NAME

v – with verbose output

E.G:
jar xvf sample.jar

created: META-INF/
inflated: META-INF/MANIFEST.MF
created: D:/Java/
created: D:/Java/audio/
inflated: D:/Java/audio/amma.wav
inflated: D:/Java/audio/sampath.wave.wav
created: D:/Java/Images/
inflated: D:/Java/Images/bike22.jpg
inflated: D:/Java/Images/Desert.jpg
inflated: D:/Java/sample.java

5. Extracting the specific file from a jar

1.
Format:
jar xf JAR_NAME ARCHIVED_FILE(S)_FROM_JAR

ARCHIVED_FILE(S)_FROM_JAR – Space separated list of files to be extracted from the Jar

E.G:
jar xf sample.jar sample.java

2.
Format:
jar xvf JAR_NAME ARCHIVED_FILE(S)_FROM_JAR

v – with verbose output

E.G:
jar xvf sample.jar sample.jar
inflated: sample.java

6. Updating the Jar

1.
Format:
jar uf JAR_NAME ARCHIVED_FILE(S)_FROM_JAR

u – Update an existing file
f – Update to jar file to be displayed on the command line
JAR_NAME – Existing jar file to be updated
ARCHIVED_FILE(S)_FROM_JAR – Space separated one or more files to be added to the existing jar file

E.G:
jar uf sample.jar hello.java

jar tf sample.jar
META-INF/
META-INF/MANIFEST.MF
D:/Java/
D:/Java/audio/
D:/Java/audio/amma.wav
D:/Java/audio/sampath.wave.wav
D:/Java/Images/
D:/Java/Images/bike22.jpg
D:/Java/Images/Desert.jpg
D:/Java/sample.java
hello.java

2.
Format:
jar uvf JAR_NAME ARCHIVED_FILE(S)_FROM_JAR

v – with verbose output

E.G:
jar uvf sample.jar hello.java
adding: hello.java(in = 2906) (out= 1025)(deflated 64%)

jar tf sample.jar
META-INF/
META-INF/MANIFEST.MF
D:/Java/
D:/Java/audio/
D:/Java/audio/amma.wav
D:/Java/audio/sampath.wave.wav
D:/Java/Images/
D:/Java/Images/bike22.jpg
D:/Java/Images/Desert.jpg
D:/Java/sample.java
hello.java

7. Executing a Jar file

Format:
java -jar JAR_FILE_NAME

-jar – jar flag tells the java launcher that the application is packaged as a jar file. (Only one jar file will be executed at time)
JAR_FILE_NAME – Name of the jar file to be executed

E.G:
java -jar sample.jar

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>