【发布时间】:2010-06-24 14:08:09
【问题描述】:
我想在运行时将一些已编译的类(.class 文件)添加到当前 Jar 文件中的目录(包)
我怎样才能做到这一点?
谢谢
【问题讨论】:
-
为什么在运行时?为什么将其添加到 jar 文件而不是将类添加到类路径?我问这个是因为这个想法似乎有点奇怪。
-
我使用这个类作为程序的插件,程序从它的包中读取它的插件我还需要这个程序是一个单独的 jar 文件
我想在运行时将一些已编译的类(.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 行为。
【讨论】: