api上说
Returns the Class object associated with the class or interface with the given string name. Invoking this method is equivalent to:
Class.forName(className, true, currentLoader)
where currentLoader denotes the defining class loader of the current class.这个currentLoader是哪个loader?是Thread.currentThread().gettContextClassLoader() 吗?如果不是那currentLoader是哪个?
Returns the Class object associated with the class or interface with the given string name. Invoking this method is equivalent to:
Class.forName(className, true, currentLoader)
where currentLoader denotes the defining class loader of the current class.这个currentLoader是哪个loader?是Thread.currentThread().gettContextClassLoader() 吗?如果不是那currentLoader是哪个?
* @param args
* @throws ClassNotFoundException
*/
public static void main(String[] args) throws ClassNotFoundException {
ClassLoader ld = Test.class.getClassLoader();
System.out.println(ld.getClass());
// class sun.misc.Launcher$AppClassLoader
Class<?> c = Class.forName("a.Test");
System.out.println(c.getClassLoader().getClass());
// class sun.misc.Launcher$AppClassLoader
c = Class.forName("a.Test",true,Thread.currentThread().getContextClassLoader());
System.out.println(c.getClassLoader().getClass());
// class sun.misc.Launcher$AppClassLoader
// 说明在没有setContextClassLoader情况下,contextClassLoader是AppClassLoader
}}
是Thread.currentThread().gettContextClassLoader()。不同地方的字节码有不同的ClassLoader加载。
package simple;
class AAA{}
public class ClassLoaderTest {
public static void main(String[] args)throws Exception {
ClassLoader loader1 = ClassLoaderTest.class.getClassLoader();
ClassLoader loader2 = Class.forName("simple.AAA").getClassLoader();
ClassLoader loader3 =Thread.currentThread().getContextClassLoader();
System.out.println(loader1 == loader2);//true
System.out.println(loader1 == loader3);//true
}
}
等效于:
Class.forName("Foo", true, this.getClass().getClassLoader())
这个currentLoader自然是this.getClass().getClassLoader()。