ClassLoader provides two methods for obtaining resources from the loaded classpath:
Public URL getresource (String name);
Here name is the classpath of the resource, which is relative to the location under the "/" root path. GetResource gets a URL object to locate the resource, and getResourceAsStream obtains the resource input stream's reference assurance program to extract the data from the correct location.
But the real use of these two methods is not ClassLoader, but class GetResource and getResourceAsStream methods, because class objects can be obtained from your class (such as Yourclass.class or Yourclass.getclass ()), while ClassLoader needs to invoke the Yourclass.getclassloader () method again, but according to the JDK documentation, the two methods of the class object are actually "delegates" (delegate) to the ClassLoader that loads it, so just use the two methods of the class object.
Therefore, call This.getclass (). getResourceAsStream (String name) directly, get the stream, and use Classloader.getsystemresourceasstream in the static method ( String name);.
Here are some ways to get classpath and the absolute path of 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 ("")
Gets the URI directory of the current class file. Not including myself!
such as: file:/d:/workspace/jbpmtest3/bin/com/test/
2.this.getclass (). GetResource ("/")
Gets the absolute URI path of the current classpath.
such as: file:/d:/workspace/jbpmtest3/bin/
3.this.getclass (). getClassLoader (). GetResource ("")
The absolute URI path of the current classpath is also obtained.
such as: file:/d:/workspace/jbpmtest3/bin/
4.classloader.getsystemresource ("")
The absolute URI path of the current classpath is also obtained.
such as: file:/d:/workspace/jbpmtest3/bin/
5.thread.currentthread (). Getcontextclassloader (). GetResource ("")
The absolute URI path of the current classpath is also obtained.
such as: file:/d:/workspace/jbpmtest3/bin/
6.servletactioncontext.getservletcontext (). Getrealpath ("/")
Web application, you get the absolute path of the Web application's root directory. In this way, we only need to provide a path relative to the Web application root directory, we can build the absolute path of locating resources.
such as: File:/d:/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/webproject
Note the point:
1. Try not to use relative paths to the current user directory as opposed to System.getproperty ("User.dir"). This is a ticking time bomb, it may kill you at any moment.
2. Use absolute path resources in the form of URIs as far as possible. It can be easily transformed into a Uri,url,file object.
3. Use relatively classpath relative path as far as possible. Do not use absolute paths. The public static URL Getextendresource (String relativepath) method of the Classloaderutil class above can already be used to locate resources in all locations using relative paths to classpath.
4. Never use hard coded absolute paths. Because we can completely 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 a configuration file is much better than hard coding!
To obtain a path beyond Classpath:
URL base = This.getclass (). GetResource ("");///Get the location of this class first, 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, This.getclass (). GetResource ("") to take out the strange, directly with the Java command-line debugging can be successful.
Here's how to get Classpath classpath in Java
More nonsense to say, directly to everyone to paste the key code, the specific code 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 a small set to introduce Java to get Classpath Classpath method simple code, hope to help everyone, if you have any questions welcome to my message, small series will promptly reply to everyone!