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.

7
  • 1
    I'm not sure I understand your question. When you detect a new version, you download the new bits and run the new bits instead of the old bits. Commented Nov 12, 2012 at 19:02
  • possible duplicate of "Automatic updates" for Java (desktop) application? Commented Nov 12, 2012 at 19:02
  • My question is: How do I download those new bits and replace the old bits at runtime? Commented Nov 12, 2012 at 19:03
  • @Lirik - Not even close, I said that JNLP is NOT an option. Commented Nov 12, 2012 at 19:03
  • See this question and [my answer](stackoverflow.com/a/207590/18573] to it Commented Nov 12, 2012 at 19:05

2 Answers 2

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 :).

Sign up to request clarification or add additional context in comments.

4 Comments

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.
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.
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?
You could also use ProcessBuilder to launch a new OS process, and let the bootstrap application exit normally after that. docs.oracle.com/javase/1.5.0/docs/api/java/lang/…
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.

2 Comments

you don't need OSGI, just 2 programs.
would be my choice too, but I kept to the question requirements.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.