Java 更新 jar 中的 .class 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1667153/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 18:55:17  来源:igfitidea点击:

Updating .class file in jar

javaeclipsejar

提问by penguru

I want to update a .classfile in a jarwith a new one. What is the easiest way to do it, especially in the Eclipse IDE?

我想用一个新的jar更新一个.class文件。最简单的方法是什么,尤其是在 Eclipse IDE 中?

采纳答案by Brian Agnew

This tutorialdetails how to update a jar file

本教程详细介绍了如何更新 jar 文件

jar -uf jar-file <optional_folder_structure>/input-file(s)     

where 'u' means update.

其中“u”表示更新。

回答by JasCav

Do you want to do it automatically or manually? If manually, a JAR file is really just a ZIP file, so you should be able to open it with any ZIP reader. (You may need to change the extension first.) If you want to update the JAR file automatically via Eclipse, you may want to look into Antsupport in Eclipse and look at the zip task.

你想自动还是手动完成?如果手动,JAR 文件实际上只是一个 ZIP 文件,因此您应该能够使用任何 ZIP 阅读器打开它。(您可能需要先更改扩展名。)如果您想通过 Eclipse 自动更新 JAR 文件,您可能需要查看Eclipse 中的Ant支持并查看zip 任务

回答by silent

Jar is an archive, you can replace a file in it by yourself in your favourite file manager (Total Commander for example).

Jar 是一个存档文件,您可以在自己喜欢的文件管理器(例如 Total Commander)中自行替换其中的文件。

回答by Craig Angus

1) you can extract the file into a folder called

1)您可以将文件解压缩到名为的文件夹中

jarname.jar

文件名.jar

and then replace the file in the folder, handy if you are updating the class a lot while debugging

然后替换文件夹中的文件,如果您在调试时更新类很多,这很方便

2) you can extract the jar replace the file then the jar it up again

2)你可以解压jar替换文件然后再把jar包起来

3) Open the jar with 7 zip and drag and drop your new class in to copy over the old one

3) 用 7 zip 打开 jar 并将您的新类拖放到其中以复制旧类

回答by Lastnico

Simply drag and drop your new class file to the JAR using 7-Zipor Winzip. You can even modify a JAR file that is included in a WAR file using the parent folder icon, and click Ok when 7zip detects that the inside file has been modified

只需使用7-Zip或 Winzip将您的新类文件拖放到 JAR 中即可。您甚至可以使用父文件夹图标修改包含在 WAR 文件中的 JAR 文件,并在 7zip 检测到内部文件已被修改时单击确定

回答by Dave Jefferson

An alternative is not to replace the .class file in the jar file. Instead put it into a new jar file and ensure that it appears earlier on your classpath than the original jar file.

另一种方法是不替换 jar 文件中的 .class 文件。而是将它放入一个新的 jar 文件中,并确保它比原始 jar 文件更早地出现在您的类路径中。

Not sure I would recommend this for production software but for development it is quick and easy.

不确定我会推荐这个用于生产软件,但对于开发来说,它既快速又简单。

回答by James Lim

  1. Use jar -xvfto extract the files to a directory.
  2. Make your changes and replace the classes.
  3. Use jar -cvfto create a new jar file.
  1. 用于jar -xvf将文件解压缩到一个目录。
  2. 进行更改并替换类。
  3. 使用jar -cvf创建一个新的jar文件。

回答by Toby Mellor

A JAR file is just a .zip in disguise. The zipped folder contains .classfiles.

JAR 文件只是伪装的 .zip。压缩文件夹包含.class文件。

If you're on macOS:

如果您使用的是 macOS:

  1. Rename the file to possess the '.zip' extension. e.g. myJar.jar -> myJar.zip.
  2. Decompress the '.zip' (double click on it). A new folder called 'myJar' will appear
  3. Find and replace the .classfile with your new .class file.
  4. Select all the contents of the folder 'myJar' and choose 'Compress x items'. DO NOT ZIP THE FOLDER ITSELF, ONLY ITS CONTENTS
  1. 重命名文件以拥有“.zip”扩展名。例如 myJar.jar -> myJar.zip。
  2. 解压缩“.zip”(双击它)。将出现一个名为“myJar”的新文件夹
  3. 查找.class文件并将其替换为新的 .class 文件。
  4. 选择文件夹“myJar”的所有内容,然后选择“压缩 x 项”。不要压缩文件夹本身,只压缩它的内容

Miscellaneous - Compiling a single .class file, with reference to a original jar, on macOS

杂项 - 在 macOS 上参考原始 jar 编译单个 .class 文件

  1. Make a file myClass.java, containing your code.
  2. Open terminal from Spotlight.
  3. javac -classpath originalJar.jar myClass.javaThis will create your compiled class called myClass.class.
  1. 创建一个文件 myClass.java,其中包含您的代码。
  2. 从 Spotlight 打开终端。
  3. javac -classpath originalJar.jar myClass.java这将创建名为 myClass.class 的已编译类。

From here, follow the steps above. You can also use Eclipse to compile it, simply reference the original jar by right clicking on the project, 'Build Path' -> 'Add External Archives'. From here you should be able to compile it as a jar, and use the zip technique above to retrieve the class from the jar.

从这里开始,按照上述步骤操作。您也可以使用 Eclipse 来编译它,只需通过右键单击项目,'Build Path' -> 'Add External Archives' 来引用原始 jar。从这里您应该能够将其编译为 jar,并使用上面的 zip 技术从 jar 中检索类。

回答by Shashi Ranjan

Editing properties/my_app.properties file inside jar:

在 jar 中编辑 properties/my_app.properties 文件:

"zip -u /var/opt/my-jar-with-dependencies.jar properties/my_app.properties". Basically "zip -u <source> <dest>", where dest is relative to the jar extract folder.

"zip -u /var/opt/my-jar-with-dependencies.jar properties/my_app.properties". 基本上"zip -u <source> <dest>",其中 dest 是相对于 jar 提取文件夹的。

回答by hari

You can find source code of any .jar file online, import the same project in your IDE with basic setups. Make necessary changes in .java file and compile it for .class files.

您可以在线找到任何 .jar 文件的源代码,使用基本设置在您的 IDE 中导入相同的项目。在 .java 文件中进行必要的更改并为 .class 文件编译它。

Once compilation is done You need to extract the jar file, replace the old .class file with new one.

编译完成后,您需要提取 jar 文件,用新的 .class 文件替换旧的 .class 文件。

And use below command for reconstruct .jar file

并使用以下命令重建 .jar 文件

Jar cf test.jar *

Jar cf test.jar *

Note : I have done so many time this changes in our project, hope you will find it useful.

注意:我在我们的项目中做了很多次这个更改,希望你会发现它有用。