How to create JAR from Java Classes

Filed Under: Java

Java Archive (JAR) files allows us to bundle multiple files in a single archive. Usually jar files contain class files and some other files like property files, images, applets etc.

We all have used jar files in projects, even JRE classes are packed as jar (rt.jar or classes.jar).

We can create jar file from any file, it’s similar to zip file.

Here we will learn how to create a jar file from a java project. I have two packages and two classes in my java project (MyProject). The project structure looks like below image.

java-jar

  1. Compile the java classes. This step is optional if classes are already compiled and inside target directory.
    
    pankaj@JD:~/CODE/MyProject/src$ javac com/journaldev/util/*.java com/journaldev/test/*.java -d ../bin/ -verbose
    [parsing started com/journaldev/util/MyUtil.java]
    [parsing completed 46ms]
    [parsing started com/journaldev/test/MyTest.java]
    [parsing completed 0ms]
    [search path for source files: .]
    [search path for class files: /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Classes/jsfd.jar,/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Classes/classes.jar,/System/Library/Frameworks/JavaVM.framework/Frameworks/JavaRuntimeSupport.framework/Resources/Java/JavaRuntimeSupport.jar,/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Classes/ui.jar,/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Classes/laf.jar,/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Classes/sunrsasign.jar,/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Classes/jsse.jar,/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Classes/jce.jar,/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Classes/charsets.jar,/System/Library/Java/Extensions/AppleScriptEngine.jar,/System/Library/Java/Extensions/dns_sd.jar,/System/Library/Java/Extensions/j3daudio.jar,/System/Library/Java/Extensions/j3dcore.jar,/System/Library/Java/Extensions/j3dutils.jar,/System/Library/Java/Extensions/jai_codec.jar,/System/Library/Java/Extensions/jai_core.jar,/System/Library/Java/Extensions/mlibwrapper_jai.jar,/System/Library/Java/Extensions/MRJToolkit.jar,/System/Library/Java/Extensions/QTJava.zip,/System/Library/Java/Extensions/vecmath.jar,/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/lib/ext/apple_provider.jar,/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/lib/ext/dnsns.jar,/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/lib/ext/localedata.jar,/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/lib/ext/sunjce_provider.jar,/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/lib/ext/sunpkcs11.jar,.]
    [loading java/lang/Object.class(java/lang:Object.class)]
    [loading java/lang/String.class(java/lang:String.class)]
    [checking com.journaldev.util.MyUtil]
    [loading java/lang/System.class(java/lang:System.class)]
    [loading java/io/PrintStream.class(java/io:PrintStream.class)]
    [loading java/io/FilterOutputStream.class(java/io:FilterOutputStream.class)]
    [loading java/io/OutputStream.class(java/io:OutputStream.class)]
    [wrote ../bin/com/journaldev/util/MyUtil.class]
    [checking com.journaldev.test.MyTest]
    [wrote ../bin/com/journaldev/test/MyTest.class]
    [total 422ms]
    

    Note that I am compiling both the packages classes in bin directory. verbose option is added for debugging purpose only.

  2. Using jar command to create the jar archive.
    
    pankaj@JD:~/CODE/MyProject/src$ cd ../bin
    pankaj@JD:~/CODE/MyProject/bin$ jar cvf MyProject.jar *
    added manifest
    adding: com/(in = 0) (out= 0)(stored 0%)
    adding: com/journaldev/(in = 0) (out= 0)(stored 0%)
    adding: com/journaldev/test/(in = 0) (out= 0)(stored 0%)
    adding: com/journaldev/test/MyTest.class(in = 444) (out= 303)(deflated 31%)
    adding: com/journaldev/util/(in = 0) (out= 0)(stored 0%)
    adding: com/journaldev/util/MyUtil.class(in = 444) (out= 304)(deflated 31%)
    

    Option c indicates that we want to create the jar file. Option v runs the jar command in verbose option that prints all the files included in jar archive and compression details. Option f allows us to send the output to the given file name.

Notice that jar command automatically adds the manifest file in the jar. Manifest file contains the metadata of jar file and it’s stored in META-INF/MANIFEST.MF file.

We can add our own manifest file with command jar cvmf MANIFEST.MF MyProject.jar com.

jar command matches the input parameters with the option name. Since we are using option as cvmf, we need to give the manifest file name first and then the jar file name.

Your jar file is ready and you can use it in any other projects.

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages