不说废话,上代码吧。
随便写一个类,他是由appclassLoader加载的
package classLoaderExample; class Bean
{
public void test()
{
System.out.println("hello");
} }
package classLoaderExample; public class Main { public static void main(String[] args) {
System.out.println(System.getProperty("java.class.path"));
Class typeLoaded = null;
try {
typeLoaded = Class.forName("classLoaderExample.Bean");//加载这个类,返回类的实例
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//typeLoaded.getClassLoader();
System.out.println(typeLoaded.getClass());
//打印该类的类加载器
ClassLoader cl=typeLoaded.getClassLoader();
System.out.println(cl);
System.out.println(cl.getParent());
System.out.println(cl.getParent().getParent()); } }