Reloader – Class Loading Utility

Reloader is a Java utility that provides some advanced class loading capabilities not found in standard Java class loaders, such as URLClassLoader.

Reloading comes handy when a new version of a class needs to be loaded without stopping the process where the class is running. All servlet/JSP engines and application servers use reloading extensively. Reloading is an invaluable feature for testing/debugging, but it is can also be useful for the production application since it allows for hot-swapping of application components.

The main problem of using URLClassLoader or JVM system loader for dynamic class loading is that they cache all loaded classes in memory and JDK does not provide API to explicitly discard these cached classes. So, a new class loader should be created every time when a class needs to be reloaded. But, since class loader is part of the class type (meaning that two versions of the same class loaded by different class loaders are treated as two incompatible data types), it is very easy to run into a problem with ClassCastException.
The related problem is that class loaders always delegate the loading request to the parent class loader first. It means that classes on the system class path can’t be reloaded since the system loader (parent of all class loaders) is always going to satisfy the request with the cached version of a class.

Reloader cures the problems of URLClassLoader and the likes and provides the following capabilities:

  • Reloads classes from the JVM classpath (system classpath). No need to put all reloadable classes into a separate directory outside of the system classpath (standard approach employed by most app servers and JSP/servlet engines).
  • Fine-grained control over what classes should or should not be reloaded. For example, Reloader can be provided with a list of classes that should be loaded. Alternatively, the list of “exception” classes (to load all classes other than exceptions) can be provided. Another option is to put reloadable classes into separate packages, in which case Reloader would load only classes from these packages. Standard approach with putting reloadable classes on the special class path not available to the system loader is, of course, also supported.
  • Extensive logging using event-notification mechanism. Reloader can log various events, such as “before loading”, “after loading”, “parent class loader loading” and so on.
  • Loading and logging can be turned on and off dynamically either using provided API or JVM system property.

For more information on class loaders, see:

Download reloader

Reloader JavaDoc API