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)