7

So, this is something that's been on my mind for a while now. How can you take a program, and make it 'auto-update'. So let's say an outer shell that checks Myserver.com/myProg/updates.xml (or some other resource) and checks to make sure version numbers are the same. Once I do that, how do I handle updating a program?

Let's say that my program was a simple main only class and the only output is:

System.out.println("Hello World"); On the update it turns into System.out.println("Hello Java");

How could I get this change into place at runtime?

Note: JNLP is not applicable for this, due to signing issues that I don't care to expand upon.

A_Elric
  • 3,508
  • 13
  • 52
  • 85

2 Answers2

9

You need 2 "applications":

  1. A minimal bootstrap application that checks for updates and runs the main application;
  2. The main application.

The user always launches the bootstrap application, never the main application (in fact the user doesn't know about the bootstrap application). This is by far the most common pattern I've seen.

If you want to do auto-updates at runtime, you'll need to use OSGI as javabeats mentioned. But only take this path if you really need this. I've never used OSGI, but I can imagine that it's non-trivial.

Edit

I don't have a concrete example, but I can imagine that

  1. the bootstrap downloads some jar and configuration files
  2. One of the configuration files contains the files required in the classpath (this could be done automatically by your app, if it picks all the jar files that are inside a given folder).
  3. With the previous list, create a new classloader (see example here) to add the jar files to the classpath.
  4. Run the main class of the application in the new classloader.

Sorry, I can't give you a more detailed answer without writing the code myself, but I charge for that :).

Community
  • 1
  • 1
Augusto
  • 28,839
  • 5
  • 58
  • 88
  • 1
    auto-updates at runtime is almost never a good idea for any non-trivial application. too many ways to get into a bad state. always better to re-start the main application. – jtahlborn Nov 12 '12 at 19:09
  • I definitely agree with you. I think it's easier in plugin-based / micro-kernel applications (I'm thinking of Jenkins as an example). Something more complex will require a restart. – Augusto Nov 12 '12 at 19:12
  • Can you give an example of how one might make this work? I'm thinking of running a command line arg to do a java -jar myJar.jar from the bootstrap, but there must be a more eloquent way, no? – A_Elric Nov 12 '12 at 20:21
  • 2
    You could also use ProcessBuilder to launch a new OS process, and let the bootstrap application exit normally after that. http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/ProcessBuilder.html – javabeats Nov 13 '12 at 13:57
2

If JNLP is ruled out, I'd think only a solution using OSGI would achieve that. Otherwise, even at the very simplest design possible, you'll need another program to manage and download the versions of your current program.

javabeats
  • 1,082
  • 3
  • 12
  • 26