如何修补 Java 程序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17560635/
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-11-01 02:19:13  来源:igfitidea点击:

How to patch a Java program?

javajarwarauto-update

提问by Jefrey Valencia

Recently I applied a fix to a Java desktop application. I did this by changing the code in one of my classes, compiled it and sent the new jar to the production environment.

最近,我对 Java 桌面应用程序进行了修复。我通过更改我的一个类中的代码来做到这一点,编译它并将新的 jar 发送到生产环境。

I'm now asked if it is possible to just patch the jar in production by just copying the compiled class that I fixed, or even create a patching program/script that will be able to update just the modified files.

我现在被问到是否可以通过复制我修复的编译类来修补生产中的 jar,或者甚至创建一个修补程序/脚本,该程序/脚本将能够只更新修改后的文件。

Additional info:

附加信息:

  1. The patch does not have to be applied in run time. Meaning the patch can be done as a separate program or activity. My old program does not need to auto update itself.
  2. Can this be done with web applications (WAR file) too?
  1. 该补丁不必在运行时应用。这意味着补丁可以作为单独的程序或活动来完成。我的旧程序不需要自动更新。
  2. 这也可以通过 Web 应用程序(WAR 文件)完成吗?

The best answer I came up is this, but it's 2 years old. Patching Java software

我想出的最佳答案是this,但它已经2岁了。 修补 Java 软件

Is my requirement rare? I have never seen a tutorial to patch a Java application.

我的要求很少见吗?我从未见过修补 Java 应用程序的教程。

采纳答案by Stephen C

I'm now asked if it is possible to just patch the jar in production by just copying the compiled class that I fixed, or even create a patching program/script that will be able to update just the modified files.

我现在被问到是否可以通过复制我修复的编译类来修补生产中的 jar,或者甚至创建一个修补程序/脚本,该程序/脚本将能够只更新修改后的文件。

Yes. It is possible to patch a JAR file using jar -u. It is also possible to patch a WAR file the same way.

是的。可以使用jar -u. 也可以用同样的方式修补 WAR 文件。

But I would NOT recommend it. The JAR or WAR file should be the unit of deployment / management. If you start patching JAR / WAR files on production servers, it is hard to track what is actually being used where. If you are not careful, chaos and confusion will reign.

但我不会推荐它。JAR 或 WAR 文件应该是部署/管理单元。如果您开始在生产服务器上修补 JAR / WAR 文件,则很难跟踪实际在何处使用了什么。如果你不小心,混乱和混乱将统治。

The only situation where patching is unavoidable is where you have a 3rd-party library or something that you can't build from source, but you need to modify nevertheless. But even in that case it is best to modify the JAR / WAR on your build platform and deploy the modified JAR / WAR. Trying to patch JARs, WARs or deployed webapps on the production platform is a bad idea.

唯一无法避免打补丁的情况是您拥有第 3 方库或无法从源代码构建的库,但您仍然需要进行修改。但即使在这种情况下,最好还是在您的构建平台上修改 JAR/WAR 并部署修改后的 JAR/WAR。尝试在生产平台上修补 JAR、WAR 或部署的 web 应用程序是一个坏主意。



Is my requirement rare?

我的要求很少见吗?

Yes. There's a better way ...

是的。有更好的方法...

I have never seen a tutorial to patch a Java application.

我从未见过修补 Java 应用程序的教程。

Not surprising, given that there's a better way ...

这并不奇怪,因为有更好的方法......

回答by Epicblood

You can update the old jar by doing jar uf jarfile classfile(s)

您可以通过执行以下操作来更新旧 jar jar uf jarfile classfile(s)

This will replace old class files with same name with new ones, making an auto patcher using that should not be hard

这将用新的类文件替换同名的旧类文件,使使用它的自动修补程序不难

回答by Andrew Thompson

For deploying Java desktop apps., the best option is usually to install the app. using Java Web Start. JWS works on Windows, OS X & *nix. It includes auto-update.

对于部署 Java 桌面应用程序,最好的选择通常是安装应用程序。使用Java Web Start。JWS 适用于 Windows、OS X 和 *nix。它包括自动更新。

Can this be done with web applications (WAR file) too?

这也可以通过 Web 应用程序(WAR 文件)完成吗?

No. JWS is for desktop apps. only.

不可以。JWS 适用于桌面应用程序。只要。

回答by Rage Steel

Take a look to Getdown— which aims to provide a system for downloading and installing a collection of files on a user's machine and upgrading those files as needed.

看看Getdown——它旨在提供一个系统,用于在用户机器上下载和安装文件集合,并根据需要升级这些文件。