Java - Jar File Read by API

Introduction

You can read the entries from a JAR file using Java API.

To read the entries from a manifest file, get the object of Manifest class using getManifest() class of the JarInputStream

import java.io.FileInputStream;
import java.util.Map;
import java.util.jar.Attributes;
import java.util.jar.JarInputStream;
import java.util.jar.Manifest;

public class Main {
  public static void main(String[] args) throws Exception {
    // Create a JAR input stream object
    JarInputStream jis = new JarInputStream(new FileInputStream("jartest.jar"));

    // Get the manifest file from the JAR file. Will return null if
    // there is no manifest file in the JAR file.
    Manifest manifest = jis.getManifest();

    if (manifest != null) {
      // Get the attributes from main section
      Attributes mainAttributes = manifest.getMainAttributes();
      String mainClass = mainAttributes.getValue("Main-Class");

      // Get the attributes from individual section
      Map<String, Attributes> entries = manifest.getEntries();
    }
  }
}

Related Topic