Implementing an automatic update feature

C

Chris

We've got a webapp that depends on a jar file in the /lib directory. We'd
like to implement an automatic updates feature, where a user could go to a
special page, click on "Check for updates", and have the system go to our
server and download a new version of the jar file (and a lot of other files
as well).

All this is easy to do. Once we've downloaded the files, however, how do we
get the JVM to stop running the code in the current jar file, unload all the
classes, and then start using the new jar file?
 
J

Jon Caldwell

Chris said:
We've got a webapp that depends on a jar file in the /lib directory. We'd
like to implement an automatic updates feature, where a user could go to a
special page, click on "Check for updates", and have the system go to our
server and download a new version of the jar file (and a lot of other files
as well).

All this is easy to do. Once we've downloaded the files, however, how do we
get the JVM to stop running the code in the current jar file, unload all the
classes, and then start using the new jar file?

When you start your application, the VM assigns you your very own
classloader for that application. Your classloader is there to stay for
that application instance, and the classloader caches class definitions
that have been previously loaded.

To accomplish this (at least partially) you must separate your classes
into a "bootstrap" jar of classes just needed to start the program (a
"main" class). This may not change on the fly. You can then load the
second jar with a child classloader (URLClassLoader is probably what you
would want to use). When the Jar gets updated, dump the old classloader,
and reconstruct it.

This gets a little tricky if you have never done this before. Basically
you have to explicitly load the classloader and class, and invoke it
through reflection like this.

URL jarUrl = new URL( "file:/path/to/my/app/updatable.jar" );
URLClassLoader cl = new URLClassLoader(jarUrl,
getClass().getClassLoader() );
Class c = cl.loadClass("com.jcaldwel.test.UpdatableMain");
Object o = c.newInstance();
Method run = c.getMethod("run", new Class[0] );
run.invoke(o,new Object[0]);
 
A

Andrew Thompson

We've got a webapp that depends on a jar file in the /lib directory. We'd
like to implement an automatic updates feature, where a user could go to a
special page, click on "Check for updates", and have the system go to our
server and download a new version of the jar file (and a lot of other files
as well).

All this is easy to do. Once we've downloaded the files, however, how do we
get the JVM to stop running the code in the current jar file, unload all the
classes, and then start using the new jar file?

JWS can keep the main and library jars up to date for you.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top