/**
* @ 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.