Java Get classpath Path
Novice Paste Resources
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 location under the "/" root path. GetResource gets a URL object to locate the resource, and getResourceAsStream obtains the reference to the resource's input stream to ensure that the program can extract the data from the correct location.
But the real use of these two methods is not 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 ()), ClassLoader needs to call the Yourclass.getclassloader () method again, However, according to the JDK document, these two methods of class object are actually "delegate" (delegate) to load its classloader to do, so only need to use these two methods of class object is OK.
Therefore, call directly; this.getclass (). getResourceAsStream (String name) ; get stream,
The static method uses theClassloader.getsystemresourceasstream (String name); 。
Here are some ways to get the absolute path of classpath and the current class. You may need to use some of these methods to get the absolute path of the resources you need.
1.This.getclass (). GetResource ("")
Gets the URI directory of the current class. Don't include yourself!
such as: file:/d:/workspace/jbpmtest3/bin/com/test/
2.This.getclass (). GetResource ("/")
Get the current classpath.absolute URI Path。
such as: file:/d:/workspace/jbpmtest3/bin/
3.This.getclass ().getClassLoader (). GetResource ("")
It's also the current classpath.absolute URI Path。
such as: file:/d:/workspace/jbpmtest3/bin/
4.Classloader.getsystemresource ("")
It's also the current classpath.absolute URI Path。
such as: file:/d:/workspace/jbpmtest3/bin/
5.Thread.CurrentThread (). Getcontextclassloader(). GetResource ("")
It's also the current classpath.absolute URI Path。
such as: file:/d:/workspace/jbpmtest3/bin/
6.Servletactioncontext.getservletcontext (). Getrealpath ("/")
Web Application, you get the absolute path to the root directory of the Web application. This way, we only need to provide a path relative to the root of the Web application to build the absolute path to the location resource.
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 compared to System.getproperty ("User.dir"). This is a time bomb that may kill you at any moment.
2. Use absolute path resources in the form of URIs as much as possible. It can easily be transformed into a Uri,url,file object.
3. Try to use relative classpath relative paths. Do not use absolute paths. Using the public static URL Getextendresource (String relativepath) method of the Classloaderutil class above, you have been able to locate resources for all locations using relative paths to classpath.
4. Never use a hard-coded absolute path. Because, we can use the ClassLoader class's GetResource ("") method 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!
To obtain a path outside of Classpath:
URL base = This.getclass (). GetResource ("");//////Obtain 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, This.getclass (). GetResource ("") take out the more strange, directly with the Java command Line debugging can be successful.
Transferred from: http://hi.baidu.com/lzpsky/blog/item/695a4239434f40fb3a87ce86.html
Java Get classpath Path