In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Today, the editor will share with you the relevant knowledge points about how to write JAVA's classloader. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.
What is ClassLoader?
Among the popular commercial programming languages, the Java language stands out because it runs on the Java Virtual Machine (JVM). This means that compiled programs are a special, platform-independent format that does not depend on the machine on which they are running. To a large extent, this format is different from the traditional executable format.
Unlike programs written by C or C++, Java programs are not an executable file, but are made up of many separate class files, each corresponding to a Java class.
In addition, these class files are not all loaded into memory immediately, but according to the needs of the program. ClassLoader is the part of JVM that loads classes into memory.
Moreover, Java ClassLoader is written in the Java language. This means that creating your own ClassLoader is very easy and you don't have to know the details of JVM.
Why write ClassLoader?
If JVM already has one ClassLoader, why write another one? That's a good question. The default ClassLoader only knows how to mount class files from the local file system. However, this is only suitable for the general situation, that is, all the Java programs have been compiled and the computer is waiting.
But the most innovative thing about the Java language is that JVM can easily get classes from non-local hard drives or from the network. For example, visitors can use custom ClassLoader to load executable content from a web site.
There are many other ways to get class files. In addition to simply loading files locally or on the network, you can use a custom ClassLoader to accomplish the following tasks:
Automatically verify digital signatures before executing unbelievable code
Use the password provided by the user to transparently decrypt the code
Dynamically create custom build classes that meet the specific needs of users
Anything you think can generate Java bytecode can be integrated into your application.
Custom ClassLoader exampl
If you've ever used jdk or any Applet viewer in a Java-based browser, you've almost certainly used a custom ClassLoader.
When Sun first released the Java language, one of the most exciting things was to see how this new technology executes code loaded at run time from a remote Web server. (in addition, there is something even more exciting-Java technology provides a powerful language that is easy to write code. Even more exciting is the ability to execute bytecode sent over a HTTP connection from a remote Web server
This feature is due to the fact that the Java language can install custom ClassLoader. The Applet viewer contains a ClassLoader that does not look for classes in the local file system, but accesses Web sites on remote servers, loads the original bytecode files through HTTP, and converts them into classes within JVM.
ClassLoaders in browsers and Applet viewers can also do other things: they support security and have different Applet run on different pages without interfering with each other.
Echidna written by Luke Gorrie is an open source package that allows you to run multiple Java applications on a single virtual machine. It uses a custom ClassLoader to prevent applications from interfering with each other by providing each application with its own copy of the file.
Our ClassLoader example
Now that we understand how ClassLoader works and how to write ClassLoader, we will create a Classloader called CompilingClassLoader (CCL). CCL compiles the Java code for us without us interfering with the process. It is basically similar to a "make" program that is built directly into the runtime system.
Note: prior to further understanding, it should be noted that some aspects of the ClassLoader system (that is, the Java 2 platform) have been improved in JDK version 1.2. This tutorial is written in JDK versions 1.0 and 1.1, but it can also be run in later versions.
The changes to ClassLoader in Java 2 describe the changes in Java version 1.2 and provide some details to modify ClassLoader to take advantage of these changes.
The basic goal of ClassLoader is to serve requests for classes. When JVM needs to use a class, it requests the class from ClassLoader by name, and ClassLoader attempts to return a Class object that represents the class. You can create a custom ClassLoader by overriding methods that correspond to different stages of the process.
In the rest of this article, you will learn the key methods of Java ClassLoader. You will learn what each method does and how it fits into the process of loading class files. You will also know what code you need to write when you create your own ClassLoader.
In the following sections, you will use this knowledge to use our ClassLoader example, CompilingClassLoader.
Method loadClass
ClassLoader.loadClass () is the entry point for ClassLoader. Its characteristics are as follows:
Class loadClass (String name, boolean resolve)
The name parameter specifies the name of the class required by JVM, which is represented in package notation, such as Foo or java.lang.object. The resolve parameter tells the method whether the class needs to be parsed. Before you prepare to execute a class, you should consider class parsing. You don't always need to parse. If JVM only needs to know whether the class exists or to find out the superclass of the class, there is no need to parse.
In Java version 1.1 and prior, the loadClass method was the only method that needed to be overridden when creating a custom ClassLoader. (changes to ClassLoader in Java 2 provide information about the findClass () method in Java 1.2. )
Method defineClass
The defineClass method is the main knack of ClassLoader. This method takes an array of raw bytes and converts it to a Class object. The original array contains data, such as mounted from a file system or network.
DefineClass manages many complex, mysterious, and implementation-dependent aspects of JVM-- it analyzes bytecode into run-time data structures, validates validity, and so on. Don't worry, you don't have to write it yourself. In fact, you can't overwrite it even if you want to, because it has been marked as final.
Method findSystemClass
The findSystemClass method mounts files from the local file system. It looks for a class file in the local file system and, if it exists, uses defineClass to convert the original bytes into Class objects to convert the file into a class. This is the default mechanism for JVM to load classes normally when running Java applications. (the changes to ClassLoader in Java 2 provide more information about the process changes in Java version 1.2. )
For a custom ClassLoader, use findSystemClass only after trying other methods to load the class. The reason is simple: ClassLoader is responsible for the special steps of loading classes, not for all classes. For example, even if ClassLoader loads some classes from a remote Web site, you still need to load a large number of basic Java libraries on the local machine. These classes are not our concern, so JVM loads them by default: from the local file system. This is what findSystemClass is for.
The workflow is as follows:
Request a custom ClassLoader load class.
Check the remote Web site to see if there are any required classes.
If so, then good; grab this class and complete the task.
If not, assuming that the class is in the base Java library, call findSystemClass to load the class from the file system.
In most custom ClassLoaders, call findSystemClass first to save the time it takes to find many Java library classes that can be loaded locally on a remote Web site. However, as you'll see in the next section, we don't let JVM load classes from the local file system until we're sure that our application code will be compiled automatically.
Method resolveClass
As mentioned earlier, classes can be loaded either incompletely (without parsing) or completely (with parsing). When writing our own loadClass, we can call resolveClass, depending on the value of the resolve parameter of the loadClass.
Method findLoadedClass
FindLoadedClass acts as a cache: when loadClass is asked to load a class, it calls this method to see if ClassLoader has loaded the class, thus avoiding the hassle of reloading existing classes. The method should be called first.
Assembly
Let's look at how to assemble all the methods.
Our loadClass implementation example performs the following steps. (here, we do not specify which technique is used to generate the class file-- it can be loaded from .NET, extracted from an archive, or compiled in real time. Either way, it's a special magic way to get the bytes of the original class file. )
CCL disclosure
The task of our ClassLoader (CCL) is to ensure that the code is compiled and updated.
Here's how it works:
When requesting a class, first check to see if it is in the current directory of the disk or the corresponding subdirectory.
If the class does not exist but is in the source code, the Java compiler is called to generate the class file.
If the class already exists, check that it is older than the source code. If so, call the Java compiler to regenerate the class file.
If the compilation fails, or if the class file cannot be generated from the existing source code for some other reason, return ClassNotFoundException.
If you still don't have this class, maybe it's in another library, so call findSystemClass to find the class.
If still not, ClassNotFoundException is returned.
Otherwise, the class is returned.
Call findLoadedClass to see if there is a loaded class.
If not, then use that special magic way to get the original bytes.
If you already have the original bytes, call defineClass to convert them into Class objects.
If there are no original bytes, then call findSystemClass to see if the class is obtained from the local file system.
If the resolve parameter is true, then call resolveClass to parse the Class object.
If there is no class already, return ClassNotFoundException.
Otherwise, the class is returned to the caller.
How Java compilation works
Before going any further, you should take a step back and talk about Java compilation. In general, the Java compiler does not just compile the classes that you ask it to compile. It also compiles other classes if they are required by the class you require to compile.
CCL compiles each class in the application that needs to be compiled one by one. But in general, after the compiler compiles the first class, CCL looks for all the classes that need to be compiled, and then compiles it. Why? The Java compiler is similar to the rule we are using: if the class does not exist, or if it is older than its source code, then it needs to be compiled. In fact, the Java compiler does most of the work in a step before CCL.
When CCL compiles them, it reports the class on which application it is compiling. In most cases, CCL invokes the compiler on the main class in the program, and it does all that needs to be done-- a single call from the compiler is sufficient.
However, there is a situation where some classes are not compiled in the first step. If you use the Class.forName method to load the class by name, the Java compiler will not know what the class needs. In this case, you will see that CCL runs the Java compiler again to compile the class. This process is demonstrated in the source code.
Use CompilationClassLoader
To use CCL, the program must be called in a special way. Cannot run the program directly, such as:% java Foo arg1 arg2
It should be run in the following ways:
% java CCLRun Foo arg1 arg2
CCLRun is a special stub program that creates CompilingClassLoader and uses it to load the main class of the program to ensure that the entire program is loaded through CompilingClassLoader. CCLRun uses Java Reflection api to call the main method of a particular class and pass parameters to it. For more information, see the source code.
Run the sample
The source code includes a set of small classes that demonstrate how it works. The main program is the Foo class, which creates an instance of the class Bar. The class Bar creates another instance of the class Baz, which is in the baz package, to show how CCL handles the code in the subpackage. Bar is also loaded by name, which is called Boo, which is used to show that it can also work with CCL.
Each class declares that it is loaded and running. Now try it with the source code. Compile CCLRun and CompilingClassLoader. Be sure not to compile other classes (Foo, Bar, Baz, and Boo), otherwise CCL will not be used because these classes have already been compiled.
% java CCLRun Foo arg1 arg2
CCL: Compiling Foo.java...
Foo! Arg1 arg2
Bar! Arg1 arg2
Baz! Arg1 arg2
CCL: Compiling Boo.java...
Boo!
Notice that the compiler, Foo.java, manages Bar and baz.Baz first. Until Bar loads Boo by name, it is called, and CCL calls the compiler again to compile it.
Foo.java
The following is the source code for Foo.java
/ / $Id$ public class Foo {static public void main (String args []) throws Exception {System.out.println ("foo!" + args [0] + "+ args [1]); new Bar (args [0], args [1]);}}
Bar.java
The following is the source code for Bar.java
/ / $Id$ import baz.*; public class Bar {public Bar (String a, String b) {System.out.println ("bar!" + a + "" + b); new Baz (a, b); try {Class booClass = Class.forName ("Boo"); Object boo = booClass.newInstance ();} catch (Exception e) {e.printStackTrace ();}
Baz/Baz.java
The following is the source code for baz/Baz.java
/ / $Id$ package baz; public class Baz {public Baz (String a, String b) {System.out.println ("baz!" + a + "" + b);}}
Boo.java
The following is the source code for Boo.java
/ / $Id$ public class Boo {public Boo () {System.out.println ("Boo!");}}
These are all the contents of the article "how to write JAVA's classloader". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.