Example of JarOutputStream in Java

By Arvind Rai, February 28, 2014
java.util.jar.JarOutputStream is a class that is used to write contents of a jar. A jar is a type of archive file and java provides JarOutputStream class that can efficiently write jar with flexibility of setting manifest file, comment set date time etc. To use JarOutputStream, we need to follow some steps which have been described below in detail with a complete example.

Prepare Manifest File

To create a jar, we need a manifest file. Java provides java.util.jar.Manifest class. In our demo we will write version and author in the manifest file. For this we need to fetch java.util.jar.Attributes from the method Manifest.getMainAttributes() and set required data in Attributes.

Create Required Jar Name

We need to provide a jar name. Create a file object and pass it to JarOutputStream as given below
OutputStream jos = new JarOutputStream(os, manifest);

Collect all Files and Class Names

Now this is the time to collect all our classes and other file names which needs to be pushed in jar. As we need to iterate, so make a list to store the files names.

Create JarEntry

While writing a file in jar, first we need to create JarEntry with the given file name. File path should start from package name. Here we can set create date, comment and other data specific to this file.
JarEntry je = new JarEntry(file);

Write Bytes of File into Jar

Write the bytes using JarOutputStream object. For this read file and write it to JarOutputStream. In our case we read the file using BufferedInputStream and writing it with buffer. It is necessary to close entry after writing every file using JarOutputStream.closeEntry().
Now find the complete example.
package com.concretepage.util.jar;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.jar.Attributes;
import java.util.jar.JarEntry;
import java.util.jar.JarOutputStream;
import java.util.jar.Manifest;
public class JarOutputStreamDemo {
       public static void main(String[] args) throws IOException {
        //prepare Manifest file
        String version = "1.0.0";
        String author = "concretepage";
        Manifest manifest = new Manifest();
        Attributes global = manifest.getMainAttributes();
        global.put(Attributes.Name.MANIFEST_VERSION, version);
        global.put(new Attributes.Name("Created-By"), author);
        //create required jar name
        String jarFileName ="D:/cp/jar/cp.jar";
        JarOutputStream jos = null;
        try {
           File jarFile = new File(jarFileName);
           OutputStream os = new FileOutputStream(jarFile);
           jos = new JarOutputStream(os, manifest);
        } catch (IOException e) {
        //Collect all file and class names to iterate
        List<String> fileList = new ArrayList<String>();
        String rootLocation ="D:/cp/jar/testjar/";
        //start writing in jar
        int len =0;
        byte[] buffer = new byte[1024];
        for(String file : fileList ){
              //create JarEntry
              JarEntry je = new JarEntry(file);
              je.setComment("Craeting Jar");
              //write the bytes of file into jar
              InputStream is = new BufferedInputStream(new FileInputStream(rootLocation+file));
            while((len = is.read(buffer, 0, buffer.length)) != -1) {
                jos.write(buffer, 0, len);
Find the output.
Check the location of your jar, you will get a jar with your provided name.

©2024 concretepage.com | Privacy Policy | Contact Us