/ * Turn from the file from the file from the file (use of ClassLoader) This article describes how to use the class library loader ClassLoader to transfer to Class at runtime.
Method 1 Using the interface class, the newly called Class is the specific implementation of it 1) Write an interface class newface.class2) Write interface file Implement TestFacea.class Renamed Testfacea.file or other file name All 3) Main program Input to Byte [], you can call on the local file, you can also use the network whether it can be converted to a class code of the compiled file content to be converted into a Class and initialize 5) Implementation The mouth is actually implemented for a file-in-use file, and of course different implementations can also be done to decrypt a file. To pay attention to a file to be transferred, it must be one The existing interface class is implemented in a pointful EJB that is called in the local, and the file being transferred is that the benefits of EJBOBJECT this method is that the method to be transferred is known to be known. , Relatively speaking, this method is simple and easy to use
Others: But Java does not provide a function similar to the class library uninstaller (Classunloader) to clear the already loaded module from memory. So if a Class has been transferred, it is placed in memory, and record this class in the program information table has been transferred. If you use it again, it is transferred from memory. If the actual file update can only be the program next time Turn time again, and you cannot update a Class during the run, if you forced once again transfer, you will be linkage error: Duplicate Class Definition (Repeat Definition CLASS)
* / package card; // Used main program PUBLIC CLASS TESTNEWFACE {public static void main (string [] args) throws java.lang.exception {
// Shared initialization parameters, start Class Testc; Object Testo; Cloader Cl = New Cloader (); // Shared the initialization parameters, end
// Method 1 example code, Newface is a local interface class, and newface.class has already started testc = cl.load ("testfacea.class", "testfacea.class"); testo = testc.newinstance ();
System.out.println ("Instance Has over!"); (NEWFACE) TESTO) .out ("Method 1 (1) Method"); System.out.Println ("Outsize =" ((NewFace) ) TESTO) .Outsize ("1111", "aaaa");}} // interface package COM;
Public interface newface {
Public void out (string xx); public int outsize (String X1, String X2);
} // Implement the class of the interface - the compiled .class file is put in D: // package com; public class testfacea imports newface {
Public void out (string xx) {system.out.println (xx "for testfacea");} public int out ze (String x1, string x2) {return x1.length () x2.length ();
} // ourselves' ClassLoad Class loaderpackage COM;
/ * To complete their from a byte [] must be converted into a Class defineClass ClassLoader extends ClassLoader because the method is to be used only protected extends ClassLoader * /// import com.newface; class cloader extends ClassLoader {static int maxsize = 10000; public class load (String namefile, string classname) throws java.lang.exception {class result = null; try {// Declats if this class has been transferred, it has been returned directly, otherwise it is transferred to class ctmp = This.FindloadedClass (ClassName); System.out.Println (CTMP.GetName () "is loading"); return ctmp;} catCH (Exception E) {system.out.println ("memory is null);} system .out.println ("start .."); java.io.fileinputstream in = new java.io.fileinputStream ("d: //" namefile; byte [] classbyte = new byte [maxsize]; // actual When applied, a file can be decrypted to decrypt the process, as long as the ClassByte // has decrypted content in ClassByte; readsize = in.read (classByte); system.out.println ("Read File Long : " Readsize); In.Close (); System.out.println (" The Clas Sname: " ClassName); Return DefineClass (" com. " ClassName, Classbyte, 0, ReadSize;}} ********************** ****************************************** Note: After compiling. The package name of the Class file, you need to be with our files. *********************************************************** ********************************************************************************** ! *********************************************************** ******************* Note: The above is the modification we have made according to the example of a netizen ClassLoad on the Internet. Everyone exchanges. *********************************************************** *******************