The Java class loader uses the precedence policy, which is loaded first when the class is loaded. Sometimes we make a system, when the class library is very large, the class may conflict, that is, the CLASSPATH has different versions of the two identical classes, which often makes debugging a lot of trouble, we may modify the class, but because the class loader is using another non-updated class, This time will be confused, once the debugging process went astray.
So how do I determine which path my current loaded class is in or which jar it exists in? The Java language gives us the solution as shown in the following code:
ImportJava.security.CodeSource;ImportJava.security.ProtectionDomain;Importorg.apache.commons.lang.StringUtils;/*** Determine which jar package the class is loaded from *@authorWinter Lau*/ Public classTest { Public Static voidMain (string[] args) {protectiondomain pd= StringUtils.class. Getprotectiondomain (); Codesource CS=Pd.getcodesource (); System.out.println (Cs.getlocation ());}}
The result of this piece of code running on my machine is:
File:/d:/projects/java/dlog4j_javayou/webapp/web-inf/lib/commons-lang-1.0.1.jar
This is the jar package where the StringUtils class is located, at a glance. However, there is a restriction that the class loaded by the system ClassLoader cannot use this method to get the path, and if you do the above test on the string class A null pointer exception will occur because the resulting codesource is empty. You can use-verbose to view the loading information of a system class when the program is started, using the same method as:
Java-verbose MyApp
"Go" to see which package the Java class is loaded from