如何编写能够在运行时自我更新的 Java 应用程序?

我想实现一个 Java 应用程序(服务器应用程序) ,它可以下载一个新版本(。Jar 文件) ,然后在运行时更新自己。

做到这一点的最佳方法是什么? 这可能吗?

我想应用程序可以下载一个新的。然后启动它。但我应该如何进行交接,例如知道新应用程序何时启动,然后退出。还是有更好的办法?

79311 次浏览

This isn't necessarily the best way, but it might work for you.

You can write a bootstrap application (ala the World of Warcraft launcher, if you've played WoW). That bootstrap is responsible for checking for updates.

  • If an update is available, it will offer it to the user, handle the download, installation, etc.
  • If the application is up to date, it will allow the user to launch the application
  • Optionally, you can allow the user to launch the application, even if it isn't up to date

This way you don't have to worry about forcing an exit of your application.

If your application is web based, and if it is important that they have an up to date client, then you can also do version checks while the application runs. You can do them at intervals, while performing normal communication with the server (some or all calls), or both.

For a product I recently worked on, we did version checks upon launch (without a boot strapper app, but before the main window appeared), and during calls to the server. When the client was out of date, we relied on the user to quit manually, but forbid any action against the server.

Please note that I don't know if Java can invoke UI code before you bring up your main window. We were using C#/WPF.

I've written a Java application that can load plugins at runtime and start using them immediately, inspired by a similar mechanism in jEdit. jEdit is open source so you have the option of looking to see how it works.

The solution uses a custom ClassLoader to load files from the jar. Once they're loaded you can invoke some method from the new jar that will act as its main method. Then the tricky part is making sure you get rid of all references to the old code so that it can be garbage collected. I'm not quite an expert on that part, I've made it work but it wasn't easy.

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.

  • 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:

  • 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.

  • 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:

  • 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.

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

  • the solution is pure Java and portable,
  • the changeover will be quicker, and
  • you can more easily retain state across the restart (modulo leakage issues).

The "best" way depends on your specific requirements.

It should also be noted that:

  • 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.

  1. First way: use tomcat and it's deploy facilities.
  2. Second way: to split application on two parts (functional and update) and let update part replace function part.
  3. Third way: In your server appliction just download new version, then old version releases bound port, then old version runs new version (starts process), then old version sends a request on application port to the new version to delete old version, old version terminates and new version deletes old version. Like this: alt text

If you build your application using Equinox plugins, you can use the P2 Provisioning System to get a ready-made solution to this problem. This will require the server to restart itself after an update.

This is a known problem and I recommend against reinventing a wheel - don't write your own hack, just use what other people have already done.

Two situations you need to consider:

  1. App needs to be self-updatable and keep running even during update (server app, embedded apps). Go with OSGi: Bundles or Equinox p2.

  2. App is a desktop app and has an installer. There are many installers with update option. Check installers list.

I am currently developing a JAVA Linux Daemon and also had the need to implement an auto-update mechanism. I wanted to limit my application to one jar file, and came up with a simple solution:

Pack the updater application in the update itself.

Application: When the application detects a newer version it does the following:

  1. Download update (Zipfile)
  2. Extract Application and ApplicationUpdater (all in the zipfile)
  3. Run updater

ApplicationUpdater: When the updater runs it does the following:

  1. Stop the Application (in my case a daemon via init.d)
  2. Copy the downloaded jar file to overwrite current Application
  3. Start the Application
  4. Cleanup.

Hope it helps someone.

I see a security problem when downloading a new jar (etc.), e.g., a man in the middle attack. You always have to sign your downloadable update.

On JAX2015, Adam Bien told about using JGit for updating the binaries. Sadly I could not find any tutorials.

Source in German.

Adam Bien created the updater see here

I forked it here with some javaFX frontend. I am also working on an automatic signing.

I've recently created update4j which is fully compatible with Java 9's module system.

It will seamlessly start the new version without a restart.