Understanding the Java ClassLoader part.1 (认识JAVA中的ClassLoader )

来源:互联网 发布:戴笠 毛人凤 知乎 编辑:程序博客网 时间:2024/04/27 01:27

找到了一篇很古老的文章,但是用作初步对ClassLoader的了解很不错。

原作者:Greg Travis (mito@panix.com)

 

开始:

This tutorial provides an overview of the Java ClassLoader and takes you through the
construction of an example ClassLoader that automatically compiles your code
before loading it. You'll learn exactly what a ClassLoader does, and what you need to
do to create your own.

这篇教程对Java ClassLoader进行了概述,并且带你一同设计一个可以在加载你的类之前对你
的类进行自动编译的Class Loader示例。你将学习到ClassLoader到底是做什么的和怎么建立
你自己的ClassLoader。

Section 1. Tutorial tips 第一部分. 教程提示

Should I take this tutorial?  这篇教程适合我吗

The Java ClassLoader is a crucial, but often overlooked, component of the Java
run-time system. It is the class responsible for finding and loading class files at run
time. Creating your own ClassLoader lets you customize the JVM in useful and
interesting ways, allowing you to completely redefine how class files are brought into
the system.

Java ClassLoader 是Java运行时系统组件,十分重要但是常常被忽视。它的类负责在运行时查找并加载
class文件。(译者注:通常指查找硬盘或者网络上的其他存储文件位置,class文件指Java源文件编译后
生成的class后缀的文件。)建立你自己的ClassLoader可以让你利用更有效和有意思的方法定制JVM的行
为,允许你完全重定义class文件是如何被引入系统中的。

 

This tutorial provides an overview of the Java ClassLoader and takes you through
the construction of an example ClassLoader that automatically compiles your code
before loading it. You'll learn exactly what a ClassLoader does and what you need to
do to create your own.

这篇教程对Java ClassLoader进行了概述,并且带你一同设计一个可以在加载你的类之前对你
的类进行自动编译的Class Loader示例。你将学习到ClassLoader到底是做什么的和怎么建立
你自己的ClassLoader。

 

A basic understanding of Java programming, including the ability to create, compile,
and execute simple command-line Java programs, as well as an understanding of
the class file paradigm is sufficient background to take this tutorial.

为了顺利阅读理解此片教程内容,你只要具备Java编程的基础知识,包括创建、编译、运行简
单的命令行Java程序,外加了解类的范例就足够了。

 

Upon completion of this tutorial, you will know how to:
• Expand the functionality of the JVM
• Create a custom ClassLoader
• Learn how to integrate a custom ClassLoader into your Java application
• Modify your ClassLoader to accommodate the Java 2 release

读完这篇教程,你将会知道如何:

• 扩展JVM的功能
• 建立一个自定义的ClassLoader
• 学习如何将自定义的CalssLoader整合到你的Java应用中去
• 修改你的ClassLoader以便适应Java 2版本

 

Section 2. Introduction / 入门

What is a ClassLoader? / ClassLoader是什么?

Among commercially popular programming languages, the Java language
distinguishes itself by running on a Java virtual machine (JVM). This means that
compiled programs are expressed in a special, platform-independent format, rather
than in the format of the machine they are running on. This format differs from
traditional executable program formats in a number of important ways.

在现在流行的商业化编程语言中,Java因它的Java虚拟机(JVM)而独树一帜。这意味着
编译的程序被编译为一种特殊的与平台无关的格式,而不是运行他们的机器所识别的语言。
这种格式与传统程序的格式有许多不同的地方。

In particular, a Java program, unlike one written in C or C++, isn't a single
executable file, but instead is composed of many individual class files, each of which
corresponds to a single Java class.

特别的一点,一个Java程序与C或者C++写出的程序不同,它不是一个单独的可执行文件,
而是由许多独立的类文件组成,每个类文件对应了一个相应的Java类。

Additionally, these class files are not loaded into memory all at once, but rather are
loaded on demand, as needed by the program. The ClassLoader is the part of the
JVM that loads classes into memory.

另外,这些类文件并不是一次性被加载到内存中,而是当程序需要的时候才被加载。
ClassLoader作为JVM的一部分,它的任务就是将类加载到内存中。

The Java ClassLoader, furthermore, is written in the Java language itself. This
means that it's easy to create your own ClassLoader without having to understand
the finer details of the JVM.

并且 Java ClassLoader 是用Java语言编写的。这意味着你不用去理解JVM的更多细节便可以轻松
的创建你自己的ClassLoader。

 

Why write a ClassLoader? / 为什么要自己写一个ClassLoader?

If the JVM has a ClassLoader, then why would you want to write another one? Good
question. The default ClassLoader only knows how to load class files from the local
filesystem. This is fine for regular situations, when you have your Java program fully
compiled and waiting on your computer.

既然JVM已经有了一个ClassLoader,那为什么你会想自己另外写一个呢?好问题。默认的
ClassLoader只可以从本地文件系统加载类文件。通常情况下当你的Java程序完全部署在你的
计算机上,这样的ClassLoader就足够了。

But one of the most innovative things about the Java language is that it makes it
easy for the JVM to get classes from places other than the local hard drive or
network. For example, browsers use a custom ClassLoader to load executable
content from a Web site.

但是Java最为创新的一点就是它可以很容易使JVM可以从除了本地硬盘以及网络以外的其他地方
获取类。比如,浏览器可以通过自定义的ClassLoader从一个Web站点加载可运行的内容。

There are many other ways to get class files. Besides simply loading files from the
local disk or from a network, you can use a custom ClassLoader to:
• Automatically verify a digital signature before executing untrusted code
• Transparently decrypt code with a user-supplied password
• Create dynamically built classes customized to the user's specific needs
Anything you can think of to write that can generate Java bytecode can be integrated
into your application.

还有许多其他获取类的发那个是。除了简单的从本地硬盘或者网络获取类,你通过自定义ClassLoader
还可以:

  • 在运行未认证的代码前自动验证数字签名
  • 通过用户提供的密码进行代码解密
  • 根据用户的特殊需要在加载类前自动编译类、

一切你能想到并可以实现生成Java字节码的代码,都可以整合到你的应用中去

Custom ClassLoader examples / 自定义ClassLoaer示例

If you've ever used the appletviewer included in the JDK or any Java-enabled
browser, you've almost certainly used a custom ClassLoader.

如果你曾经用过JDK或者浏览器中的appletviewer ,你其实已经是用过自定义ClassLoader了。

When Sun initially released the Java language, one of the most exciting things was
watching how this new technology executed code that it had loaded on the fly from a
remote Web server. (This was before we'd realized something more exciting -- that
Java technology provided a great language for writing code.) There was just
something thrilling about it executing bytecode that had just been sent through an
HTTP connection from a distant Web server.

当Sun公司最初发布Java这门语言时,其中一个最令人兴奋的事情就是看到这个新技术可以在
运行时从一个远程的Web服务器动态加载代码并执行。(这是在我们意识到更令人兴奋的事情之前--
Java是一门强大的编程语言
)。可以在运行刚刚从远程Web服务器通过HTTP连接发送过来的字节码
在那时候是一件非常令人激动的事。

What made this feat possible was the ability of the Java language to install a custom
ClassLoader. The appletviewer contains a ClassLoader that, instead of looking in
the local filesystem for classes, accesses a Web site on a remote server, loads the
raw bytecode files via HTTP, and turns them into classes inside the JVM.

正是因为Java语言可以自定义ClassLoader的这个特性,才使这一技术成为现实。appletviewer中的ClassLoader
替代了传统的从本地文件系统读取类的方式,转而使用通过访问远程的Web站点,通过HTTP连接加载原始字节码
文件,并且将它们转换为JVM中所使用的类。

The ClassLoaders in browsers and appletviewers do other things as well: they take
care of security and keep different applets on different pages from interfering with
each other.

浏览器与appletviewers中的ClassLoader还可以做一些其他工作:
关注安全方面和保证不同页面中使用不同的applets以避免他们之间的互相干扰。

Echidnaby Luke Gorrie is an open-source software package that allows you to
safely run multiple Java applications inside a single virtual machine. (See Further
reading and references. ) It uses a custom ClassLoader to prevent the applications
from interfering with each other, by giving each application its own copy of the class
files.

Luke Gorrie开发的Echidna 是一个可以让你你在一个单独的虚拟机中运行多个Java应用的开源软
件包。(See Further reading and references.)它使用了一个自定义的ClassLoader,使每个
应用加载属于自己的类文件副本,以防止应用间互相的干扰。

 

Our example ClassLoader / 我们的ClassLoader示例

After you have a good idea of how a ClassLoader works and how one is written, we'll
create our own custom ClassLoader called CompilingClassLoader (CCL). CCL
compiles our Java code for us, in case we didn't bother to do it ourselves. It's
basically like having a simple "make" program built directly into our run-time system.

在你对ClassLoader的工作机制以及如何实现自定义的ClassLoader有个很好的了解后,我们将
创建我们自己的定制ClassLoader,名字叫
CompilingClassLoader (CCL).CCL为我们编辑Java
代码,也就是说我们不必再自己去负责编译的工作了。就像是有一个“make”程序为我们
编译代码再将其加载到运行时系统中。

Note: Before we go any further, it's important to note that some aspects of the
ClassLoader system have been improved in JDK version 1.2 (also known as the
Java 2 platform). This tutorial was written with JDK versions 1.0 and 1.1 in mind, but
everything in it works under later versions as well.
ClassLoader changes in Java 2 describes the changes in Java version 1.2 and
provides details for modifying our ClassLoader to take advantage of these changes.

提示:在我们继续往下进行以前,有一些需要注意的地方。ClassLoader机制在JDK 1.2版本后
进行了改进。这篇教程是使用JDK1.0和1.1版本,但是所写的代码在后期更新版本中同样适用。

ClassLoader在Java2中有所变化,我们提供了一些细节来修改我们的ClassLoader来更好的利用
这变化带来的优势。