Java Get classpath Path

Source: Internet
Author: User

ClassLoader provides two methods for obtaining resources from a loaded classpath: PublicURL getresource (String name);  PublicInputStream getResourceAsStream (String name); Here name is the classpath of the resource and it is relative to the "/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 class GetResource and getResourceAsStream methods, because the class object can be obtained from your class {such as Yourclass.class or Yourclass.getclass ()}, and ClassLoader needs to call the Yourclass.getclassloader () method again, but according to the JDK document, these two methods of the class object are actually "delegates" (      Delegate) for the ClassLoader to load it, so you just need to use these two methods of the class object. Therefore, calling directly This. GetClass (). getResourceAsStream (string name); Gets the stream, using Classloader.getsystemresourceasstream (string name) in the static method      ; 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 ("/" ) gets the absolute URI path of the current classpath. such as: File:/d:/workspace/jbpmtest3/bin/3.This  . GetClass (). getClassLoader (). GetResource ("") The resulting absolute URI path is also the current classpath. such as: File:/d:/workspace/jbpmtest3/bin/4.Classloader.getsystemresource ("") The resulting absolute URI path is also the current classpath. such as: File:/D:/WORKSPACE/JBPMTEST3/BIN/5.Thread.CurrentThread (). Getcontextclassloader (). GetResource ("") The resulting absolute URI path is also the current classpath. such as: File:/d:/workspace/jbpmtest3/bin/6.Servletactioncontext.getservletcontext (). Getrealpath ("/") in the Web application, 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/d:/program Files/apache-tomcat-7.0.40-2/webapps/miaomutong Note:1. Try not to use relative System.getproperty ("User.dir") The relative path of the current user directory. This is a time bomb that may kill you at any moment. 2try to use absolute path resources in the form of URIs. It can easily be transformed into a Uri,url,file object. 3. Try to use relative classpath relative paths. Do not use absolute paths. Use the public of the Classloaderutil class aboveStaticThe URL getextendresource (String relativepath) method has been able to locate resources for all locations using relative paths to classpath. 4. Never use a hard-coded absolute path. Because, we can completely use the ClassLoader class of 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! Method for obtaining a path outside classpath: URL base= This. GetClass (). GetResource ("");//first get the position of this class, such as/home/popeye/testjava/build/classes/net/String Path =NewFile (Base.getfile (), ".../....../....../" +name). Getcanonicalpath ();//we can get/home/popeye/testjava/name .Also, 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.

Java Get classpath Path

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.