Take the 2-minute tour ×

I have two classes listed as following

package foo;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;

public class CustomClassLoader extends ClassLoader{
    public CustomClassLoader(ClassLoader parent){

    public Class<?> loadClass(String name) throws ClassNotFoundException {
        System.out.println( " >>> loading " + name );

        if (name.startsWith("foo")){
            return getClass(name);
        return super.loadClass(name);

    public Class getClass(String name){
        try {
            byte[] data= getClassByteData(name);
        return  this.defineClass(name, data, 0, data.length);

        } catch (IOException e) {
        return null;

    public byte[] getClassByteData(String name) throws IOException{
        String file = name.replace(".", File.separator)+".class";
        InputStream in   = this.getClass().getResourceAsStream("/"+file);
        int length = in.available();
        byte[] datas = new byte[length];
        return datas;


package foo;

    public class Test {
        public static void main(String[] args) {

            System.out.println( " Who is my loader? >>" + Test.class.getClassLoader() );


java -Djava.system.class.loader=foo.CustomClassLoader foo.Test

 >>> loading java.lang.System
 >>> loading java.nio.charset.Charset
 >>> loading java.lang.String
 >>> loading foo.Test
 >>> loading java.lang.Object
 >>> loading java.lang.StringBuilder
 >>> loading java.lang.Class
 >>> loading java.io.PrintStream
 Who is my loader? >>foo.CustomClassLoader@de6ced

My question are as below:

  • **

    1. 1.Why those java.lang.System,java.nio.charset.Charset etc as above would be loader by CustomClassLoader? In my ideas i think when i run java -Djava.system.class.loader foo.Test this command, jvm first search out class foo.Test, loader it and execute the main method , and then when it detect System.out.println(), it will continue to loade Class 'java.lang.System' and 'java.io.PrintWriter' because those classes are used by it right?

      2.If When I run a class which use some classes located in "java.lang" package, those class will also be loaded again, for my case delegated CustomClassLoader>>ExtClassLoader>>BoostrapClassLoader to load?

      3.When /lib/rt.jar and /lib/ext/**.jar are loaded, before we run a class like java foo.Test all those classes have bean loader aready?


Thanks all your help in advance!

share|improve this question
add comment

2 Answers

up vote 2 down vote accepted

To answer your questions, let's take them in turn.

  1. Java's ClassLoading mechanism is based on a delegation model where an attempt to load a class is normally delegated to parent class loaders first. If the parent class loader is able to successfully load the class, then the loaded class is passed back down the delegate chain and return by whichever class loader was originally invoked. Since you set your custom class loader as the system class loader, the JVM instantiated it as the default loader used when your application starts. It is then used to load all classes that it needs. Most of those classes (i.e. anything that doesn't start with foo.*) are delegated to the parent class loaders, which explains why you see messages about your class loader attempting to load them.

  2. Because the loading of non foo.* classes is delegated to the parent class loader, loading a system class, such as System.out, again will be handled by the parent class loader, which handles caching all loaded classes and will return the previously loaded instance.

  3. Loading of the rt.jar and lib/ext/*.jar files is handled by the parent class loader (or its parent), not you. Therefore, there is no need for your class loader to concern itself with those JAR files.

Generally, a custom class loader is built to insure that classes can be loaded from a non-standard location, such as JARs stored in a database.

share|improve this answer
add comment

I assume that actual command line you running is java -Djava.system.class.loader=foo.CustomClassLoader foo.Test.

  1. In general all classes can be loaded multiple times by different class loaders. In fact they are even considered different classes. So then foo.Test needs a java.lang.System, its class loader is invoked to find or load it.

  2. In your case, CustomClassLoader delegates non foo class loading to super, which will not load same class second time, but return previously loaded one.

  3. Talking about loading jars in misleading. Classes from those jars are loaded individually, on-demand. To load any program JVM needs to create a thread, so Thread class and its dependencies are loaded even before your class.

you can run sun's java with -verbose:class to see how classes are loaded.

share|improve this answer
add comment

Your Answer


