java 运行时 添加jar文件,Java:在运行时将类添加到Jar存档

I want to add some compiled classes (.class files) to directories(packages) in current Jar file at runtime

How can I do that?

Thanks

解决方案

This cannot be done - To update a Jar file you need to create a new one and overwrite the old one with the new one.

Below is a sample on how you would do this:

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.");

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值