如何编写可以在运行时自行更新的 Java 应用程序? [英] How can I write a Java application that can update itself at runtime?

查看:31
本文介绍了如何编写可以在运行时自行更新的 Java 应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现一个 java 应用程序(服务器应用程序),它可以从给定的 url 下载新版本(.jar 文件),然后在运行时更新自身.

I would like to implement a java application (server application) that can download a new version (.jar file) from a given url, and then update itself at runtime.

这样做的最佳方法是什么,可能吗?

What is the best way to do this and is it possible?

我猜应用程序可以下载一个新的 .jar 文件并启动它.但是我应该如何进行交接,例如知道新应用程序何时启动然后退出.或者有没有更好的方法来做到这一点?

I guess that the application can download a new .jar file and start it. But how should I do the handover, e.g. know when the new application is started and then exit. Or is there a better way to do this?

推荐答案

一个解决方案的基本结构如下:

The basic structure of a solution is as follows:

  • 有一个主循环负责重复加载最新版本的应用程序(如果需要)并启动它.

  • There is a main loop responsible for repeatedly loading the latest version of the app (if required) and launching it.

应用程序会做它的事情,但会定期检查下载 URL.如果它检测到新版本,它会返回到启动器.

The application does its thing, but periodically checks the download URL. If it detects a new version it exits back to the launcher.

您可以通过多种方式实现这一点.例如:

There are a number of ways you could implement this. For example:

  • 启动器可以是一个包装脚本或二进制应用程序,它启动一个新的 JVM 以从一个被替换的 JAR 文件运行应用程序.

  • The launcher could be a wrapper script or binary application that starts a new JVM to run the application from a JAR file that gets replaced.

启动器可以是一个 Java 应用程序,它为新的 JAR 创建一个类加载器,加载一个入口点类并在其上调用一些方法.如果你这样做,你必须注意类加载器存储泄漏,但这并不困难.(您只需要确保在重新启动后无法访问具有从 JAR 加载的类的对象.)

The launcher could be a Java application that creates a classloader for the new JAR, loads an entrypoint class and calls some method on it. If you do it this way, you have to watch for classloader storage leaks, but that's not difficult. (You just need to make sure that no objects with classes loaded from the JAR are reachable after you relaunch.)

外部包装方法的优点是:

The advantages of the external wrapper approach are:

  • 你只需要一个 JAR,
  • 您可以替换整个 Java 应用程序,
  • 应用程序创建的任何辅助线程等都将在没有特殊关闭逻辑的情况下消失,并且
  • 您还可以处理从应用程序崩溃等中恢复的问题.

第二种方法需要两个 JAR,但具有以下优点:

The second approach requires two JARs, but has the following advantages:

  • 该解决方案是纯 Java 和可移植的,
  • 转换会更快,并且
  • 您可以更轻松地在重启期间保持状态(模泄漏问题).

最佳"方式取决于您的具体要求.

The "best" way depends on your specific requirements.

还需要注意的是:

  • 自动更新存在安全风险.一般来说,如果提供更新的服务器受到威胁,或者提供更新的机制容易受到攻击,那么自动更新可能会导致客户端受到威胁.

  • There are security risks with auto-updating. In general, if the server that provides the updates is compromised, or if the mechanisms for providing the updates are susceptible to attack, then auto-updating can lead to a compromise of the client(s).

向客户推送更新可能会对客户造成损害,这可能会带来法律风险,并对您的企业声誉造成风险.

Pushing a update to a client that cause damage to the client could have legal risks, and risks to your business' reputation.

如果您能找到避免重新发明轮子的方法,那就太好了.请参阅其他答案以获取建议.

If you can find a way to avoid reinventing the wheel, that would be good. See the other answers for suggestions.

这篇关于如何编写可以在运行时自行更新的 Java 应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆