Custom class loader-sun weiqin Java Object-Oriented Programming

Source: Internet
Author: User
Package cn.edu. BNU. songjie. ch10.selfclassloader; import Java. io. *;/*** for a custom class loader, you only need to extend the classloader and overwrite its findclass (sring name) method. * This method is based on the class name specified by the parameter, returns the reference of the corresponding class object. * @ Author songjie **/public class myclassloader extends classloader {private string name; private string Path = "d :\\"; private final string filetype = ". class "; Public myclassloader (string name) {This. name = Name;} public myclassloader (classloader parent, string name) {super (parent); this. name = Name;} Public String tostring () {return name;} public void setpath (string path) {This. path = path;} Public String Getpath () {return path;} protected class findclass (string name) {byte [] DATA = loadclassdata (name); Return defineclass (name, Data, 0, Data. length ); // This method is inherited from classloader}/*** reads the second-level data of the class into the memory * @ Param name * @ return */private byte [] loadclassdata (string name) {fileinputstream FCM = NULL; byte [] DATA = NULL; bytearrayoutputstream baos = NULL; try {name = Name. replaceall ("\\. "," \\\\ "); FCM = new fileinputstream (New file (path + name + filetype); baos = new bytearrayoutputstream (); int CH = 0; while (CH = Fi. Read ())! =-1) {baos. write (CH);} DATA = baos. tobytearray ();} catch (filenotfoundexception E1) {system. out. println ("file not found");} catch (ioexception E2) {system. out. println ("File Content reading exception");} finally {try {baos. close (); FCM. close () ;}catch (ioexception E3) {e3.printstacktrace () ;}} return data ;}}
 
 
 
 
 

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.