A simple classpath tool

xiaoxiao2021-03-06  67

The priority problem is the problem with the fairway declaration method, but it is not a problem with only Java ClassPath. To solve this problem, you only need to stand on the shoulders of the legendary software.: The UNIX operating system has a which command, specify a name in the command parameter, which will show when this name is executed as a command The path name of the file. In fact, the which command is to analyze the PATH variable, then identify the position of the command for the first time. This should also be a tool for Java path management. Under its inspiration, I started to design a Java tool JWHICH. This tool requires the name of a Java class and then finds the absolute path of the location where the class loader is about to load according to the ClassPath guidelines.

Below is an example of JWHICH. It shows that when the Java class loader is loaded with the com.clarkware.ejb.shoppingCartBean class, the first appearance of the absolute path name, the lookup results show that the class is in a certain directory:

> Java jwhich com.clarkware.ejb.shoppingCartBean class 'com.clarkware.ejb.shoppingcartbean' found in '/Home/mclark/classes/com/clarkware/ejb/shoppingCartBean.class'

Here is the use of the second JWHICH. It shows that when the Java class loader is loaded with a javax.servlet.http.httpservlet class, the first appearance of the absolute path name, the lookup results show that the class is in a file:

> Java jwhich javax.servlet.http.httpservlet.http.httpservlet 'found in' file: /Home_mclark/clib/servlet.jar! /javax/servlet/http/httpservlet.class'

Fourth, JWHICH work process

To accurately determine which class in ClassPath is loaded first, you must go deep into the gear loader. In fact, it doesn't sound so complicated when the specific implementation - you just ask the type loader!

1: Public class jwhich {2: 3: / ** 4: * Depending on the current classpath setting, 5: * shows the absolute path of the class file containing the specified class, 7: * 8: * @Param ClassName 9: * / 10: public static void which (string classname) {11: 12: if (! Classname.StartSwith ("/")) {13: classname = "/" classname; 14:} 15: classname = classname.Replace ('.', '/'); 16: classname = classname ".class"; 17: 18: java.net.url classurl = 19: New jwhich (). GetClass (). GetResource (classname); 20: 21: if (ClassURL! = null) {22: system.out.println ("/ nclass'" ClassName 23: "'Found In / N'" ClassURL.GetFile () "'"); 24:} else {25: system.out.println ("/ nclass'" classname 26: "'not found in / n'" 27: system.getproperty ("java.class.path ") " '"); 28:} 29:} 30: 31: public static void main (string args []) {32: if (args.length> 0) {33: jwhich.which (args [0] ); 34:} else {35: system.err.println ("USAGE: Java jwhich"); 36:} 37:} 38:} First, you must adjust the name of the class to the class loader (12 -16 line). In front of the name of the class, add a "/" means that the desired class loader matches the class name in the classpath, rather than trying to add a package name prefix that call the class. The purpose of all "." Is converted to "/" is to format the class name into a legal URL resource name according to the requirements of the type loader.

Next, the program query resource from the class loader, the name of this resource must match (18-19 rows). Each Class object maintains a reference to the ClassLoader object loaded, so here is a class loader in which the JWHICH class is loaded. The class.getResource () method actually delegates the class-loader loaded to the class, returns a URL for reading the class file resource; or when the specified class name cannot be found in the current classpath, Class.getResource ( The method returns NULL.

Finally, if the specified class can be found in the current classpath, the program displays the absolute path name (21-24 line) of the location of the class files that contain this class. As a debug auxiliary means, if the specified class cannot be found in the current classpath, the program acquires the java.class.path system properties and displays the current ClassPath (24-28 rows). Author: Cactus Studio

■ Source: ENET

转载请注明原文地址:https://www.9cbs.com/read-111687.html

New Post(0)