一个简单程序的类加载流程 [英] Flow of class loading for a simple program

查看:102
本文介绍了一个简单程序的类加载流程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始学习Java的内部架构。我已经大致理解了类加载的概念,它在 jvm 运行时加载所需的类, ClassNotFoundException 在类中抛出找不到,特定的类加载器加载类引用的类。

I am just now beginning to learn the internal architecture of Java. I have roughly understood the concept of class loading which loads the required classes when jvm runs, ClassNotFoundException is thrown when a class is not found and specific class loader loads the classes referenced by the class.

有人可以在下面的示例Java代码中清楚地解释类加载的流程,即引导类加载和用户定义的类加载的顺序。

Can someone please explain clearly the flow of class loading i.e. the sequence of bootstrap class loading and user-defined class loading in the sample Java code below.

import java.io.File;
public class Sample
{
    public static void main(String[] args)
    {
        String fileName = "sample";
        File file = new File(fileName);
        file.isFile();
    }
} 

我还从参考资料中了解到 classloader 维护它加载的类的名称空间。通过名称空间,这是否意味着类的文字名称?还有人可以解释一下这个含义/优点吗?

Also I learnt from a reference material that "classloader maintains the namespaces of the classes it loads". By namespaces, does that mean the literal names of the class? Also can someone please explain the implication/advantage of that?

推荐答案

你将运行样本课程如下

> java Sample

for magic magic,查看 -verbose:class 的输出选项,你会看到大量的以下行..

for little magic, check out the output of-verbose:class option and you see tons of following lines..

[Opened C:\jdk1.6.0_14\jre\lib\rt.jar]
[Loaded java.lang.Object from C:\jdk1.6.0_14\jre\lib\rt.jar]
[Loaded java.io.Serializable from C:\jdk1.6.0_14\jre\lib\rt.jar]
[Loaded java.lang.Comparable from C:\jdk1.6.0_14\jre\lib\rt.jar]
.
.
.
.
.
.
[Loaded java.security.cert.Certificate from C:\jdk1.6.0_14\jre\lib\rt.jar]
[Loaded Sample from file:/D:/tmp/]
[Loaded java.lang.Shutdown from C:\jdk1.6.0_14\jre\lib\rt.jar]
[Loaded java.lang.Shutdown$Lock from C:\jdk1.6.0_14\jre\lib\rt.jar]

你看到一堆来自 \ jre \lib \rt.jar 的类被加载,远在你的类加载之前 Bootstrap 类加载器(或Primordial)。这些是运行任何Java程序的先决条件,因此由Bootstrap加载。

You see a bunch of classes from \jre\lib\rt.jar are loaded, much before your class is loaded by Bootstrap class loader (or Primordial ). These are the pre-requisite for running any Java program hence loaded by Bootstrap.

另一组jar由 Extension 类加载器加载。在这个特定的例子中,不需要lib \ jre \lib \ _ext 中的任何类,因此它没有被加载。但Extension类加载器专门分配了从扩展lib加载类的任务。

Another set of jars is loaded by Extension class loader. In this particular example, there was no need of any classes from the lib \jre\lib\ext hence its not loaded. But Extension class loader are specifically assigned the task of loading the classes from the extension lib.

编辑:除了标准平台java类外,Sun / Oracle还提供了一组用于扩展平台的jar核心API 。放置在扩展lib文件夹中的jar自动放在类路径中,因此不需要显式地包含在类路径中。这是关于同一主题的不错的官方文章

Apart from the standard platform java classes Sun/Oracle also provide a set of jars which are used to extend the platform's core API. The jars placed in the extension lib folder are automatically placed in the classpath and hence not needed to be included in classpath explicitly. Here is nice official article on the same topic.

最后,您的班级 Sample Application 加载Bootstrap和Extension之后的类加载器已完成加载。

Finally, your class Sample is loaded by Application class loader after Bootstrap and Extension have finished loading.

这篇关于一个简单程序的类加载流程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆