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

找到了一篇很古老的文章,但是用作初步对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来更好的利用
这变化带来的优势。

Python网络爬虫与推荐算法新闻推荐平台:网络爬虫:通过Python实现新浪新闻的爬取,可爬取新闻页面上的标题、文本、图片、视频链接(保留排版) 推荐算法:权重衰减+标签推荐+区域推荐+热点推荐.zip项目工程资源经过严格测试可直接运行成功且功能正常的情况才上传,可轻松复刻,拿到资料包后可轻松复现出一样的项目,本人系统开发经验充足(全领域),有任何使用问题欢迎随时与我联系,我会及时为您解惑,提供帮助。 【资源内容】:包含完整源码+工程文件+说明(如有)等。答辩评审平均分达到96分,放心下载使用!可轻松复现,设计报告也可借鉴此项目,该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的。 【提供帮助】:有任何使用问题欢迎随时与我联系,我会及时解答解惑,提供帮助 【附带帮助】:若还需要相关开发工具、学习资料等,我会提供帮助,提供资料,鼓励学习进步 【项目价值】:可用在相关项目设计,皆可应用在项目、毕业设计、课程设计、期末/期/大作业、工程实训、大创等学科竞赛比赛、初期项目立项、学习/练手等方面,可借鉴此优质项目实现复刻,设计报告也可借鉴此项目,也可基于此项目来扩展开发出更多功能 下载后请首先打开README文件(如有),项目工程可直接复现复刻,如果基础还行,也可在此程序基础上进行修改,以实现其它功能。供开源学习/技术交流/学习参考,勿用于商业用途。质量优质,放心下载使用。
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
各种安全相关思维导图整理收集。渗透步骤,web安全,CTF,业务安全,人工智能,区块链安全,数据安全,安全开发,无线安全,社会工程学,二进制安全,移动安全,红蓝对抗,运维安全,风控安全,linux安全.zip项目工程资源经过严格测试可直接运行成功且功能正常的情况才上传,可轻松复刻,拿到资料包后可轻松复现出一样的项目,本人系统开发经验充足(全领域),有任何使用问题欢迎随时与我联系,我会及时为您解惑,提供帮助。 【资源内容】:包含完整源码+工程文件+说明(如有)等。答辩评审平均分达到96分,放心下载使用!可轻松复现,设计报告也可借鉴此项目,该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的。 【提供帮助】:有任何使用问题欢迎随时与我联系,我会及时解答解惑,提供帮助 【附带帮助】:若还需要相关开发工具、学习资料等,我会提供帮助,提供资料,鼓励学习进步 【项目价值】:可用在相关项目设计,皆可应用在项目、毕业设计、课程设计、期末/期/大作业、工程实训、大创等学科竞赛比赛、初期项目立项、学习/练手等方面,可借鉴此优质项目实现复刻,设计报告也可借鉴此项目,也可基于此项目来扩展开发出更多功能 下载后请首先打开README文件(如有),项目工程可直接复现复刻,如果基础还行,也可在此程序基础上进行修改,以实现其它功能。供开源学习/技术交流/学习参考,勿用于商业用途。质量优质,放心下载使用。
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值