0

In first case, For explicit loading of test.ClassLoaderTest using below code,

public ClassLoaderTest{
      public static void main(String[] args){
        .....
        Class.forName("test.ClassLoaderTest", true,
                          ClassLoaderTest.class.getClassLoader().getParent());
        ....
      }

findClass() method of Launcher$ExtClassLoader instance gets invoked to load test.ClassLoaderTest with below error due to visibility principle,

java.lang.ClassNotFoundException: test.ClassLoaderTest
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at sun.misc.Launcher$ExtClassLoader.findClass(Launcher.java:229)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:247)
at test.ClassLoaderTest.main(ClassLoaderTest.java:29)

In second case, On explicit loading of test.ClassLoaderTest1, using

public ClassLoaderTest{
          public static void main(String[] args){
            .....
            Class.forName("test.ClassLoaderTest1");
            ....
          }

loadClass() method of Launcher$AppClassLoader instance is ultimately used to load test.ClassLoaderTest1 class,

where test.ClassLoaderTest1 is a wrong class file that lead to below error,

java.lang.ClassNotFoundException: test.ClassLoaderTest1
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:264)
    at test.ClassLoaderTest1.main(ClassLoaderTest.java:16)

In both cases, class loading job is done by java.net.URLClassLoader.findClass()


Class.forName() internally invokes getClassLoader() to know the class loader that already loaded the class.

In second case, When Class gets a class loader instance(of type Launcher$AppClassLoader) by calling ClassLoader cl = getClassLoader0(); to invoke the class loader instance again.

Is java.lang.ClassLoader mainly used for sub-classing custom class loader? that load classes not available in CLASSPATH but from network source etc...

1 Answer 1

1

From the Javadoc,

public **abstract** class ClassLoader
extends Object

There are different ClassLoader implementations that use different strategies for locating and reading the byte streams that compose a class.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.