什么是Java ClassLoader? [英] What is a Java ClassLoader?

查看:92
本文介绍了什么是Java ClassLoader?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一些简单的句子中,什么是Java ClassLoader,何时使用以及为什么?

In a few simple sentences, what is a Java ClassLoader, when is it used and why?

好的,我读了一篇wiki文章。 ClassLoader加载类。好。因此,如果我包含jar文件并导入,则ClassLoader可以完成这项任务。

OK, I read a wiki article. ClassLoader loads classes. OK. So if I include jar files and import, a ClassLoader does the job.

为什么我要打扰这个ClassLoader?我从来没有使用它,也不知道它存在。

Why should I bother with this ClassLoader? I've never used it and didn't know it existed.

问题是,为什么ClassLoader类存在?而且,你如何在实践中使用它? (案件存在,我知道。)

The question is, why does the ClassLoader class exist? And also, how do you use it in practice? (Cases exist, I know.)

推荐答案

取自这个漂亮的教程

用静态编译的编程语言(如C和C ++)编写的应用程序被编译为本机特定于机器的指令并保存为可执行文件。将代码组合成可执行本机代码的过程称为链接 - 将单独编译的代码与共享库代码合并以创建可执行应用程序。这在动态编译的编程语言(例如Java)中是不同的。在Java中,Java编译器生成的.class文件保持原样,直到加载到Java虚拟机(JVM)中 - 换句话说,链接过程由JVM在运行时执行。类根据需要加载到JVM中。当加载的类依赖于另一个类时,也会加载该类。

Applications written in statically compiled programming languages, such as C and C++, are compiled into native, machine-specific instructions and saved as an executable file. The process of combining the code into an executable native code is called linking - the merging of separately compiled code with shared library code to create an executable application. This is different in dynamically compiled programming languages such as Java. In Java, the .class files generated by the Java compiler remain as-is until loaded into the Java Virtual Machine (JVM) -- in other words, the linking process is performed by the JVM at runtime. Classes are loaded into the JVM on an 'as needed' basis. And when a loaded class depends on another class, then that class is loaded as well.

启动Java应用程序时,要运行的第一个类(或入口点)进入应用程序)是一个名为main()的public static void方法。这个类通常有对其他类的引用,所有加载引用类的尝试都是由类加载器执行的。

When a Java application is launched, the first class to run (or the entry point into the application) is the one with public static void method called main(). This class usually has references to other classes, and all attempts to load the referenced classes are carried out by the class loader.

要感觉这个递归类加载为以及一般的类加载想法,请考虑以下简单类:

To get a feeling of this recursive class loading as well as the class loading idea in general, consider the following simple class:

public class HelloApp {
   public static void main(String argv[]) {
      System.out.println("Aloha! Hello and Bye");
   }
}

如果你运行这个指定-verbose:class的类命令行选项,以便它打印正在加载的类,您将获得如下所示的输出。请注意,这只是部分输出,因为列表太长而无法在此处显示。

If you run this class specifying the -verbose:class command-line option, so that it prints what classes are being loaded, you will get an output that looks as follows. Note that this is just a partial output since the list is too long to show here.

prmpt>java -verbose:class HelloApp



[Opened C:\Program Files\Java\jre1.5.0\lib\rt.jar]
[Opened C:\Program Files\Java\jre1.5.0\lib\jsse.jar]
[Opened C:\Program Files\Java\jre1.5.0\lib\jce.jar]
[Opened C:\Program Files\Java\jre1.5.0\lib\charsets.jar]
[Loaded java.lang.Object from shared objects file]
[Loaded java.io.Serializable from shared objects file]
[Loaded java.lang.Comparable from shared objects file]
[Loaded java.lang.CharSequence from shared objects file]
[Loaded java.lang.String from shared objects file]
[Loaded java.lang.reflect.GenericDeclaration from shared objects file]
[Loaded java.lang.reflect.Type from shared objects file]
[Loaded java.lang.reflect.AnnotatedElement from shared objects file]
[Loaded java.lang.Class from shared objects file]
[Loaded java.lang.Cloneable from shared objects file]
[Loaded java.lang.ClassLoader from shared objects file]
[Loaded java.lang.System from shared objects file]
[Loaded java.lang.Throwable from shared objects file]
.
.
.
[Loaded java.security.BasicPermissionCollection from shared objects file]
[Loaded java.security.Principal from shared objects file]
[Loaded java.security.cert.Certificate from shared objects file]
[Loaded HelloApp from file:/C:/classes/]
Aloha! Hello and Bye
[Loaded java.lang.Shutdown from shared objects file]
[Loaded java.lang.Shutdown$Lock from shared objects file]

如您所见,首先加载应用程序类(HelloApp)所需的Java运行时类。

As you can see, the Java runtime classes required by the application class (HelloApp) are loaded first.

Java编程语言不断发展,使应用程序开发人员的日常生活更加轻松。这是通过提供简化生活的API来实现的,它允许您专注于业务逻辑而不是基本机制的实现细节。最近将J2SE 1.5更改为J2SE 5.0以反映Java平台的成熟度就是明证。

The Java programming language keeps evolving to make the life of applications developers easier everyday. This is done by providing APIs that simplify your life by allowing you to concentrate on business logic rather than implementation details of fundamental mechanisms. This is evident by the recent change of J2SE 1.5 to J2SE 5.0 in order to reflect the maturity of the Java platform.

从JDK 1.2开始,引导类加载器是内置于JVM中负责加载Java运行时的类。此类加载器仅加载在引导类路径中找到的类,并且由于这些是受信任的类,因此不会对不受信任的类执行验证过程。除了引导类加载器之外,JVM还有一个扩展类加载器,负责从标准扩展API加载类,以及一个系统类加载器,它从一般类路径和应用程序类加载类。

As of JDK 1.2, a bootstrap class loader that is built into the JVM is responsible for loading the classes of the Java runtime. This class loader only loads classes that are found in the boot classpath, and since these are trusted classes, the validation process is not performed as for untrusted classes. In addition to the bootstrap class loader, the JVM has an extension class loader responsible for loading classes from standard extension APIs, and a system class loader that loads classes from a general class path as well as your application classes.

由于有多个类加载器,它们在树中表示,其根是引导类加载器。每个类加载器都有对其父类加载器的引用。当要求类加载器加载类时,它会在尝试加载项本身之前查询其父类加载器。父母反过来咨询其父母,依此类推。所以只有在所有祖先类加载器都找不到当前类加载器涉及的类之后。换句话说,使用委托模型。

Since there is more than one class loader, they are represented in a tree whose root is the bootstrap class loader. Each class loader has a reference to its parent class loader. When a class loader is asked to load a class, it consults its parent class loader before attempting to load the item itself. The parent in turn consults its parent, and so on. So it is only after all the ancestor class loaders cannot find the class that the current class loader gets involved. In other words, a delegation model is used.

java.lang.ClassLoader 是一个抽象类,可以由需要扩展JVM动态加载类的方式的应用程序进行子类化。 java.lang.ClassLoader (及其子类)中的构造函数允许您在实例化新的类加载器时指定父级。如果未明确指定父级,则将将虚拟机的系统类装入器指定为默认父级。换句话说,ClassLoader类使用委托模型来搜索类和资源。因此,ClassLoader的每个实例都有一个关联的父类加载器,因此当请求查找一个或多个类时,在尝试查找类或资源本身之前,该任务将委托给其父类加载器。 ClassLoader的 loadClass()方法在调用加载类时按顺序执行以下任务:

The java.lang.ClassLoader is an abstract class that can be subclassed by applications that need to extend the manner in which the JVM dynamically loads classes. Constructors in java.lang.ClassLoader (and its subclasses) allow you to specify a parent when you instantiate a new class loader. If you don't explicitly specify a parent, the virtual machine's system class loader will be assigned as the default parent. In other words, the ClassLoader class uses a delegation model to search for classes and resources. Therefore, each instance of ClassLoader has an associated parent class loader, so that when requested to find a class or resources, the task is delegated to its parent class loader before attempting to find the class or resource itself. The loadClass() method of the ClassLoader performs the following tasks, in order, when called to load a class:

如果已经加载了一个类,则返回它。
否则,它将搜索新类委托给父类加载器。
如果父类加载器找不到类, loadClass()调用方法 findClass()查找并加载课程。
如果父类加载器找不到类, finalClass()方法将在当前类加载器中搜索该类。

If a class has already been loaded, it returns it. Otherwise, it delegates the search for the new class to the parent class loader. If the parent class loader doesn't find the class, loadClass() calls the method findClass() to find and load the class. The finalClass() method searches for the class in the current class loader if the class wasn't found by the parent class loader.

原始文章中还有更多内容,它还向您展示了如何实现自己的网络类加载器,它可以回答您关于原因(以及如何)的问题)。另请参阅 API文档

There's more in the original article, which also shows you how to implement your own network class loaders, which answers your question as to why (and how). See also the API docs.

这篇关于什么是Java ClassLoader?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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