简体   繁体   English

如何将新的java类添加到jar文件中的现有类

[英]How to add new java classes to the existing classes in jar file

I created new modules for software written in java. 我为用java编写的软件创建了新模块。 This software is run from jar file.My classes are extending the classes in this software. 该软件从jar文件运行。我的类正在扩展该软件中的类。 I want to write a plugin which adds new classes to the jar file. 我想写一个插件,它将新类添加到jar文件中。 I know how to extract a jar file to the directory and add them to the jar file using java.util.jar package but as there are a lot of files to extract from the file, is it possible to add new classes without extracting the files to the folder, somehow directly. 我知道如何将jar文件解压缩到目录并使用java.util.jar包将它们添加到jar文件中,但由于要从文件中提取大量文件,是否可以在不提取文件的情况下添加新类以某种方式直接到文件夹。

您可以使用以下命令

jar uf foo.jar foo.class

Your question indicates that you want to do this programmatically, not using the command-line jar tool. 您的问题表明您希望以编程方式执行此操作, 而不是使用命令行jar工具。

From the docs it appears that you'll at least need to get a JarInputStream from an existing JarFile , iterate over the entries using getNextJarEntry , and write to a JarOutputStream with the entries, also adding your own new files using new JarEntry objects for each new file. 从文档看来,您至少需要从现有的JarFile获取JarInputStream ,使用getNextJarEntry迭代条目,并使用条目写入JarOutputStream ,同时使用新的JarEntry对象为每个新文件添加自己的新文件文件。

Here's sample code for writing a jar from scratch programmatically, this should extend nicely to your case, the difference being that most of the JarEntry objects will be coming from a JarInputStream . 这是用于以编程方式从头开始编写jar 示例代码,这应该很好地扩展到您的情况,不同之处在于大多数JarEntry对象将来自JarInputStream

jar uf jar-file input-file(s)

将允许在jar中添加新文件/更新现有文件而不提取。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM