Create JAR File in Java
Create JAR File in Java
Let us see how to create a JAR file using Java’s jar command as well as using Eclipse IDE.
The JAR file format is based on the popular ZIP file format. Usually these file are used for
archiving and distribution the files and implementing various libraries, components and plug-ins
in java applications. Compiler and JVMs (Java Virtual Machine) can understand and implement
these formats for java application.
view source
print?
1 jar cf JAR_FILE_NAME FILE_NAMES_OR_DIRECTORY_NAME
2 e.g.
3 jar cf MyApp1.jar C:\JavaProject\MyApp
view source
print?
1 jar tf JAR_FILE_NAME
2 e.g.
3 jar tf MyApp1.jar
view source
print?
1 jar tvf JAR_FILE_NAME
2 e.g.
3 jar tvf MyApp1.jar
Note that we have used v (verbose) option to see the detail of JAR.
Extract content of JAR file
view source
print?
1 jar xf JAR_FILE_NAME
2 e.g.
3 jar xf MyApp1.jar
view source
print?
1 jar xf JAR_FILE_NAME FILE_NAME(S)_FROM_JAR_FILE
2 e.g.
3 jar xf MyApp1.jar Test1.class
view source
print?
1 jar uf JAR_FILE_NAME FILE_NAMES_FROM_JAR_FILE
2 e.g.
3 jar uf MyApp1.jar Test1.class
view source
print?
1 java -jar JAR_FILE_NAME
2 e.g.
3 java -jar MyApp.jar
In order to create an executable JAR, one of the classes that we include in our JAR must be a
main class.
Create a text file called MANIFEST.MF using any text editor and copy following content in it.
view source
print?
1 Manifest-Version: 1.0
2 Main-Class: MyMainClass
Where MyMainClass is the name of the class that contents main method. Also note that you have
to specify fully qualified class name here.
Use following command to create an executable JAR file.
view source
print?
1 jar cvfm MyApp.jar MANIFEST.MF FILE_NAMES_OR_DIRECTORY_NAME