Search code examples
javajarclassloaderdesktop-applicationauto-update

How create a Java application that auto-updates itself?


It is as simple as an application that can update itself. More specific, maybe a launcher that loads a jar file from the web and replaces it with the current jar.

The approach that I want is:

  • A launcher which is coded in Java.
  • That launcher overall uses classloader e.t.c.
  • I want to keep the data from the previous jar. Maybe the data from the previous jar are written on a .txt file and pass them to the new one? (If possible)

I have no clue about classloader or any direction to follow. So my question is "How can i use classloader to create an auto-update Java application?".

P.S. I want java web start to be the last choice. Some lines of code would help me very much.

Edit: I understand completely what are you saying in theory. What i am looking for is specific links or code which i can take as reference. The question isn't a duplicate because i can't find links or code but theory.


Solution

  • You can use the URLClassLoader to load a jar file from an URL. Mind, URLs may also be local files, and I'd recommend that you download the jar file locally before loading it in, instead of loading it directly from the web. The reason for this is that you want the application to remain usable, even if the user cannot reach your server.

    However, I'd also recommend against taking this approach. What if you want to update the launcher? It's better to download a seperate updater application from the main application, launch the seperate updater application, have that kill the main application and update it.