Classloader: A Processing Method for Jar

Source: Internet
Author: User
/**

* @ Author Tom

*

* This class is used to test the dynamic loading of classes in jar files.

*/

Public class jarloader

{

Private Static void test (string [] ARGs) throws exception

{

Object OBJ = new myappclassloader (""). loadclass ("testccl"). newinstance ();

System. Out. println ("loaded? "+ OBJ );

}

Public static void main (string [] ARGs)

{

Try

{

Test (ARGs );

}

Catch (exception E)

{

E. printstacktrace ();

}

}

}

Public class myappclassloader extends urlclassloader

{

String m_jarfile = "E: \ javaproject \ classloadertest \ bin \ test. Jar ";

Public myappclassloader (string jarfile)

{

Super (new URL [0]);

If (jarfile! = NULL & jarfile. Length ()> 0)

{

This. m_jarfile = jarfile;

}

}

 

Public class loadclass (string name) throws classnotfoundexception

{

System. Out. println ("myappclassloader loadclass !! ");

Try

{

URL url = new java. Io. File (m_jarfile). tourl ();

Return new urlclassloader (new URL [] {URL}). loadclass (name );

}

Catch (exception E)

{

E. printstacktrace ();

}

Return super. loadclass (name );

}

}

Public class testccl

{

Public testccl ()

{

System. Out. println ("CCL run !!! ");

// New BB ();

Try

{

Class. forname ("BB"). newinstance ();

// New BB ();

}

Catch (exception E)

{

E. printstacktrace ();

}

}

Public static void main (string [] ARGs)

{



}

}

Public Class BB

{

Public BB ()

{

System. Out. println ("BB construct ");

}

}

The result is

Myappclassloader loadclass !!

CCL run !!!

Bb construct

Loaded? Testccl @ 93dee9

"CCL run !!!" Why does this sentence appear only once? According to the principle that class. forname ("BB") uses myappclassloader, why is it not the loadclass method,

No.

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.