How to read Jar files in Java

Jar files are ways of packaging Java classes and sources at times. We might at times need to read the Jar files and process the contents. Let’s see how to read Jar Files in Java

Let’s quickly see the code

package com.codezuzu;

import java.util.jar.JarEntry;
import java.util.jar.JarInputStream;

public class JarReader {

  public static void main(String[] args) throws Exception {
    JarInputStream jarInputStream = new JarInputStream(new FileInputStream(args[0]));

    JarEntry jarEntry;

    while ((jarEntry = jarInputStream.getNextJarEntry()) != null) {

To read a Jar file, we need to use JarInputStream. Each Entry within a Jar is represented by a JarEntry object. Once we get hold of JarInputStream, we need to iterate through it till we get JarEntry. Here we just output the name and compressed size. In real life, we can add custom processing on each JarEntry.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.