Search code examples
javaclassloader

Why jar file inside <JAVA_HOME>/jre/lib directory not loaded by bootstrap class loader


It is being said that

The bootstrap class loader loads the core Java libraries located in the <JAVA_HOME>/jre/lib directory.

And

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.

Now if my main program is

public class TestSt {

    public static void main(String[] args) {

        System.out.println(String.class.getClassLoader());
        System.out.println(Student.class.getClassLoader());
        System.out.println(TestSt.class.getClassLoader());
    }

}

The output is

null
sun.misc.Launcher$AppClassLoader@73d16e93 sun.misc.Launcher$AppClassLoader@73d16e93

Which is fine.

Now if I put Student Jar file in /jre/lib/ext The output is

null
sun.misc.Launcher$ExtClassLoader@3d4eac69 sun.misc.Launcher$AppClassLoader@2a139a55

Which is also fine.

But I couldnot understand that if I place student jar file in
/jre/lib directory . Why the output is giving as

null
sun.misc.Launcher$AppClassLoader@73d16e93 sun.misc.Launcher$AppClassLoader@73d16e93

I was thinking that Student Class in this case should be loaded from bootstrap class loader, Why it is loaded by application class loader. I think I am missing something. Please let me know where I am wrong.


Solution

  • There is only a few directories where all the JARs are loaded automatically. In directories such as /jre/lib only the JARs it expects to load are loaded. Any other JAR needs to loaded via the classpath.