Custom class loader -- load class files in any specified directory

Source: Internet
Author: User
Public class myclassloader extends classloader {string path; // The Public myclassloader (string path) {super (); this. path = path ;}@ suppresswarnings ("deprecation") @ overrideprotected class <?> Findclass (string name) throws classnotfoundexception {// read the specified class file string file = path + "/" + name + "through the file input stream ". class "; system. out. println (File); try {fileinputstream FCM = new fileinputstream (File); // write the byte data of the read class file to the memory bytearrayoutputstream out = new bytearrayoutputstream (); int I = 0; while (I = Fi. read ())! =-1) {out. write (I. close (); byte [] Buf = out. tobytearray (); // extract the byte data written to the memory to the array // public byte [] tobytearray () to create a newly allocated byte array. The size is the current size of the output stream, and the valid content of the buffer has been copied to the array. Return defineclass (BUF, 0, Buf. Length);} catch (exception e) {e. printstacktrace () ;}return super. findclass (name );}}

Test class

Public class myclassloadertest {static variables in = new classes (system. in); public static void main (string [] ARGs) throws exception {system. out. println ("Path of the folder where the class file to be loaded is located:"); string Path = in. nextline (); // The parent path of the class file to be loaded: system. out. println ("Name of the class file to be loaded:"); string name = in. nextline (); Class clazz = new myclassloader (PATH ). loadclass (name); // main method of executing the loaded class file // method met = clazz. getmethod ("Mai N ", string []. class); // system. out. println (met. tostring (); // met. invoke (null, (object) New String [] {}); // load the specified class file class clazz2 = new myclassloader (PATH) in any directory through the custom class loader ). loadclass (name); system. out. println ("\ r \ n -------------- list all constructor ========"); constructor [] cons = clazz2.getconstructors (); for (constructor: cons) {system. out. println (constructor. tostring ();} system. out. println ("--------------- List all Putong methods in the class program ........ "); Method [] Methods = clazz2.getmethods (); For (method: Methods) {system. out. println (method. tostring () ;}}// F: \ java30 \ d24 // myiebygui


This article comes from "either desperate or rolling back !" Blog, please be sure to keep this source http://jiangzuun2014.blog.51cto.com/8732469/1533979

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.