Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what class loader is used?

I have several questions regarding to class loaders.

Class.forName("class.name");

and

....
NotYetLoadedClass cls = new NotYetLoadedClass();
.....

What class loaders will be used in each case? For the first case I assume class loader that was used to load class in which method code is executing. And in the second case I assume thread context class loader.

In case I am wrong, a small explanation is appreciated.

like image 669
michael nesterenko Avatar asked Jul 25 '12 21:07

michael nesterenko


People also ask

Why ClassLoader is used in Java?

Class loaders are responsible for loading Java classes dynamically to the JVM (Java Virtual Machine) during runtime. They're also part of the JRE (Java Runtime Environment). Therefore, the JVM doesn't need to know about the underlying files or file systems in order to run Java programs thanks to class loaders.

What is a class loader example?

For example, java. lang package class. Extensions Class Loader: It delegates class loading request to its parent. If the loading of a class is unsuccessful, it loads classes from jre/lib/ext directory or any other directory as java.

Which is a class loader?

The Java ClassLoader is a part of the Java Runtime Environment that dynamically loads Java classes into the Java Virtual Machine. The Java run time system does not need to know about files and file systems because of classloaders. Java classes aren't loaded into memory all at once, but when required by an application.


1 Answers

Both use the current ClassLoader. As DNA correctly points out, http://docs.oracle.com/javase/6/docs/api/java/lang/Class.html#forName%28java.lang.String%29 states that Class.forName() uses the current class loader. A little experiment shows that a class loaded for instantiation using the new statement also uses the current ClassLoader:

public class Test
{
    public static void main(String[] args) throws Exception
    {
        Thread.currentThread().setContextClassLoader(new MyClassLoader());
        SomeClass someClass = new SomeClass();
        someClass.printClassLoader();
    }

    public static class MyClassLoader extends ClassLoader
    {
        public MyClassLoader()
        {
            super();
        }

        public MyClassLoader(ClassLoader parent)
        {
            super(parent);
        }
    }
}



public class SomeClass
{
    public void printClassLoader()
    {
        System.out.println(this.getClass().getClassLoader());
        System.out.println(Thread.currentThread().getContextClassLoader());
    }
}

In Test we set the current's thread ContextClassLoader to some custom ClassLoader and then instantiate an object of class SomeClass. In SomeClass we print out the current thread's ContextClassLoader and the ClassLoader that loaded this object's class. The result is

sun.misc.Launcher$AppClassLoader@3326b249
test.Test$MyClassLoader@3d4b7453

indicating that the current ClassLoader (sun.misc.Launcher.AppClassLoader) was used to load the class.

like image 94
Uli Avatar answered Oct 15 '22 15:10

Uli