Fassen Sie den Java-Klassenlader zusammen.
boostrap class loader
jre / lib / rt.jar
oder jre / lib /
unter .jar vorhanden ist./Library/Java/JavaVirtualMachines/jdk1.x.x_xxx.jdk/Contents/Home/jre/lib/rt.jar
etc.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
Example.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 (). getParent ()
zeigt, dass sun.misc.Launcher $ ExtClassLoader
das übergeordnete Element istexample/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