-1

I want to understand working of System.getProperty("user.dir") in different applications like console application , web application etc.

It's giving different path while running from console application and web application. Here are the examples-:
While running in console application it prints as below:

D:\eclipse workspace mars\ResearchProject

Which is root directory of project in which java class files lies having System.getProperty("user.dir") line of code.

On the other hand if I run System.getProperty("user.dir") code from some servlet/ service/ business/ dac java class then it prints as below:

D:\eclipse-jee-mars-2-win32\eclipse

Which is root folder of eclipse.

How System.getProperty("user.dir") works for different kind of application?
Is there any way to get root directory of web application?

Ashish Kumar
  • 916
  • 2
  • 15
  • 32

1 Answers1

2

How System.getProperty("user.dir") works for different kind of application?

It works exactly the same way in all cases. It returns the "current directory" that was specified by the application that launched the application.

  • In the case of an interactive shell, it will be the current directory of the shell (or subshell) that launched the application.

  • In the case of a shell script or a native launcher, it will be the current directory set by the script or the launcher. (For example, when you start Tomcat using the "catalina.sh" script, the script sets the current directory.)

  • In the case of an application launched by Eclipse, it will be the current directory set by the launch configuration you are using. The default is the project directory, but you can override this in the launch config.

Is there any way to get root directory of web application?

I assume that you mean the root directory of the deployed webapp in the web container.

String path = request.getServletContext().getRealPath("/");

For more details:

However, there are various "traps" with accessing webapp files via the file system. One is that they may be clobbered at any time by a redeployment. A better approach is to access "files" in the deployed webapp using getResourceAsStream.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216