Summary of methods for implementing fetching paths in Java _java

Source: Internet
Author: User

One, the Java file to get the path

Thread.CurrentThread (). Getcontextclassloader (). GetResource ("")//Get the path of the resource file (. class file)
Classloader.getsystemresource ("")
Class_Name.class.getClassLoader (). GetResource ("")
Class_name.class. GetResource ("/")
Class_name.class. GetResource ("")//Get the path of the current class
System.getproperty ("User.dir")//Get the absolute path to the project root directory
System.getproperty ("Java.class.path")//Get class path and package path

The printed output is as follows:

file:/f:/work_litao/uri_test/webcontent/web-inf/classes/
file:/f:/work_litao/uri_test/webcontent/web-inf/classes/
file:/f:/work_litao/uri_test/webcontent/web-inf/classes/
file:/f:/work_litao/uri_test/webcontent/web-inf/classes/
file:/f:/work_litao/uri_test/webcontent/web-inf/classes/com/xml/imp/
F:work_litaouri_test
f:work_litaouri_testwebcontentweb-infclasses; F:work_litaouri_testwebcontentweb-inflibdom4j.jar

Second, the relative path and absolute path of the current application are obtained in JSP

Absolute path for root directory: Request.getrequesturi ()
Absolute path of the file: Application.getrealpath (Request.getrequesturi ());
Absolute path to the current Web application: Application.getrealpath ("/");
Gets the upper-level directory of the request file: New file (Application.getrealpath (Request.getrequesturi ())). GetParent ()

The relative and absolute paths of the current application are obtained in the servlet

The absolute path of the root directory: Request.getservletpath ();
Absolute path to File: Request.getsession (). Getservletcontext (). Getrealpath (Request.getrequesturi ())
Absolute path of the current Web application: Servletconfig.getservletcontext (). Getrealpath ("/");

(ServletContext objects get several ways:
Javax.servlet.http.HttpSession.getServletContext ()
Javax.servlet.jsp.PageContext.getServletContext ()
Javax.servlet.ServletConfig.getServletContext ()
)

The above mentioned is the entire content of this article, I hope you can enjoy.

Please take a moment to share the article with your friends or leave a comment. We will sincerely thank you for your support!

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.