How do I modify (add/delete) the content of an existing JAR file?

V S N Murthy Boggavarapu

You can't update the existing JAR file. You have to create a copy with the changes in it, remove the original, and then rename the updated JAR file. The following demonstrates this by adding a file to the JAR. provides additional information for working with JAR/ZIP files.

import java.util.*;
import java.util.jar.*;

public class JarUpdate {
    * main()
   public static void main(String[] args) throws IOException {
      // Get the jar name and entry name from the command-line.

      String jarName = args[0];
      String fileName = args[1];

      // Create file descriptors for the jar and a temp jar.
      File jarFile = new File(jarName);
      File tempJarFile = new File(jarName + ".tmp");

      // Open the jar file.

      JarFile jar = new JarFile(jarFile);
      System.out.println(jarName + " opened.");

      // Initialize a flag that will indicate that the jar was updated.

      boolean jarUpdated = false;

      try {
         // Create a temp jar file with no manifest. (The manifest will
         // be copied when the entries are copied.)

         Manifest jarManifest = jar.getManifest();
         JarOutputStream tempJar =
            new JarOutputStream(new FileOutputStream(tempJarFile));

         // Allocate a buffer for reading entry data.

         byte[] buffer = new byte[1024];
         int bytesRead;

         try {
            // Open the given file.
            FileInputStream file = new FileInputStream(fileName);

            try {
               // Create a jar entry and add it to the temp jar.
               JarEntry entry = new JarEntry(fileName);

               // Read the file and write it to the jar.

               while ((bytesRead = != -1) {
                  tempJar.write(buffer, 0, bytesRead);

               System.out.println(entry.getName() + " added.");
            finally {

            // Loop through the jar entries and add them to the temp jar,
            // skipping the entry that was added to the temp jar already.

            for (Enumeration entries = jar.entries(); entries.hasMoreElements(); ) {
               // Get the next entry.

               JarEntry entry = (JarEntry) entries.nextElement();

               // If the entry has not been added already, add it.

               if (! entry.getName().equals(fileName)) {
                  // Get an input stream for the entry.
                  InputStream entryStream = jar.getInputStream(entry);

                  // Read the entry and write it to the temp jar.


                  while ((bytesRead = != -1) {
                     tempJar.write(buffer, 0, bytesRead);

            jarUpdated = true;
         catch (Exception ex) {
            // Add a stub entry here, so that the jar will close without an
            // exception.
            tempJar.putNextEntry(new JarEntry("stub"));
         finally {
      finally {
         System.out.println(jarName + " closed.");

         // If the jar was not updated, delete the temp jar file.

         if (! jarUpdated) {

      // If the jar was updated, delete the original jar file and rename the
      // temp jar file to the original name.

      if (jarUpdated) {
         System.out.println(jarName + " updated.");
0 Comments  (click to add your comment)
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



About | Sitemap | Contact