ClassLoader : Java Glossary
home C words local find no local find frame, full screen Google search web for topic jump to footer translate with Babelfish by Roedy Green ©1996-2008 Canadian Mind Products
Go to : punctuation 0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z (all)
ClassLoader
ClassLoaders sound intimidating, but if you look at the source code for one, they are pretty simple. Basically all they do is find the class file somewhere on disk, read it into RAM, and call java.lang.ClassLoader.defineClass tell the system to treat ram image as legitimate byte codes. defineClass and all the other interesting methods are protected, so you will have to write you own class that extends ClassLoader to get at them. It will all come clear if you study the source code for ClassLoader in src.zip.

There are some minor complications dealing with chaining ClassLoaders so they ask other ClassLoaders first before going to look on their own.

The default ClassLoader can drive you nuts. Sometimes it refuses to look on the local hard disk CLASSPATH. It won’t look in a jar other than the ones mentioned in the <APPLET ARCHIVE ones. These archives are downloaded en masse whether you need the files in them or not. The ClassLoader/Security system won’t let net-loaded classes use pre-installed local DLLs. Unsigned Applets cannot use custom ClassLoaders.

Custom ClassLoaders

A custom ClassLoader is really quite simple. It need not even involve writing a new class. You can often just instantiate one of the existing ClassLoaders. A ClassLoader needs a private byte[] loadClassData(String name) method to find the bytes for the class somewhere, and a standard protected synchronized Class loadClass(String name, boolean resolve) to call your loadClassData. You inherit everything else you need from ClassLoader.

Why would you ever want a custom ClassLoader?

Hot Code Changes

ClassLoaders let you modify code on the fly without shutting down an application. New code is loaded with Class.forname. It creates new versions of the objects by reloading the classes for the objects with a ClassLoader. You then have both old and new versions of the same object, both with the same name, but with different data and different code. From the JVM’s point of view, a class loaded with a new ClassLoader is a different animal entirely from the one loaded with the standard ClassLoader even if the classes have the same name. You can manipulate both old an new objects with a common interface.

With a new ClassLoader, you can load a different version of a class. The old objects continue to use the old code. New objects use the new code. A given ClassLoader can load a given class only once. There is no need to unload a class. When the objects using it are no longer referenced, the class object itself, along with the code, will be garbage collected. The same classes, loaded by different class loaders are considered distinct classes. They are not instanceofs each other!

You will have to instantiate a new ClassLoader every time you have a new generation of classes. You can load all the replacement classes of a generation with the same ClassLoader. However, when you want to replace the replaced classes, you need a yet another new ClassLoader. You can do this with multiple instances of the same ClassLoader. You only need to write one ClassLoader, perhaps not even one, not one for each generation.

Have a look at the java.net.URLClassLoader. You may find for your given problem you don’t even have to write whole new ClassLoader, just instantiate one of Sun’s.

Learning More

Sun’s Javadoc on the URLClassLoader class : available:
Sun’s Javadoc on the ClassLoader class : available:
Sun’s JDK Technote Guide on How RMI uses ClassLoaders : available:
Sun’s Javadoc on the Class class : available:

CMP_homejump to top
CMP logo
feedback Please email your feedback for publication, errors, omissions, broken/redirected link reports
and suggestions to improve this page to Roedy Green : feedback email
made with CSS
HTML Checked!
ICRA ratings logo
mindprod.com IP:[65.110.21.43]
Your face IP:[38.103.63.18] The information on this page is for non-military use only.
You are visitor number 29,307. Military use includes use by defence contractors.
You can get a fresh copy of this page from: or possibly from your local J: drive (Java virtual drive/Mindprod website mirror)
http://mindprod.com/jgloss/classloader.html J:\mindprod\jgloss\classloader.html