class file Introduction and Loading
After the Java compiler compiles the Java file, the. class file is generated on disk. This class file is a binary file, which is a machine code that only the JVM virtual machine can recognize. The JVM virtual machine reads the bytecode file, takes out binary data, loads it into memory, parses the information in the. class file, and generates the corresponding class object:
The class bytecode file is based on the rules of the JVM virtual machine in the code to organize the code, the specific class file is how to organize class information, you can refer to this blog: in-depth understanding of the Java class file format series. Or the Java Virtual Machine specification.
The following code shows a manual loading of the class file bytecode into the system, converting it into a class object, and then instantiating the process:
A. Define a programmer class:
[Java] view plain copy print? Package samples; /** * Program Ape * @author Louluan * * Public class Programmer {public void code () {System.out . println ("I ' m a programmer,just coding ..."); } B. Customize a class loader:
[Java] view plain copy print? Package samples; /** * Custom class loader for converting bytecode to class object * @author Louluan/public class Myclassloader extends ClassLoader {PU Blic class<?> Definemyclass (byte[] b, int off, int len) {return Super.defineclass (b, off, Len); c. Then compile into Programmer.class file, read bytecode in the program, then convert to the corresponding class object, then instantiate:
[Java] View plain copy print? package samples; import java.io.file; import java.io.fileinputstream; import java.io.filenotfoundexception; import java.io.ioexception;