Search code examples
javaclassloader

Who loads java.lang.ClassLoader?


I was reading how the classes are loaded. It seems instance of java.lang.ClassLoader is doing that job.

But who loads the java.lang.ClassLoader?


Solution

  • Figured I could add this as an answer so people can see it more readily...

    The java.lang.ClassLoader is part of the Java core libraries (as an abstract class) and Java-provided implementations of it are loaded by the JVM by the bootstrap class loader. The bootstrap class loader is written in native code, and is run when the JVM starts, to load all the Java libraries in $JAVA_HOME/jre/lib

    To quote the relevant Wikipedia entry re: the Java Class Loaders:

    When the JVM is started, three class loaders are used:

    1. Bootstrap class loader
    2. Extensions class loader
    3. System class loader

    The bootstrap class loader loads the core Java libraries located in the $JAVA_HOME/jre/lib directory. This class loader, which is part of the core JVM, is written in native code.

    The extensions class loader loads the code in the extensions directories ($JAVA_HOME/jre/lib/ext, or any other directory specified by the java.ext.dirs system property). It is implemented by the sun.misc.Launcher$ExtClassLoader class.

    The system class loader loads code found on java.class.path, which maps to the CLASSPATH environment variable. This is implemented by the sun.misc.Launcher$AppClassLoader class.

    When you start the JVM with java -cp <some classes> my.package.MainClass the bootstrap classloader mentioned above is run in native code (as part of the JVM executable) to load all Java native libraries. The class loader chain mentioned above is then kicked off to load any remaining classes that have been specified at the command line and/or in classpath arguments.