JVM Load Class ClassLoader class method

Source: Internet
Author: User
Tags stub
Package com.budco.bmp.core.email.TestMail;

Import Java.io.ByteArrayOutputStream;
Import Java.io.File;
Import Java.io.FileInputStream;
Import java.io.FileNotFoundException;
Import java.io.IOException;
Import Java.lang.reflect.Field;

public class Testclassloader extends ClassLoader {

Path to existing file
private static final String DRIVE = "c:\\com\\budco\\bmp\\";
private static final String DRIVE1 = "com\\budco\\bmp\\";
private static final String FILETYPE = ". Class";

@Override
Public final class<?> Findclass (final String name) {
Final byte[] data = loadclassdata (name);

Final String ClassPath = Classloader.getsystemresource (DRIVE1 +
Name). replace (' \ \ ', '. ')). GetFile (). SUBSTRING (
1);
Put you to load the class with '. ' instead \ \ should be package path + class name//(DRIVE1 + name). replace (' \ \ ', '. ') ==
Com.budco.bmp.HelloWorld
Return DefineClass (DRIVE1 + name). replace (' \ \ ', '. '), data, 0, data.length);
}

Public byte[] loadClassData (final String name) {
Name = name.substring (0,name.lastindexof ('. '));
FileInputStream FIS = null;
byte[] data = null;
Final byte buffer[] = new byte[1024];
try {
FIS = new FileInputStream (new File (DRIVE + name + FILETYPE));
Final Bytearrayoutputstream BAOs = new Bytearrayoutputstream ();
int len = 0;
try {
while (len = fis.read (buffer))!=-1) {
Baos.write (buffer, 0, Len);
}
data = Baos.tobytearray ();
System.out.println ("-----------------| | | | | | | | --------------");
System.out.println (new String (data));
Baos.close ();
Fis.close ();
catch (Final IOException e) {

E.printstacktrace ();
}
catch (Final FileNotFoundException e) {

E.printstacktrace ();
}
return data;
}

/**
* @param args
*/
public static void Main (final string[] args) throws Exception {
TODO auto-generated Method Stub
Final Testclassloader loader = new Testclassloader ();
Final Testclassloader loader2 = new Testclassloader ();
Final class<?> obj = Loader.findclass ("HelloWorld");
Final class<?> o = Class.forName ("Com.budco.bmp.HelloWorld");
Final Field fs = O.getfield ("RS");
System.out.println ("------------------------ooo==" + fs.get ("RS"));
Final Class obj2 = Loader2.loadclass ("Test2", true);
Final Object objclass = Obj.newinstance ();

System.out.println ("==========1" + Objclass.getclass ());
System.out.println ("==========2" + obj.getname ());
System.out.println ("==========3" + Obj.getclassloader ());
System.out.println ("----------------------4" + obj.getcanonicalname ());

Final Field f1 = Obj.getfield ("RS");
Final String s = (String) f1.get ("RS");
System.out.println ("-------------------" + s);
Only the public domain name can be obtained

Final Field F2 = Obj.getfield ("txt");
Final String s2 = (string) f2.get ("txt");
System.out.println ("-------------------" + s2);

}

Public final class GG {

}

}


Here is the test class Helloworld.java want this class to compile. class file


Package com.budco.bmp;

public class HelloWorld {

Private String name = NULL;
private static final String DRIVE = "c:\\com\\budco\\bmp\\";
Public final static String RS = "HAL";
Public final static String txt = "txt";

Public String GetName () {
return name;
}

public void SetName (final String name) {
THIS.name = name;
}

Private String Getmethodname () {
This.setname ("ale");
System.out.println ("-------------");
Return "";

}

/**
* @param args
*/
public static void Main (final string[] args) {
TODO auto-generated Method Stub
System.out.println ("---------HelloWorld!----");

String path = "";
Path = drive.replace (' \ \ ', '. ');
SYSTEM.OUT.PRINTLN (path);
}

}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.