I always think that the tostring method of the Tomcat 5 Class Loader is cool. Why? Because its tostring method lists all the parent class loaders and the resources loaded by the class loaders (that is, classpath ). some friends may not have noticed it. If you want to see how cool it is, you can find a JSP.
<% SYSTEM. Out. println (COM. syj. Test. Class. getclassloader (); %>
WEB-INF, classes, COM, syj, test
Start tomcat to access JSP and check the console:
Information: exposing service with name {http://ws.syj.com} demo
16:15:32 org. Apache. Coyote. http11.http11protocol start
Information: Starting coyote HTTP/1.1 on http-8080
16:15:33 org. Apache. JK. Common. channelsocket init
Information: JK2: ajp13 listening on/0.0.0.0: 8009
16:15:33 org. Apache. JK. server. jkmain start
Information: JK running id = 0 time = 15/78 Config = D:/syj. Work/syj. Server/tomcat5/CONF/jk2.properties
16:15:33 org. Apache. Catalina. startup. Catalina start
Information: server startup in 6907 MS
Reprinted please indicate the source http://blog.csdn.net/sunyujia/
Webappclassloader
Delegate: false
Repositories:
/WEB-INF/classes/
----------> Parent classloader:
Standardclassloader
Delegate: True
Repositories:
File: D:/syj. Work/syj. Server/tomcat5/shared/classes/
----------> Parent classloader:
Standardclassloader
Delegate: True
Repositories:
File: D:/syj. Work/syj. Server/tomcat5/common/classes/
File: D:/syj. Work/syj. Server/tomcat5/common/lib/ant-launcher.jar
File: D:/syj. Work/syj. Server/tomcat5/common/lib/ant. Jar
File: D:/syj. Work/syj. Server/tomcat5/common/lib/commons-collections-3.1.jar
File: D:/syj. Work/syj. Server/tomcat5/common/lib/commons-dbcp-1.2.1.jar
File: D:/syj. Work/syj. Server/tomcat5/common/lib/commons-el.jar
File: D:/syj. Work/syj. Server/tomcat5/common/lib/commons-pool-1.2.jar
File: D:/syj. Work/syj. Server/tomcat5/common/lib/jasper-compiler.jar
File: D:/syj. Work/syj. Server/tomcat5/common/lib/jasper-runtime.jar
File: D:/syj. Work/syj. Server/tomcat5/common/lib/jsp-api.jar
File: D:/syj. Work/syj. Server/tomcat5/common/lib/mssql_all.jar
File: D:/syj. Work/syj. Server/tomcat5/common/lib/naming-common.jar
File: D:/syj. Work/syj. Server/tomcat5/common/lib/naming-factory.jar
File: D:/syj. Work/syj. Server/tomcat5/common/lib/naming-java.jar
File: D:/syj. Work/syj. Server/tomcat5/common/lib/naming-resources.jar
File: D:/syj. Work/syj. Server/tomcat5/common/lib/servlet-api.jar
File: D:/syj. Work/syj. Server/tomcat5/common/lib/tools. Jar
----------> Parent classloader:
Sun. Misc. launcher $ appclassloader @ 18d0000f
The classpath provides clear debugging information and is useful in some complex class loading scenarios,
Decompile tomcat5/Server/lib/Catalina. Jar/webappclassloader. Class class to view the source code
- Public String tostring ()
- {
- Stringbuffer sb = new stringbuffer ("webappclassloader/R/N ");
- SB. append ("delegate :");
- SB. append (_ flddelegate );
- SB. append ("/R/N ");
- SB. append ("repositories:/R/N ");
- If (repositories! = NULL)
- {
- For (INT I = 0; I <repositories. length; I ++)
- {
- SB. append ("");
- SB. append (repositories [I]);
- SB. append ("/R/N ");
- }
- }
- If (parent! = NULL)
- {
- SB. append ("----------> parent classloader:/R/N ");
- SB. append (parent. tostring ());
- SB. append ("/R/N ");
- }
- Return sb. tostring ();
- }
So easy, so I want to write an util class and create a public static string tostring (classloader) method. I just want to do it right away, but the repositories attribute is self-built by Tomcat, in the class loader provided by Sun, which attribute stores classpath? I am not interested in reading the source code of the Tomcat class loading process, so I found a channel in the memory in debug and found that it was originally stored in the urlclassloader's urlclasspath UCP attribute, although not public, but it does not matter, because reflection can be achieved, but reflection is a strong point. Haha, there are not many codes and I will post them to share the results with you.
- Import java. Io. file;
- Import java. Lang. Reflect. field;
- Import java.net. url;
- Import java.net. urlclassloader;
- Import java. util. List;
- /**
- * <P>
- * Title: Debugging auxiliary class
- * </P>
- *
- * <P>
- * Description:
- * </P>
- *
- * <P>
- * Copyright: Reprinted please indicate the source http://blog.csdn.net/sunyujia/
- * </P>
- *
- * @ Author Sun Xiaojia
- * @ Main sunyujia@yahoo.cn
- * @ Date Sep 20,200 8 3:27:23 pm
- */
- Public class debugutil {
- /**
- *
- * Description: converts a class loader to a string for printing and debugging.
- * Reprinted please indicate the source http://blog.csdn.net/sunyujia/
- *
- * @ Mail sunyujia@yahoo.cn
- *
- * @ Since: SEP 20,200 8 3:29:59
- */
- Public static string tostring (classloader ){
- Stringbuffer sb = new stringbuffer (classloader. getclass (). getname ());
- SB. append ("/R/N ");
- Try {
- Field [] fields = urlclassloader. Class. getdeclaredfields (); // Retrieves all attributes of the urlclassloader class loader.
- For (INT I = 0; I <fields. length; I ++) {// traverses attributes
- Field field = Fields [I];
- If (field. getname (). Equals ("UCP") {// locate the UCP attribute Field object
- Field. setaccessible (true); // open the access permission
- Final object UCP = field. Get (classloader); // obtain the UCP object in the Class Loader
- Field [] ucpfields = UCP. getclass (). getdeclaredfields (); // retrieve all attributes of the UCP object
- For (int K = 0; k <ucpfields. length; k ++) {// traverses attributes of a UCP object
- Ucpfields [K]. setaccessible (true); // open the access permission
- If (ucpfields [K]. getname (). Equals ("path") {// obtain the path attribute Field object in the UCP object
- List list = (list) ucpfields [K]. Get (UCP); // obtain the path object in the UCP object
- SB. append ("repositories:/R/N ");
- For (Int J = 0; j <list. Size (); j ++ ){
- SB. append ("");
- SB. append (list. Get (j ));
- SB. append ("/R/N ");
- }
- }
- }
- }
- }
- If (classloader. getparent ()! = NULL ){
- SB. append ("----------> parent classloader:/R/N ");
- SB. append (tostring (classloader. getparent (); // Recursion
- SB. append ("/R/N ");
- }
- } Catch (exception e ){
- E. printstacktrace ();
- }
- Return sb. tostring ();
- }
- Public static void main (string [] ARGs) throws exception {
- File file1 = new file (
- "D:/syj. Work/syj. Server/tomcat5/common/lib/ant. Jar ");
- File file2 = new file (
- "D:/syj. Work/syj. Server/tomcat5/webapps/webservicesdemo/WEB-INF/classes ");
- Urlclassloader MCL = new urlclassloader (new URL [] {
- File1.touri (). tourl (), file2.touri (). tourl ()});
- System. Out. println ("demo 1 :");
- System. Out
- . Println (debugutil. tostring (debugutil. Class. getclassloader ()));
- System. Out. println ("DEMO 2 :");
- Class clazz = MCL. loadclass ("com. syj. test ");
- System. Out. println (debugutil. tostring (clazz. getclassloader ()));
- }
- }
Program output result:
Demonstration 1:
Sun. Misc. launcher $ appclassloader
Repositories:
File:/D:/syj. Work/syj. workspace/WS1/myblogtest/bin/
----------> Parent classloader:
Sun. Misc. launcher $ extclassloader
Repositories:
File:/D:/syj. Work/syj. ENV/jdk6.0/JRE/lib/EXT/dnsns. Jar
File:/D:/syj. Work/syj. ENV/jdk6.0/JRE/lib/EXT/localedata. Jar
File:/D:/syj. Work/syj. ENV/jdk6.0/JRE/lib/EXT/sunjce_provider.jar
File:/D:/syj. Work/syj. ENV/jdk6.0/JRE/lib/EXT/sunmscapi. Jar
File:/D:/syj. Work/syj. ENV/jdk6.0/JRE/lib/EXT/sunpkcs11.jar
Demonstration 2:
Java.net. urlclassloader
Repositories:
File:/D:/syj. Work/syj. Server/tomcat5/common/lib/ant. Jar
File:/D:/syj. Work/syj. Server/tomcat5/webapps/webservicesdemo/WEB-INF/classes/
----------> Parent classloader:
Sun. Misc. launcher $ appclassloader
Repositories:
File:/D:/syj. Work/syj. workspace/WS1/myblogtest/bin/
----------> Parent classloader:
Sun. Misc. launcher $ extclassloader
Repositories:
File:/D:/syj. Work/syj. ENV/jdk6.0/JRE/lib/EXT/dnsns. Jar
File:/D:/syj. Work/syj. ENV/jdk6.0/JRE/lib/EXT/localedata. Jar
File:/D:/syj. Work/syj. ENV/jdk6.0/JRE/lib/EXT/sunjce_provider.jar
File:/D:/syj. Work/syj. ENV/jdk6.0/JRE/lib/EXT/sunmscapi. Jar
File:/D:/syj. Work/syj. ENV/jdk6.0/JRE/lib/EXT/sunpkcs11.jar