Updating a JAR whilst running

Download a new version and rename it
with the same name of the last Jar and
then initialise the new Jar, creating
a seamless update of the Jar within
the JVM … Is it even possible to
update a Jar whilst its running?

The JAR file is not ‘running’, the JVM is running. Your JAR file just contains the class information (aka byte code instructions) that make the JVM do useful work. In most cases the JVM will actually not put a system lock on your JAR file, so you can replace that file to your hearts content.

The real problem of course is that once the JVM loads your JAR it will carry along happily with what it loaded and never read from your JAR file again, no matter how many times you overwrite it. This is the behavior of the default class loader and cannot be changed – however as others have pointed out – you do NOT have to use the default class loader. You can implement your own, similar to what Web Application Servers use, in order to load updated JARS from the filesystem. Caveat though – defining your own classloader is considered a ‘Bad Idea™’ unless you really know what your doing. You can read more here and here.

Leave a Comment