This article is a detailed analysis and introduction to the method of obtaining the JAVA path, including paths other than CLASSPATH. Friends in need can refer to
to summarize some methods of obtaining the JAVA path online:
Note: If you start the program from ANT, the result obtained by this.getClass().getResource("") is strange. You can successfully debug it directly using the JAVA command line.
Some methods to get the classpath and the absolute path of the current class
Methods to get the path outside CLASSPATH:
URL base = this.getClass().getResource(""); //先获得本类的所在位置,如/home/popeye/testjava/build/classes/net/ String path = new File(base.getFile(), "……/……/……/"+name).getCanonicalPath(); //就可以得到/home/popeye/testjava/name
The following are some methods to get the classpath and the current class Some methods for absolute paths. You may need to use some of these methods to get the absolute path to the resource you need.
1.FileTest.class.getResource("")
For example: file:/D:/java/eclipse32/workspace/jbpmtest3/bin/com/test/
2.FileTest.class.getResource("/")
What you get is the absolute value of the current classpath URI path.
For example: file:/D:/java/eclipse32/workspace/jbpmtest3/bin/
3.Thread.currentThread().getContextClassLoader().getResource("")
What you get is also the absolute URI path of the current ClassPath.
For example: file:/D:/java/eclipse32/workspace/jbpmtest3/bin/
4.FileTest.class.getClassLoader().getResource("")
What you get is also the absolute URI path of the current ClassPath.
For example: file:/D:/java/eclipse32/workspace/jbpmtest3/bin/
5.ClassLoader.getSystemResource("")
What you get is also the absolute URI path of the current ClassPath.
For example: file:/D:/java/eclipse32/workspace/jbpmtest3/bin/
I recommend using Thread.currentThread().getContextClassLoader().getResource("") to get the absolute path of the current classpath URI notation.
In Web applications, we generally obtain the absolute path of the root directory of the Web application through the ServletContext.getRealPath ("/") method. In this way, we only need to provide the path relative to the root directory of the web application to construct an absolute path to locate the resource.
Notes:
1. Try not to use a relative path relative to the current user directory of System.getProperty ("user.dir"). This is a ticking time bomb that could kill you at any time.
2. Try to use absolute path resources in the form of URI. It can be easily converted into URI, URL, and File objects.
3. Try to use relative paths relative to classpath. Don't use absolute paths. Using the public static URL getExtendResource(String relativePath) method of the ClassLoaderUtil class above, you can already locate resources at all locations using relative paths relative to the classpath.
4. Never use hard-coded absolute paths. Because, we can use the getResource("") method of the ClassLoader class to get the absolute path of the current classpath.
Using hardcoded absolute paths is completely unnecessary! It will definitely make your death look ugly! The program will not be portable!
If you must specify an absolute path, then using a configuration file is much better than hard coding!
Of course, I still recommend that you use a program to get the absolute path of the classpath to spell the absolute path of the resource
The above is the detailed content of Obtain JAVA path, including detailed explanation of paths outside CLASSPATH. For more information, please follow other related articles on the PHP Chinese website!