Java Project get root path (differentiate between Web project and application project)

Source: Internet
Author: User

Java projects often read the configuration file, where the configuration file is read, and the path to the location file is read. Therefore, it is especially critical to get the file path correctly in the project.

Depending on the Java project, there are some minor differences in getting the file path

Test environment: E:\Eclipse-workspace\BlkReport------My source project root directory

K:\Tomcat 7.0----------The root directory of the Tomcat project

(i) Get the project root path in the Java Web project:

1.1 Getting the project root path in a JSP or servlet

String Realpath = Getservletcontext (). Getrealpath ("/")

(b) In a Java desktop application, getting the root directory can be obtained by using the file class's API GetAbsolutePath ()

File File=new file ("");
String Abspath=file.getabsolutepath ();

return abspath;

(iii) in Tomcat, if you get the project root path by using (New File (")). GetAbsolutePath () Gets the Bin folder path name under the Tomcat installation path, such as" K:\Tomcat 7.0 \ Bin ", Take the obtained path out of the "bin" and then stitch the "webapps\\ project folder name" to get the root path of the project.

Java Project get root path (differentiate between Web project and application project)

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.