English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
ClassLoader provides two methods for obtaining resources from the loaded classpath:
public URL getResource(String name); public InputStream getResourceAsStream(String name);
Here name is the classpath of the resource, which is relative to the "/The location under the root path. getResource returns a URL object to locate the resource, and getResourceAsStream gets the reference of the input stream of the resource to ensure that the program can extract data from the correct location.
However, the methods actually used are not the two methods of ClassLoader, but the getResource and getResourceAsStream methods of Class, because the Class object can be obtained from your class (such as YourClass.class or YourClass.getClass()), and the ClassLoader needs to call YourClass.getClassLoader() method again. However, according to the JDK documentation, the two methods of Class object are actually 'delegated' (delegate) to the ClassLoader that loads it, so it is enough to use the two methods of Class object.
Therefore, you can directly call this.getClass().getResourceAsStream(String name); to get the stream, and in the static method, use ClassLoader.getSystemResourceAsStream(String name); .
Here are some methods to obtain the absolute paths of classpath and the current class. You may need to use some of these methods to get the absolute path of the required resource.
1.this.getClass().getResource("")
The absolute directory of the current class file's URI is obtained. It does not include itself!
For example: file:/D:/workspace/jbpmtest3/bin/com/test/
2.this.getClass().getResource(""/"
The absolute URI path of the current classpath is obtained.
For example: file:/D:/workspace/jbpmtest3/bin/
3.this.getClass().getClassLoader().getResource("")
The obtained path is also the absolute URI path of the current ClassPath.
For example: file:/D:/workspace/jbpmtest3/bin/
4.ClassLoader.getSystemResource("")
The obtained path is also the absolute URI path of the current ClassPath.
For example: file:/D:/workspace/jbpmtest3/bin/
5.Thread.currentThread().getContextClassLoader().getResource("")
The obtained path is also the absolute URI path of the current ClassPath.
For example: file:/D:/workspace/jbpmtest3/bin/
6.ServletActionContext.getServletContext().getRealPath("/")
In the Web application, get the absolute path of the root directory of the Web application. In this way, we only need to provide the path relative to the root directory of the Web application to build the absolute path of the location resource.
For example: file:/D:/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/WebProject
Points to note:
1.Try not to use relative paths relative to System.getProperty("user.dir") the current user directory. This is a time bomb that can kill you at any time.
2.Try to use absolute paths in URI form. It can be easily converted to URI, URL, and File objects.
3.Try to use relative paths to the classpath. Do not use absolute paths. The public static URL getExtendResource(String relativePath) method of the above ClassLoaderUtil class can already use relative paths to the classpath to locate all resource locations.
4.Absolutely do not use hard-coded absolute paths. Because, we can completely use the getResource("") method of ClassLoader class to get the absolute path of the current classpath. If you must specify an absolute path, then using a configuration file is much better than hard coding!
Methods to obtain paths outside of CLASSPATH:
URL base = this.getClass().getResource(""); //First, obtain the location of this class, such as/home/popeye/testjava/build/classes/net/
String path = new File(base.getFile(), "……/……/……/"+name()).getCanonicalPath(); //we can get/home/popeye/testjava/
In addition, if the program is started from ANT, the result of this.getClass().getResource(
Let's take a look at the method of obtaining the classpath in Java next.
Enough chatter, let's directly post the key code, as shown below:
<span style="font-size: 18px;"> System.out.println("++++++++++++++++++++++++"); String path = System.getProperty("java.class.path"); String path2 = FreeMarkerWriter.class.getProtectionDomain().getCodeSource().getLocation().getPath(); String path3 = FreeMarkerWriter.class.getProtectionDomain().getCodeSource().getLocation().getFile(); String path4 = Thread.currentThread().getContextClassLoader().getResource(" System.out.println("path 1 = " + path); System.out.println("path 2 = " + path2); System.out.println("path 3 = " + path3); System.out.println("path 4 = " + path4); System.out.println("++++++++++++++++++++++++"); </span>
The following is a simple code snippet introduced by the editor to obtain the classpath in Java, which I hope will be helpful to everyone. If you have any questions, please leave a message, and the editor will reply to everyone in time!
Declaration: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been manually edited, and does not assume any relevant legal liability. If you find any content suspected of copyright infringement, please send an email to notice#w.3Please send an email to codebox.com (replace # with @ when sending an email) to report any violations, and provide relevant evidence. Once verified, this site will immediately delete the content suspected of infringement.