开发者

Automatically update jar files

开发者 https://www.devze.com 2023-01-12 11:54 出处:网络
I am currently working on desktop software based on java.It\'s quite a big code base (more than 40 jar files).

I am currently working on desktop software based on java.It's quite a big code base (more than 40 jar files).

I wish to provide an automatic update functionality. The desktop software constantly checks one back end sys开发者_JAVA百科tem to see if there are new versions of the jar files available.

The problem now is: How to replace the updated jar files?


If you deploy your application using Java Webstart (JNLP), you get this mechanism almost for free...

From http://mindprod.com/jgloss/javawebstart.html

The key benefit for Java Web Start is automatic update without having to download the entire program every time.


  • Easiest would be to check for updates on each startup, download the updates and then launch your application. I think this is the way that Java Web Start works (see aioobes answer).

  • More complex would be to use either the netbeans or eclipse framework for your application. Both are rather complex and you will have to rewrite your application to work with them. This solution supports live updates.

As far as I am aware there is no easy way to update a running application. It is possible to load new versions of a class with a different classloader, but not possible to unload old versions while they are still referenced.


You can make a little server and a launcher which downloads the newest version, replaces the old one, and starts the jar with:

Runtime.getRuntime().exec("java yourjar -jar");

And you terminate the launcher with:

System.exit(1)


You can also serialize down your state (keep it in memory) and then create a new ClassLoader instance pointing to the new .jar files. Then serialize up your state again using this new classloader. You have just changed the underlaying .jars within a executing product.

Please note that you do not need to change the classloader for everything only for the part that is actually using the .jar files. This can be tricky to conclude what parts that are. And you might get nasty linking errors if done wrongly. So..

.. to keep it simple, use WebStart or a preloader that updates the .jars and then starts the main app (basically what WebStart does for you).

A reason for rolling your own, is that you can use your own format for the .jars, encryption, other packing formats etc.


After reading some answers to many auto-update questions, I thought of a solution. This is how I would implement a secure auto-update for a Java/Kotlin jar application.

Assumption: the installer will contain two jars: a launcher and the main application. Any shortcuts created will point to the launcher, but still be the name of the application. The release will contain the main application and the installer.

The launcher is launched first:

  1. First check if an update has already been downloaded as app_name_update.jar
  2. if an update has been downloaded, rename app_name_update.jar to app_name.jar
  3. Start app_name.jar
  4. This part does not have to be in the launcher, but it's preferred as to not slow down the main application: at this point, the launcher should check for an update (e.g. GitHub releases API) and download it to {CWD}/unverified_app_name_update.jar.
  5. Compare the hash of unverified_app_name_update.jar to an online location containing hashes for all published versions. hashes.txt would be an example found in the same github repository. If the software is open-source, GPG signed commits is a must and the launcher should check if the latest update is a verified commit! If its a proprietary application, keep the hashes.txt at a separate URL from the release where your company does not control the infrastructure (e.g. GitHub).

Main app launched:

  • No need to check for updates unless updates are mandatory, in which case check for update -> if update found, start loading animation "updating" while you can detect that the launcher is still running. Ensure that the launcher has no race condition while loops!


I found ready project to solve automatically updating. You can update your app, in your cases you can update jars and resources of your desktop app. The idea of the this is next: wrap you app with starter which can control updating and running you app. In details you can find here.

0

精彩评论

暂无评论...
验证码 换一张
取 消