Java path Summary

Source: Internet
Author: User

Java class

System. getproperty ("user. dir ");

New file ("/")

Thread. currentthread (). getcontextclassloader (). getresource ("")

Filetest. Class. getclassloader (). getresource ("")

Classloader. getsystemresource ("")

Filetest. Class. getresource ("")

Filetest. Class. getresource ("/")

New file ("/"). getabsolutepath ()

JSP

Request. getrequesturi () -- absolute path corresponding to the root directory

Application. getrealpath (request. getrequesturi () -- absolute path of the object

Application. getrealpath ("/") -- absolute path of the current Web Application

New file (application. getrealpath (request. getrequesturi (). getparent () -- get the upper-level directory of the request file

Servletconfig. getservletcontext (). getrealpath ("/") -- absolute path of the current Web Application

Servlet

Request. getservletpath () -- absolute path corresponding to the root directory

Request. getsession (). getservletcontext (). getrealpath (request. getrequesturi () -- absolute path of the object

Server Path

1) WebLogic

New fileinputstream ("yourconfig/yourconf. properties ")

New fileinputstream ("./yourconfig/yourconf. properties ")

2) Tomcat

System. getproperty ("user. dir ")

1. system. getproperty ("user. dir ");

(User's current working directory.

Example: Write a Java filepathtest. Java; which is located under the basic package.

package basic;public class FilePathTest {public static void main(String[] args) {String sPath = System.getProperty("user.dir");System.out.println(sPath);}}

Use the javac command to compile the file into a. Class file (automatically generated in eclipse, which is located in the bin directory)

Test Example 1:

Set the basic and filepathtest. class files in the folder.

1) copy to the C root directory: Enter the DOS name and enter Java basic. filepathtest press ENTER ==> c :\

2) copy to the root directory of drive D: Enter the DOS name and enter Java basic. filepathtest press ENTER ==> D :\

Test Example 2:

Package this class into a jar file and execute "Java-jar jarname". The effect is the same as that in test example 1.

Test Example 3:

For a web project, this method varies depending on different Web servers.

For example, in Tomcat, the JSP method calls the tomcat installation root directory.

Therefore, this method is basically useless in web projects.

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.