Java CLASSPATH (2) Classpath

Source: Internet
Author: User

 

 

Ii. Possible Problems
Whether or not they are willing to admit that beginners and experienced Java developers are the same. They have been cheated by lengthy and complex classpath at some times (usually in the worst cases. The number of third-party classes and user-defined classes that the application depends on increases gradually, and classpath gradually becomes a place where all possible directories and file names are accumulated. In this case, it is no longer obvious which class is first loaded by the class loader. This issue is particularly prominent if classpath contains repeated class entries. As mentioned above, the Class Loader always loads the first class with the proper name it finds in classpath. From the actual results, it "hides" other classes with a proper name but with a lower priority in classpath.

If you are not careful, you can easily fall into the classpath trap. After a long day of work, you add a directory to the classpath to make the application use the best and latest classes, but at the same time, You forget: another version of the class is stored in another directory with a higher priority in classpath!

3. A simple classpath Tool

The priority issue is inherent in the flat path declaration method, but it is not only a problem with Java classpath. To solve this problem, you only need to stand on the shoulders of the legendary software giant: the Unix operating system has a which command, specifying a name in the command parameter, which displays the path name of the execution file when the command is executed. In fact, the which command analyzes the PATH variable and finds the location where the command first appeared. This should also be a good tool for Java class path management. Inspired by it, I started to design a Java tool JWhich. This tool requires you to specify the name of a Java class, and then find the absolute path of the class to be loaded by the class loader according to classpath instructions.

The following is a JWhich instance. When the Java class loader loads the com. clarkware. ejb. ShoppingCartBean class, the absolute path name of the class where it appears for the first time. The search result shows that the class is in a directory:

> Java JWhich com. clarkware. ejb. ShoppingCartBean

Class com. clarkware. ejb. ShoppingCartBean found in
/Home/mclark/classes/com/clarkware/ejb/ShoppingCartBean. class

The following is the second JWhich instance. It shows the absolute path name of the class where the class appears for the first time when the Java class loader loads the javax. servlet. http. HttpServlet class. The search result shows that the class is in a file:

> Java JWhich javax. servlet. http. HttpServlet

Class javax. servlet. http. HttpServlet found in
File:/home/mclark/lib/servlet. jar! /Javax/servlet/http/HttpServlet. class

 

IV,JWhich's Working Process
To accurately determine which class is first loaded in classpath, you must go deep into the thinking method of the class loader. In fact, the implementation is not that complicated-you just need to ask the Class Loader directly!

1: public class JWhich {
2:
3 :/**
4: * according to the current classpath settings,
5: * displays the class file containing the specified class.
6: * absolute path of the location
7 :*
8: * @ param className <Class Name>
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 ("Class" + className +
23: "found in" + classUrl. getFile () + "");
24:} else {
25: System. out. println ("Class" + className +
26: "not found in" +
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 class name slightly so that the class loader can accept (12-16 rows ). Adding a "/" before the class name requires the class loader to accurately match the class name in classpath, rather than implicitly adding the package name prefix of the calling class. The purpose of converting all "." to "/" is to format the class name into a valid URL resource name according to the requirements of the class loader.

Next, the program queries the resource from the class loader. The resource name must match the formatted Class Name (rows 18-19 ). Each Class Object maintains a reference to its ClassLoader object, so it is queried from the Class loader that loads the JWhich Class. Class. the getResource () method is used to delegate the class loader of the class to return a URL for reading class file resources. Alternatively, when the specified class name cannot be found in the current classpath, class. the getResource () method returns null.

 

Finally, if the currentIf the specified class can be found in the classpath, the program displays the absolute path name of the class file containing the class (lines 21-24 ). As an auxiliary debugging tool, if the specified class cannot be found in the current classpath, the program obtains the java. class. path System attribute and displays the current classpath (24-28 rows ).

It is easy to imagine how the above simple code works in a Java Servlet using the Servlet engine classpath or in an EJB component using the EJB server classpath. For example, if the JWhich class is loaded by the Servlet engine's custom class loader, the program will use the Servlet engine's class loader to find the specified class. If the class loader of the Servlet engine cannot find the class file, it will delegate its parent class loader. Generally, when JWhich is loaded by a Class Loader, it can find out the current class loader and all the classes loaded by all its parent class loaders.

[Conclusion] if it is necessary to be the mother of all inventions, the tool that helps us manage Java class paths can be said to be a long time late. Java newsgroups and email lists are filled with many classpath problems. Now, JWhich provides us with a simple but powerful tool to help us completely turn to Java class paths in any environment.

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.