java类加载(二)ClassLoader及相关方法

(一)获取ClassLoader
(1)BootStrapClassLoader == null
ClassLoader.getSystemClassLoader().getParent().getParent();

(2)ExtClassLoader
ClassLoader.getSystemClassLoader().getParent()

(3)SystemClassLoader
//委托至sun.misc.Launcher.getLauncher().getClassLoader();(安全检查)
ClassLoader.getSystemClassLoader()

(4)通过Thread获取ClassLoader,具体细节待理
Thread.currentThread().getContextClassLoader();

该方法同样也是取自Launcher,源头: http://javasourcecode.org/html/open-source/jdk/jdk-6u23/sun/misc/Launcher.java.html#line.69
    // Also set the context class loader for the primordial thread.
    Thread.currentThread().setContextClassLoader(loader);

引用

Returns the parent class loader for delegation. Some implementations may use null to represent the bootstrap class loader. This method will return null in such implementations if this class loader's parent is the bootstrap class loader.

If a security manager is present, and the invoker's class loader is not null and is not an ancestor of this class loader, then this method invokes the security manager's checkPermission method with a RuntimePermission("getClassLoader") permission to verify access to the parent class loader is permitted. If not, a SecurityException will be thrown.

你可能感兴趣的:(java)