Understanding the Java classloader part.1 (understanding the classloader in Java)

Source: Internet
Author: User

I found a very old article, but I have a good understanding of classloader.

Original Author: Greg Travis (Mito@panix.com)

 

Start:

This tutorial provides an overview of the Java classloader and takes you through
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
Do to create your own.

This tutorial provides an overview of Java classloader and brings you together to design a class for you before loading your class.
Class Loader example. You will learn what classloader is and how to establish it.
Your own classloader.

Section 1. Tutorial Tips Part 1. The tutorial prompts shocould I take this tutorial? Is this tutorial suitable for me?

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
The system.

Java classloader is a system component during Java runtime, which is very important but often ignored. Its Class is responsible for searching and loading at runtime.
Class file. (Translator's note: It usually refers to the location of other storage files on the hard disk or network, and the class file refers to
The generated file with the class suffix .) Setting up your own classloader allows you to customize JVM rows in more effective and interesting ways.
To allow you to completely redefine how class files are introduced into the system.

 

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
Do to create your own.

This tutorial provides an overview of Java classloader and brings you together to design a class for you before loading your class.
Class Loader example. You will learn what classloader is and how to establish it.
Your own 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
The class file paradigm is sufficient background to take this tutorial.

To smoothly read and understand the content of this tutorial, you only need to have the basic knowledge of Java programming, including creation, compilation, and running simplicity.
A single command line Java program, and an example of understanding the class is enough.

 

Upon completion of this tutorial, you will know how:
• 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

After reading this tutorial, you will know how:

• Extend JVM Functions
• Create a custom classloader
• Learn How to Integrate custom calssloader into your Java application
• Modify Your classloader to adapt to Java 2

 

Section 2. Introduction/getting started what is a classloader? /What is classloader?

Among extends cially 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.

Among the popular commercial programming languages, Java is unique for its Java Virtual Machine (JVM. This means
Compiled programs are compiled into a special platform-independent format, rather than running languages recognized by their machines.
This format is different from the format of traditional programs.

In particle, a Java program, unlike one written in C or C ++, isn' t a single
Executable File, but instead is composed of customized individual class files, each of which
Corresponds to a single Java class.

Specifically, a Java program is different from a program written in C or C ++. It is not a separate executable file,
It is composed of many independent class files, and each class file corresponds to a corresponding Java class.

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
JVM that loads classes into memory.

In addition, these class files are not loaded into the memory at a time, but are loaded only when the program needs them.
As a part of the JVM, classloader loads classes into the memory.

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.

In addition, Java classloader is written in Java. This means that you don't have to understand more details about JVM.
Create your own classloader.

 

Why write a classloader? /Why write a classloader by yourself?

If the JVM has a classloader, Then why wowould 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.

Since the JVM already has a classloader, why do you want to write another one by yourself? Good question. Default
Classloader can only load class files from the local file system. Generally, when your Java program is fully deployed in your
On the computer, this classloader is enough.

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.

However, the most innovative aspect of Java is that it can easily enable JVM from other places except local hard disks and networks.
Obtain a class. For example, a browser can load executable content from a web site through a custom classloader.

There are using other ways to get class files. Besides simply loading files from
Local disk or from a network, you can use a custom classloader:
• 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.

There are also many other get classes that are sent. In addition to getting classes from a local hard disk or network, you can use the custom classloader
You can also:

  • Automatically verifies the digital signature before running the unauthenticated code
  • Use the password provided by the user to decrypt the code
  • Automatically compile the class,

All code that you can think of and generate Java bytecode can be integrated into your application.

M classloader examples/custom classloaer example

If you 've ever used the appletviewer has ded In the JDK or any Java-Enabled
Browser, you 've almost certainly used a custom classloader.

If you have used appletviewer in JDK or browser, you have already used a custom 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
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
HTTP connection from a distant web server.

When sun first launched the Java language, one of the most exciting things was to see this new technology
The code is dynamically loaded and executed from a remote web server during runtime. (Before we realize something more exciting --
Java is a powerful programming language ). You can run the bytecode sent from the remote web server over the HTTP connection.
It was very exciting at that time.

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
Raw bytecode files via HTTP, and turns them into classes inside the JVM.

It is precisely because the Java language can customize this feature of classloader that makes this technology a reality. Classloader in appletviewer
Instead of reading classes from the local file system, the original bytecode is loaded through an HTTP connection by accessing a remote web site.
And convert them to the classes used in 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
Each Other.

Classloader in the browser and appletviewers can also do some other work:
Focus on security and ensure that different applets are used on different pages to avoid mutual interference between them.

Echidnaby Luke gorrie is an open-source software package that allows you
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.

The echidna developed by Luke gorrie is an open-source software that allows you to run multiple Java applications in a single virtual machine.
Package. (See further reading and references.) It uses a custom classloader
The application loads copies of its own class files to prevent interference between applications.

 

Our example classloader/our classloader example

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.

After you have a good understanding of the working mechanism of classloader and how to implement a custom classloader, we will
Create our own custom classloader named compilingclassloader (CCL). CCL edit Java
Code, that is, we no longer have to take charge of compilation by ourselves. It's like there is a "make" program for us
Compile the code and load it into the runtime system.

Note: Before we go any further, it's important to note that some aspects of
Classloader system have been improved in JDK version 1.2 (also known as
Java 2 platform). This tutorial was written with JDK versions 1.0 and 1.1 in mind,
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.

Tip: Before we proceed, there are some notes. Classloader mechanism after JDK 1.2
Improvements were made. This tutorial uses JDK 1.1 and, but the code is also applicable in later versions.

Classloader has changed in Java2. We provide some details to modify our classloader to make better use of it.
The benefits of this change.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.