我读了JVM的概念还有类加载器
- 引导类加载器
- 扩展类加载器
- 应用程序类加载器
我理解这三个概念,但我怀疑谁负责加载自己的类。
示例:
import java.lang.*;(引导类加载器)
class MyClass{
public static void main(String args[]){
-----------
----------------
}
}
那么这里谁负责加载MyClass这里
最佳答案
通过排除,它不能是第一个或第二个,因为 MyClass
不是 JRE 类,也不是 JRE 扩展。
因此,MyClass
由应用程序类加载器加载。
要检查这一点,您可以在此类实例上调用 getClassLoader()
方法:
public static void main(String args[]) {
System.out.println(MyClass.class.getClassLoader());
}
它将输出如下内容:
sun.misc.Launcher$AppClassLoader@1d16e93
AppClassLoader
作为名称相当有意义。
您可以在 this Oracle blog 上找到一些引用信息:
3. application class loader
- Loads classes from application classpath
...
- This is the loader that loads your Java application's "main" class (class with main method in it).
In Sun's implementation, this is an instance of sun.misc.Launcher$AppClassLoader (actually it is an inner class of sun.misc.Launcher class).
博客条目很旧,但这一点仍然有效。
关于java - 哪个加载器负责在 JVM 中加载自己的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46871146/