How can I write a Java application that can be updated at runtime?

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?

I assume that the application can download a new .jar file and run it. But how do I do a handover, for example? Know when a new application starts, and then exit. Or is there a better way to do this?

+65
java jar auto-update
Oct 23 '10 at 4:21
source share
8 answers

The basic structure of the solution is as follows:

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

  • The application performs its task, but periodically checks the download URL. If he discovers a new version, he returns to the launchpad.

There are several ways to implement this. For example:

  • The launcher can be a shell script or a binary application that launches a new JVM to launch the application from a JAR file that will be replaced.

  • The launcher may be a Java application that creates a class loader for the new JAR, loads the entry point class, and calls some method on it. If you do this, you need to keep track of the leaks of the class repository, but it is not difficult. (You just need to make sure that no objects with classes loaded from the JAR will be available after a restart.)

Advantages of the external wrapper method:

  • you only need one JAR,
  • you can replace the whole java application,
  • any secondary threads created by the application, etc. disappear without special shutdown logic, and
  • You can also handle recovery from application crashes, etc.

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

  • the solution is pure Java and portable,
  • the transition will be faster, and
  • you can easily save the state during reboot (problems with leak modulo).

The "best" way depends on your specific requirements.

It should also be noted that:

  • There are security risks when updating automatically. In general, if the server that provides updates is at risk, or if the mechanisms for delivering updates are susceptible to attack, then automatic updates can compromise clients.

  • Pressing an update to a customer that is detrimental to the customer may have legal risks and risks to the reputation of your business.




If you can find a way to avoid wheel reuse, that would be good. See Other suggestions for answers.

+44
Oct 23 '10 at 6:01
source share

I am currently developing JAVA Linux Daemon and also need to implement an automatic update mechanism. I wanted to limit my application to a single jar file and came up with a simple solution:

Install the update application in the update itself.

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

  • Download Update (Zipfile)
  • Extract application and ApplicationUpdater (all in a zip file)
  • Run updater

ApplicationUpdater . When the updater is running, it performs the following actions:

  • Stop the application (in my case, the daemon via init.d)
  • Copy downloaded jar file to overwrite current application
  • Run the application
  • Cleanup

Hope this helps someone.

+26
Nov 12
source share

I wrote a Java application that can load plugins at runtime and use them immediately, inspired by a similar mechanism in jEdit. jEdit is open source, so you have the opportunity to see how it works.

The solution uses a custom ClassLoader to load files from the bank. After loading them, you can call some method from the new jar, which will act as the main method. Then the hard part is to make sure that you get rid of all the links to the old code so that it can collect garbage. I am not quite an expert in this part, I did it, but it was not easy.

+10
Oct 23 2018-10-10T00:
source share

This is a known problem, and I recommend not reinventing the wheel - do not write your own hack, just use what other people have already done.

Two situations you need to consider:

  • The application must be self-healing and continue to work even during the upgrade (server application, embedded applications). Go to OSGi: Bundles or Equinox p2 .

  • The application is a desktop application and has an installer. There are many installers with the upgrade option. Check the list of installers .

+7
Oct 26 '10 at 7:53
source share
  • First way: use tomcat and deploy tools.
  • The second method: divide the application into two parts (functional and updates) and allow partial replacement of components to replace the part.
  • Third way: in your server application, just download the new version, then the old version releases the connected port, and then the old version launches the new version (starts the process), then the old version sends a request to the application port to the new version to delete the old version, the old version ends , and the new version removes the old version. Like this: alt text
+5
Oct 23 '10 at 18:21
source share

This is not necessarily the best way, but it may work for you.

You can write a download application (ala the World of Warcraft launcher if you played WoW). This 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 relevant, it allows the user to run the application
  • If you wish, you can allow the user to run the application, even if it is not updated

This way, you don’t have to worry about getting out of your application.

If your application is based on the Internet, and if it is important that they have a modern client, you can also perform version checks while the application is running. You can execute them at intervals, performing normal communication with the server (some or all of the calls), or both.

For the product that I recently worked with, we performed a version check at startup (without an application to bind to download, but before the main window appears) and during calls to the server. When the client was outdated, we relied on the user to log out manually, but prohibit any action against the server.

Please note that I do not know if Java can invoke user interface code before you open the main window. We used C # / WPF.

+2
Oct 23 '10 at 4:23
source share

If you are building an application using Equinox plugins, you can use the P2 Provisioning System to get a complete solution to this problem. This will require a server reboot after the upgrade.

+2
Oct 26 2018-10-10T00:
source share

I see a security problem when loading a new can (etc.), for example, a person in an average attack. You always need to sign your downloadable update.

At JAX2015, Adam Bien talked about using JGit to update binary files. Unfortunately, I could not find the textbooks.

Source in German.

Adam Bien created the update program here

I developed it here with some javaFX interface. I am also working on automatic signing.

0
Sep 17 '15 at 8:15
source share



All Articles