---
title: "java类加载（二）ClassLoader及相关方法"
author: "slendersEye"
date: "2012-09-24T17:15:08Z"
tags: []
categories: ["编程语言"]
slug: "java-classloader"
draft: false
lastmod: "2012-09-24T17:15:08Z"
id: 1684504
url: "https://sleepyy.iteye.com/blog/1684504"
---

（一）获取ClassLoader
(1)BootStrapClassLoader == null

```java

ClassLoader.getSystemClassLoader().getParent().getParent();

```

(2)ExtClassLoader

```java

ClassLoader.getSystemClassLoader().getParent()

```

(3)SystemClassLoader

```java

//委托至sun.misc.Launcher.getLauncher().getClassLoader();（安全检查）
ClassLoader.getSystemClassLoader()

```

(4)通过Thread获取ClassLoader，具体细节待理

```java

Thread.currentThread().getContextClassLoader();

```

该方法同样也是取自Launcher，源头：<http://javasourcecode.org/html/open-source/jdk/jdk-6u23/sun/misc/Launcher.java.html#line.69>

```java

    // 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. 
>