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

查看:161
本文介绍了如何编写可在运行时自行更新的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应用程序,

  • 应用程序创建的任何辅助线程等将在没有特殊关闭逻辑的情况下消失,并且

  • 您也可以处理从应用程序崩溃中恢复等。

  • you only need one JAR,
  • you can replace the entire Java app,
  • any secondary threads created by the app, etc will go away without special shutdown logic, and
  • you can also deal with recovery from application crashes, etc.

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

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


  • 解决方案是纯Java和便携式,

  • 转换速度更快,

  • 您可以更轻松地在重新启动时保持状态(模数泄漏问题)。

最佳方式取决于您的具体情况要求。

The "best" way depends on your specific requirements.

还应注意:


  • 那里是汽车的安全风险-updating。通常,如果提供更新的服务器受到威胁,或者提供更新的机制容易受到攻击,那么自动更新可能会导致客户端受到损害。

  • 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天全站免登陆