Summarize Java class loader.
boostrap class loader
jre / lib / rt.jar
orjre / lib /
under .jar./Library/Java/JavaVirtualMachines/jdk1.x.x_xxx.jdk/Contents/Home/jre/lib/rt.jar
etc.
--A class loader that comes with the JVM and is used to load classes when the JVM starts.
--The return value of getClassLoader ()
for the class loaded by bootstrap class loader will be null
Example.java
public class Example
{
public static void main(String[] args)
{
System.out.println(String.class.getClassLoader());
}
}
$ javac Example.java && java Example
null
extension class loader
jre / lib / ext
--The return value of getClassLoader ()
for the class loaded by extension class loader will be sun.misc.Launcher $ ExtClassLoader
.
--The return value of getClassLoader (). getParent ()
will be null
, that is, there is no parentExample.java
import sun.net.spi.nameservice.dns.DNSNameService;
public class Example
{
public static void main(String[] args)
{
ClassLoader extLoader = DNSNameService.class.getClassLoader();
System.out.println(extLoader);
System.out.println(extLoader.getParent());
}
}
$ javac Example.java && java Example
sun.misc.Launcher$ExtClassLoader@75b84c92
null
user class loader
getClassLoader ()
for the class loaded by user class loader will be sun.misc.Launcher $ AppClassLoader
--getClassLoader (). getParent ()
reveals that sun.misc.Launcher $ ExtClassLoader
is the parentexample/A.java
package example;
public class A
{
}
Example.java
import example.A;
public class Example
{
public static void main(String[] args)
{
ClassLoader appLoader = Example.class.getClassLoader();
System.out.println(appLoader);
System.out.println(appLoader.getParent());
System.out.println(appLoader.getParent().getParent());
System.out.println();
appLoader = A.class.getClassLoader();
System.out.println(appLoader);
System.out.println(appLoader.getParent());
System.out.println(appLoader.getParent().getParent());
}
}
$ javac example/A.java && jar cf example/A.jar example/A.class
$ javac Example.java && java -classpath example/A.jar:./ Example
sun.misc.Launcher$AppClassLoader@2a139a55
sun.misc.Launcher$ExtClassLoader@75b84c92
null
sun.misc.Launcher$AppClassLoader@2a139a55
sun.misc.Launcher$ExtClassLoader@75b84c92
null
Recommended Posts