Java's ClassLoader and Package mechanism introduced the delegation mechanism of ClassLoader, which passed the loaded task to the superiors, and pushed until the class loader (there is no superior loader). If the startup class loader is loaded with this class, then it will be loaded first. If you can't, it is passed down. In fact, this leads out a concept of runtime. Classes loaded with different loaders, even if the package name is the same, it cannot be accessed. This ensures that the core class library is not destroyed.
This article will tell how to extend the ClassLoader class to implement a self-type loader, each Class object has a reference to the ClassLoader to load his ClassLoader, you can get it through the Public ClassLoader getClassLoader () method. In order to create your own class loader, we should extend the ClassLoader class, which is an abstract class. Our purpose is to load a class from the local file system using the type loader we implemented. We created a FileClassLoader Extends ClassLoader. We need to overwrite the FindClass (String Name) method in ClassLoad, which gets a Class object through the name of the class. Public class findclass (string name) {byte [] data = loadingclassdata (name); return defineClass (Name, Data, 0, Data.Length);} We should also provide a method loadclassdata (String Name), return through the name of the class. The byte array of Class files. Then use the defineClass () method provided by ClassLoader we can return the Class object. public byte [] loadClassData {FileInputStream fis = null (String name); byte [] data = null; try {fis = new FileInputStream (new File (drive name fileType)); ByteArrayOutputStream baos = new ByteArrayOutputStream (); int ch = 0; while ((ch = fis.read ())! = -1) {baos.write (ch);} DATA = baos.tobyteaRray ();} catch ({ooException e) {E.PrintStackTrace ();} Return Data;} Here we are loaded from the D disk.
Below we provide a class public class myApp {}, and no methods and variables are defined in the class. Below we compile myapp.java to get myapp.class, then place the file on the root directory of the D disk. To prove that myapp.class is loaded by the ClassLoader we defined, we print the name of the type-loaded loader loaded with myApp.class in the MAIN () method of FileClassLoader. public static void main (String [] args) throws Exception {FileClassLoader loader = new FileClassLoader (); Class objClass = loader.loadClass ( "MyApp", true); Object obj = objClass.newInstance (); System.out.println ( objClass.getName ()); System.out.println (objClass.getClassLoader ());} FileClassLoader javac FileClassLoader.java compile and run java FileClassLoader can see the output is MyAppFileClassLoader @ 1a5ab41 if you put your MyApp.class What is the case where the program is located? Readers do it yourself! The source code of FileClassLoader is given below. Import java.io.byteArrayoutputStream; import java.io.file; import java.io.fileinputstream; import java.io.ioException
public class FileClassLoader extends ClassLoader {public static final String drive = "d: /"; public static final String fileType = ".class"; public FileClassLoader () {super ();} public FileClassLoader (ClassLoader arg0) {super (arg0) ;} public Class findClass (String name) {byte [] data = loadClassData (name); return defineClass (name, data, 0, data.length);} public byte [] loadClassData (String name) {FileInputStream fis = null; byte [] data = null; try {fis = new FileInputStream (new File (drive name fileType)); ByteArrayOutputStream baos = new ByteArrayOutputStream (); int ch = 0; while ((ch = fis.read ())! = -1) {baos.Write (CH);} DATA = baos.tobyteaRray ();} catch (ooException e) {E.PrintStackTrace ();} return data;} public STA tic void main (String [] args) throws Exception {FileClassLoader loader = new FileClassLoader (); Class objClass = loader.loadClass ( "MyApp", true); Object obj = objClass.newInstance (); System.out.println (objClass .getname ()); system.out.println (Objclass.getClassLoader ());}}