The difference between Java and other languages is that Java runs on the Java Virtual Machine (JVM). This means that the compiled code is saved in a platform-independent format, rather than a format that runs on a specific machine. This format has many important differences from the traditional executable code format. Specifically, unlike a C or C++ program, a Java program is not an independent executable file, but consists of many separate class files, each class file corresponding to a Java class. In addition, these class files are not loaded into memory immediately, but are loaded when the program needs them. A class loader is a tool used in the Java virtual machine to load classes into memory. Moreover, the Java class loader is also implemented in Java. This way you can easily create your own class loader without having an in-depth understanding of the Java virtual machine.
Why create a class loader?
Now that the Java virtual machine already has a class loader, do we need to create others ourselves? Good question. The default class loader only knows how to load classes from the local system. When your program is compiled entirely natively, the default class loader generally works well. But one of the most exciting things about Java is how easy it is to load classes from the network instead of just locally.
For example, a browser can load classes through a custom class loader. There are also many ways to load classes. One of the most exciting things about Java is that you can customize it besides simply from local or network:
* Automatically verify digital signatures before executing untrusted code
* Decrypt the code based on the password provided by the user
* Dynamically create classes according to user needs. Anything you care about can be easily integrated into your application in the form of bytecode. Examples of custom class loaders if you have used JDK (Java Software Development Kit) appletviewer (small application browser) or other
For Java embedded browsers, you already use a custom class loader. When Sun first released the Java language, one of the most exciting things was watching how Java executed code downloaded from a remote website. Execute from remote site via HTTP
The bytecode transmitted by the P connection looks a bit weird. This works because Java has the ability to install custom class loaders. The applet browser contains a class loader. This class loader does not find Java classes locally. Instead, it accesses the remote server, loads the original bytecode file through HTTP, and then converts it into a Java class in the Java virtual machine. Of course class loaders do a lot of other things: they block unsafe Java classes and keep different applets on different pages from interfering with each other. Echidna, a package written by Luke Gorrie, is an open Java software package that allows multiple Java applications to be safely run in a Java virtual machine. It prevents interference between applications by using a custom class loader to give each application a copy of the class file.
java class loader:
There are three class loaders by default in Java: bootstrap class loader, extension class loader, and system class loader (also called application class loader)
Class loaders are one of Java's most powerful features. But developers often forget to classload components. A class loader is a class responsible for finding and loading class files at runtime. Java allows the use of different class loaders, even custom class loaders.
Java programs contain many class files, each corresponding to a single Java class. Unlike static C programs, these class files are loaded into memory once and they need to be loaded at any time. This is what makes class loaders different. It obtains platform-independent bytecodes from source files (usually .class or .jar files) and then loads them into the JVM memory space so they can be interpreted and executed. By default, each class of an application is loaded by java.lang.ClassLoader. Because it can be inherited, its functionality can be freely enhanced.
Custom class loader
import java.io.*;
import java.net.*;
import java.util.*;
import java.lang.reflect.Method;
public class CustomClassLoader extends URLClassLoader {
private FileInputStream input = null; //File input stream
private ByteArrayOutputStream out = null; //byte array output stream
private String[] url = null; //Class file loading path
private byte[] data = null; //Class file bytecode
private String extensionalName = ""; //Class file extension
public CustomClassLoader(URL[] urls) throws Exception{
super(urls);
this.url = new String[urls.length];
for (int i = 0; i < urls.length; i++) {
this.url[i] = urls[i].toURI().toString();
}
}
/*
* Parse URL
*/
private void setFilePath() {
for (int i = 0; i < this.url.length; i++) {
if (this.url[i].substring(0,4).toLowerCase().equals("file") == true) {
this.url[i] = this.url[i].substring(5);
}
}
}
/*
* Obtain the bytecode of the file with the specified class name (package name + class name)
* @name name String
* @return byte[]
*/
private byte[] getFileData(String name) {
try {
this.setFilePath();
for (String url : this.url) {
String fileName = url + name.replace('.', '/').concat(".") +
this.getExtensionalName();
input = new FileInputStream(new File(fileName));
if (input != null) {
break;
}
}
out = new ByteArrayOutputStream();
data = new byte[1024];
int len = -1;
while ((len = input.read(data)) != -1) {
out.write(data, 0, len);
}
data = out.toByteArray();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (input != null)
input.close();
if (out != null)
out.close();
return data;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
/*
* Search class files based on the specified class name
* @param name String
* @return Class
*/
protected Class findClassByName(String name) {
try {
byte[] data = this.getFileData(name);
if (data == null) {
return null;
}
return this.defineClass(name, data, 0, data.length);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/*
* Override loadClass() method
* @param name String
* @return Class
*/
public Class loadClass(String name) {
Class c = null;
try {
c = super.loadClass(name);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
if (c == null) //When the default method of the parent class is not loaded into the specified class, use a custom method to find it
c = this.findClassByName(name);
return c;
}
}
public String getExtensionalName() {
return extensionalName;
}
public void setExtensionalName(String extensionalName) {
this.extensionalName = extensionalName;
}
public static void main(String[] args) throws Exception {
URL[] url = new URL[] {new URL("file:e:/")}; //Add the path to the class you want to load
//Can be network or local
CustomClassLoader csl = new CustomClassLoader(url);
csl.setExtensionalName("rs");
Class c1 = csl.loadClass("com.demo");
Object obj = c1.newInstance();
Method method = c1.getMethod("printText", null);
method.invoke(obj, null);
}