Java obtains the class/jar package path, and java obtains classjar
On the Java platform, errors such as non-existent methods due to Class conflicts are occasionally reported, but there is no problem during compilation. Compiling and running in the same environment usually won't happen. Compiling in one environment but running in another environment, such as upgrading the integration environment, may encounter this problem. The cause may be the inconsistency between the jar environment of the integrated environment and the development environment. For example, if there are more or fewer jar files, it is the source of the conflict.
If an error is reported, you may obtain the path of the class or the path of the jar package, locate the class file, and check whether the class file is used during compilation.
Generally, we can use ClassPathTest. class. getResource (""). getPath (); to obtain the location of the class file corresponding to the ClassPathTest class, even if it is in a jar package; but this method is used to obtain the path of the class in the jar package, not omnipotent. Some jar packages cannot be obtained after security protection is added.
We can also use LogFactory. class. getProtectionDomain (). getCodeSource (). getLocation (). getFile (); method to obtain the path of the jar package where the currently used LogFactory class is located. Compared with the previous method, this method is specially used for the class in jar, the path structure is only one layer of the jar package and does not contain its internal package structure. However, this is not omnipotent, and cannot be obtained for the security-protected jar. For example, java. lang. System, we cannot know its path information.
Test class:
1 package cn.j2se.junit.classpath; 2 3 import static org.junit.Assert.*; 4 5 import java.io.UnsupportedEncodingException; 6 7 import org.apache.commons.logging.Log; 8 import org.apache.commons.logging.LogFactory; 9 import org.junit.Test;10 11 public class ClassPathTest {12 private static final Log logger = LogFactory.getLog(ClassPathTest.class);13 @Test14 public void testClassPath() {15 String classPath = ClassPathTest.class.getResource("").getPath();16 logger.info("the classpath of ClassPathTest is:[" + classPath + "]");17 18 assertEquals(1, 1);19 }20 21 @Test22 public void testJarPath() throws UnsupportedEncodingException {23 // System.class.getResource("") == null24 String lfClassPath = LogFactory.class.getResource("").getPath();25 logger.info("the classpath of LogFactory is:[" + lfClassPath + "]");26 27 // System.class.getProtectionDomain().getCodeSource() == null28 String lfJarPath = LogFactory.class.getProtectionDomain().getCodeSource().getLocation().getFile();29 logger.info("the jar path of LogFactory is:[" + lfJarPath + "]");30 assertEquals(1, 1);31 }32 }
Test results:
1 [INFO ] 2015-08-25 14:26:30 CST2 the classpath of ClassPathTest is:[/D:/develop/eclipse_jee_juno_SR2/workspace/lijinlong/j2se/bin/cn/j2se/junit/classpath/]3 4 [INFO ] 2015-08-25 14:26:30 CST5 the classpath of LogFactory is:[file:/D:/develop/eclipse_jee_juno_SR2/workspace/lijinlong/j2se/lib/commons-logging-1.1.1.jar!/org/apache/commons/logging/]6 7 [INFO ] 2015-08-25 14:26:30 CST8 the jar path of LogFactory is:[/D:/develop/eclipse_jee_juno_SR2/workspace/lijinlong/j2se/lib/commons-logging-1.1.1.jar]