ITeye博客

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

slendersEye · 2012-09-24 17:15 · java基础 · 编程语言 · Markdown

(一)获取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.

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。