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 "/" root path. getResource gets a URL object to locate the resource, and getResourceAsStream gets the reference to the resource input stream to ensure that the program can extract data from the correct location.
But what we really use is not the ClassLoader methods, but the Class's getResource and getResourceAsStream methods, because the Class object can be obtained from your class (such as YourClass.class or YourClass.getClass()), and the ClassLoader needs to call the YourClass.getClassLoader() method again. However, according to the JDK documentation, these two methods of the Class object are actually "delegate" to the ClassLoader that loads it, so you only need to use these two methods of the Class object.
Therefore, directly call this.getClass().getResourceAsStream(String name) to get the stream, and use ClassLoader.getSystemResourceAsStream (String name) in the static method.
Here are some ways to get the classpath and the absolute path to the current class. You may need to use some of these methods to get the absolute path to the resources you need.
1.this.getClass().getResource("")
What you get is the URI directory of the current class file. Not including yourself!
For example: file:/D:/workspace/jbpmtest3/bin/com/test/
2.this.getClass().getResource("/")
What you get is the absolute URI path of the current classpath.
For example: file:/D:/workspace/jbpmtest3/bin/
3.this.getClass() .getClassLoader().getResource("")
What you get is also the absolute URI path of the current ClassPath.
For example: file:/D:/workspace/jbpmtest3/bin/
4.ClassLoader.getSystemResource("")
What you get is also the absolute URI path of the current ClassPath.
For example: file:/D:/workspace/jbpmtest3/bin/
5.Thread.currentThread().getContextClassLoader().getResource("")
What you get is also the absolute URI path of the current ClassPath.
For example: file:/D:/workspace/jbpmtest3/bin/
6.ServletActionContext.getServletContext().getRealPath("/")
In a web application, get the absolute path to the root directory of the web application. In this way, we only need to provide a path relative to the root directory of the web application to build an absolute path to locate resources.
For example: file: /D:/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/WebProject
Note:
1. Try not to use the relative path relative to System.getProperty ("user.dir") current user directory. This is a time bomb that may 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, File objects.
3. Try to use relative classpaths. Do not 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 the relative path relative to the classpath.
4. Absolutely do not 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. If you have to specify an absolute path, then using configuration files is also much better than hard-coded!
How to get a path outside of CLASSPATH:
URL base = this.getClass().getResource(""); //First get the location of this class, such as /home/popeye/testjava/build/classes/net/
String path = new File(base.getFile(), "…/…/…/…/”+name).getCanonicalPath(); // You can get /home/popeye/testjava/name
In addition, if you start the program from ANT, the retrieved from this.getClass().getResource("" is quite strange, and you can successfully debug it with the JAVA command line.
Let's take a look at the method to get the classpath classpath in Java
I've said a lot more, so I'll post the key code to you directly. The specific code is as follows:
<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("").getPath();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 above is the simple code of the method to obtain classpath classpath in Java introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message. The editor will reply to you in time!