Implementing an automatic update feature

Discussion in 'Java' started by Chris, Jan 5, 2005.

  1. Chris

    Chris Guest

    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?
     
    Chris, Jan 5, 2005
    #1
    1. Advertising

  2. Chris

    Jon Caldwell Guest

    Chris wrote:
    > 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]);
     
    Jon Caldwell, Jan 5, 2005
    #2
    1. Advertising

  3. On Wed, 5 Jan 2005 16:38:42 -0600, Chris wrote:

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

    --
    Andrew Thompson
    http://www.PhySci.org/codes/ Web & IT Help
    http://www.PhySci.org/ Open-source software suite
    http://www.1point1C.org/ Science & Technology
    http://www.LensEscapes.com/ Images that escape the mundane
     
    Andrew Thompson, Jan 6, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Guest
    Replies:
    1
    Views:
    797
    Guest
    Jun 29, 2004
  2. Replies:
    6
    Views:
    1,983
  3. Rob R. Ainscough
    Replies:
    3
    Views:
    5,071
    tdavisjr
    Mar 22, 2006
  4. ssubbarayan
    Replies:
    1
    Views:
    529
    Andrew Smallshaw
    Oct 29, 2008
  5. Sonny Chee

    Implementing a "calcuator" feature

    Sonny Chee, Jan 21, 2007, in forum: Ruby
    Replies:
    1
    Views:
    99
    Jeremy Tregunna
    Jan 21, 2007
Loading...

Share This Page