Reloading Java Classes 101: Objects, Classes and ClassLoaders

November 10th, 2009 by Jevgeni Kabanov

Welcome to Turnaround article series from ZeroTurnaround.

In this article we will review how to reload a Java class using a dynamic classloader. To get there we’ll see how objects, classes and classloaders are tied to each other and the process required to make changes. We begin with a bird’s eye view of the problem, explains the reloading process, and then proceed to a specific example to illustrate typical problems and solutions. Other articles in the series include:


A Bird’s Eye View

The first thing to understand when talking about reloading Java code is the relation between classes and objects. All Java code is associated with methods contained in classes. Simplified, you can think of a class as a collection of methods, that receive “this” as the first argument. The class with all its methods is loaded into memory and receives a unique identity. In the Java API this identity is represented by an instance of java.lang.Class that you can access using the MyObject.class expression.

Every object created gets a reference to this identity accessible through the Object.getClass() method. When a method is called on an object, the JVM consults the class reference and calls the method of that particular class. That is, when you call mo.method() (where mo is an instance of MyObject), then the JVM will call mo.getClass().getDeclaredMethod("method").invoke(mo) (this is not what the JVM actually does, but the result is the same).

object

Every Class object is in turn associated with its classloader (MyObject.class.getClassLoader()). The main role of the class loader is to define a class scope — where the class is visible and where it isn’t. This scoping allows for classes with the same name to exist as long as they are loaded in different classloaders. It also allows loading a newer version of the class in a different classloader.

reloading-object

The main problem with code reloading in Java is that although you can load a new version of a class, it will get a completely different identity and the existing objects will keep referring the previous version of the class. So when a method is called on those objects it will execute the old version of the method.

Let’s assume that we load a new version of the MyObject class. Let’s refer to the old version as MyObject_1 and to the new one as MyObject_2. Let’s also assume that MyObject.method() returns “1″ in MyObject_1 and “2″ in MyObject_2. Now if mo2 is an instance of MyObject_2:

  • mo.getClass() != mo2.getClass()
  • mo.getClass().getDeclaredMethod("method").invoke(mo)
    != mo2.getClass().getDeclaredMethod("method").invoke(mo2)
  • mo.getClass().getDeclaredMethod("method").invoke(mo2) throws a ClassCastException, because the Class identities of mo and mo2 do no match.

This means that any useful solution must create a new instance of mo2 that is an exact copy of mo and replace all references to mo with it. To understand how hard it is, remember the last time you had to change your phone number. It’s easy enough to change the number itself, but then you have to make sure that everyone you know will use the new number, which is quite a hassle. It’s just as difficult with objects (in fact, it’s actually impossible, unless you control the object creation yourself), and we’re talking about many objects that you must update at the same time.

Down and Dirty

Let’s see how this would look in code. Remember, what we’re trying to do here is load a newer version of a class, in a different classloader. We’ll use an Example class that looks like this:

public class Example implements IExample {
  private int counter;
  public String message() {
    return "Version 1";
  }
  public int plusPlus() {
    return counter++;
  }
  public int counter() {
    return counter;
  }
}

We’ll use a main() method that will loop infinitely and print out the information from the Example class. We’ll also need two instances of the Example class: example1 that is created once in the beginning and example2 that is recreated on every roll of the loop:

public class Main {
  private static IExample example1;
  private static IExample example2;
 
  public static void main(String[] args)  {
    example1 = ExampleFactory.newInstance();
 
    while (true) {
      example2 = ExampleFactory.newInstance();
 
      System.out.println("1) " +
        example1.message() + " = " + example1.plusPlus());
      System.out.println("2) " +
        example2.message() + " = " + example2.plusPlus());
      System.out.println();
 
      Thread.currentThread().sleep(3000);
    }
  }
}

IExample is an interface with all the methods from Example. This is necessary because we’ll be loading Example in an isolated classloader, so Main cannot use it directly (otherwise we’d get a ClassCastException).

public interface IExample {
  String message();
  int plusPlus();
}

From this example, you might be surprised to see how easy it is to create a dynamic class loader. If we remove the exception handling it boils down to this:

public class ExampleFactory {
  public static IExample newInstance() {
    URLClassLoader tmp = 
      new URLClassLoader(new URL[] {getClassPath()}) {
        public Class loadClass(String name) {
          if ("example.Example".equals(name))
            return findClass(name);
          return super.loadClass(name);
        }
      };
 
    return (IExample) 
      tmp.loadClass("example.Example").newInstance();
  }
}

The method getClassPath() for the purposes of this example could return the hardcoded classpath. However, in the full source code (available in the Resources section below) you can see how we can use the ClassLoader.getResource() API to automate that.

Now let’s run Main.main and see the output after waiting for a few loop rolls:

1) Version 1 = 3
2) Version 1 = 0

As expected, while the counter in the first instance is updated, the second stays at “0″. If we change the Example.message() method to return “Version 2″. The output will change as follows:

1) Version 1 = 4
2) Version 2 = 0

As we can see, the first instance continues incrementing the counter, but uses the old version of the class to print out the version. The second instance class was updated, however all of the state is lost.

To remedy this, let’s try to reconstruct the state for the second instance. To do that we can just copy it from the previous iteration.

First we add a new copy() method to Example class (and corresponding interface method):

  public IExample copy(IExample example) {
    if (example != null)
      counter = example.counter();  
    return this;
  }

Next we update the line in the Main.main() method that creates the second instance:

example2 = ExampleFactory.newInstance().copy(example2);

Now waiting for a few iterations yields:

1) Version 1 = 3
2) Version 1 = 3

And changing Example.message() method to return “Version 2″ yields:

1) Version 1 = 4
2) Version 2 = 4

As you can see even though it’s possible for the end user to see that the second instance is updated and all its state is preserved, it involves managing that state by hand. Unfortunately, there is no way in the Java API to just update the class of an existing object or even reliably copy its state, so we will always have to resort to complicated workarounds.

In subsequent articles we’ll review how web containers, OSGi, Tapestry 5, Grails and others confront the problem of managing state when reloading classes, then we’ll dig into how HotSwap, Dynamic Languages, and the Instrumentation API work, and go behind the scenes with JRebel as well.

Resources

Other Articles in the Reloading Java Classes Series

24 Tweets

28 Comments »

  1. Excellent!!!

    Maladets Jevgeni !

    Comment by Cem Koc — November 11, 2009 @ 8:09 am

  2. It seems that the example works only if the class is in the main classpath (i.e. in “./example” directory).

    If you put the class in a different directory, the code breaks with ClassNotFoundException, even if you modify the URL generation code.

    Why is that?

    //cp = new URL(loc.substring(0, loc.length() – resName.length()));
    cp = new URL(“file:/D:/temp”);

    [java] Exception in thread “Thread-0″ java.lang.RuntimeException: java.lang.ClassNotFoundException: example.Example
    [java] at example.DynaClassFactory.newInstance(DynaClassFactory.java:32)
    [java] at example.Main.run(Main.java:19)
    [java] at java.lang.Thread.run(Thread.java:619)

    Comment by Rob — November 24, 2009 @ 6:48 pm

  3. @Rob

    “example” is a Java package and is part of the class name. When URLClassLoader looks for example.Example it really searches all classpath URLs for “example/Example.class”.

    Comment by Jevgeni Kabanov — November 24, 2009 @ 10:58 pm

  4. Hi Evgenij,

    Understood – this is exactly what I tried.
    It seems that if the example.Example is in the \launching classpath\ – i.e. \.\ or root, it works.

    However, If i place the example/Example somewhere else, and then use:

    = new URL(\file:/Location off class path\);

    The code breaks.

    Do you have an explanation for this behavior?

    Thank you.

    Comment by Rob — November 25, 2009 @ 10:35 pm

  5. [...] load the new class file in the JVM’s memory. A more detailed explanation is listed at the JRebel blog – in practice this means the only thing we have to do after updating a class file is [...]

    Pingback by Speeding up development with JRebel — December 4, 2009 @ 12:25 pm

  6. [...] RJC101: Objects, Classes and ClassLoaders [...]

    Pingback by Reloading Java Classes 201: How do ClassLoader leaks happen? | ZeroTurnaround.com — December 9, 2009 @ 12:13 pm

  7. [...] RJC101: Objects, Classes and ClassLoaders [...]

    Pingback by Reloading Java Classes 301: Classloaders in Web Development — Tomcat, GlassFish, OSGi, Tapestry 5 and so on | ZeroTurnaround.com — January 14, 2010 @ 9:00 am

  8. [...] RJC101: Objects, Classes and ClassLoaders [...]

    Pingback by Reloading Java Classes 401: HotSwap and JRebel — Behind the Scenes | ZeroTurnaround.com — February 11, 2010 @ 2:36 pm

  9. [...] RJC101: Objects, Classes and ClassLoaders [...]

    Pingback by RJC501: How Much Does Turnaround Cost? | ZeroTurnaround.com — March 9, 2010 @ 1:16 pm

  10. This is great, I saw the link on TheServerSide and knew I had to check it out.
    Nice article

    Comment by Helen Neely — March 10, 2010 @ 5:47 pm

  11. There is a not case.
    mo.getClass().getDeclaredMethod(“method”).invoke(mo)
    != mo2.getClass().getDeclaredMethod(“method”).invoke(mo2)
    If method invocation return a fixed object, the result must be =.
    So in that case, the assertion failed.

    Comment by qinxian — March 13, 2010 @ 7:01 am

RSS feed for comments on this post. TrackBack URL

Leave a comment

Additional comments powered by BackType

Olark Livehelp