Java:在运行时将类添加到Jar归档

Java:在运行时将类添加到Jar归档,java,jar,runtime,Java,Jar,Runtime,我想在运行时将一些已编译的类(.class文件)添加到当前Jar文件中的目录(包) 我该怎么做 谢谢这无法完成-要更新Jar文件,您需要创建一个新文件,并用新文件覆盖旧文件 以下是您将如何执行此操作的示例: import java.io.*; import java.util.*; import java.util.zip.*; import java.util.jar.*; public class JarUpdate { /** * main() */ publ

我想在运行时将一些已编译的类(.class文件)添加到当前Jar文件中的目录(包)
我该怎么做


谢谢

这无法完成-要更新Jar文件,您需要创建一个新文件,并用新文件覆盖旧文件

以下是您将如何执行此操作的示例:

import java.io.*;
import java.util.*;
import java.util.zip.*;
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);
               tempJar.putNextEntry(entry);

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

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

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

            // 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.

                  tempJar.putNextEntry(entry);

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

            jarUpdated = true;
         }
         catch (Exception ex) {
            System.out.println(ex);

            // Add a stub entry here, so that the jar will close without an
            // exception.

            tempJar.putNextEntry(new JarEntry("stub"));
         }
         finally {
            tempJar.close();
         }
      }
      finally {
         jar.close();
         System.out.println(jarName + " closed.");

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

         if (! jarUpdated) {
            tempJarFile.delete();
         }
      }

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

      if (jarUpdated) {
         jarFile.delete();
         tempJarFile.renameTo(jarFile);
         System.out.println(jarName + " updated.");
      }
   }
}

我不太确定,但我认为不可能从JAR文件(我们称之为foo.JAR)加载类,然后修改从中加载类的JAR文件,添加一个新类,并期望类加载器能够找到该类


我会考虑重构应用程序本身,使其能够动态加载类(使用URLClassLoader或任何其他技术),而不是试图强制执行您描述的单个JAR行为。

为什么在运行时?为什么要将其添加到jar文件中,而不是将类添加到类路径中?我问这个问题是因为这个想法似乎有点奇怪。我使用这个类作为程序的插件,程序从它的包中读取插件。我还需要这个程序是一个jar文件,但是jar文件是zip格式的,并且可以将文件添加到zip档案中@如果您使用WinZip或7Zip,请窃笑是的,不是在Java中。@窃笑:Java的zip支持没有那些程序强大;重新实现或扩展它以支持将条目添加到现有存档可能需要大量的工作。而且它很可能仍然不能做您想要的事情,WinZip或7Zip也不能,因为您要写入的文件将被JVM锁定,而JVM一直在从中读取类。