4

I am trying to get deploy root directory of my servlet based project from java. I am using the following lines of codes to get the path details.

Type 1:

File directory = new File (".");
try {
    System.out.println ("Current directory's canonical path: " 
            + directory.getCanonicalPath()); 
    System.out.println ("Current directory's absolute  path: " 
                + directory.getAbsolutePath());
}catch(Exception e) {
    System.out.println("Exceptione is ="+e.getMessage());
}

Type 2:

String currentDir = System.getProperty("user.dir");
System.out.println("Current dir using System:" +currentDir);

While executing the above codes from main class i am getting user directory. When i executes from server side, gets as, "Current dir using System:D:\Apache Tomcat 6.0.16\bin". But my project is located in D:\Apache Tomcat 6.0.16\wepapps\SampleStructs.

Please give me any suggestions for this and help me out of this.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Raghupathiraja
  • 199
  • 2
  • 2
  • 9
  • 2
    your executable will be placed in `\Apache Tomcat 6.0.16\bin` once the project is compiled! Irrespective of where the project folder is. – codeMan Dec 27 '12 at 06:07
  • 1
    @codeMan To be precise, there's no executable, and the web app doesn't end up in \bin, either. – Dave Newton Dec 27 '12 at 15:11
  • WARNING: there is no sensible real world use case to need to know this. If the sole purpose is to be able to grab a location to save uploaded files, then please head to https://stackoverflow.com/q/18664579. Or if the sole purpose is to be able to grab resource files such as configuration properties files, then please head to https://stackoverflow.com/q/2161054. See also this related question: https://stackoverflow.com/q/12160639. – BalusC Jun 21 '22 at 08:29

4 Answers4

7

First of all, the main cause of your problem is the difference between current working directory and the location of your executable. You should know that current working directory in Linux is not the directory where the executable is, but instead the current directory where the program was started from.

As an example, let's say you have a program current which prints out the current directory and it is located in /home/user/scripts/.

If you do this:

cd /home/user/scripts
./current

It will print out: /home/user/scripts/ But, if you do this:

cd /home/user/
scripts/current

The output will be: /home/user/

As to the possible solutions, some of them I found useful are:

  • Refer to your project resources relative to the classpath, see ClassLoader.getResourceAsStream() for more info
  • Refer to your configuration resources, like properties files and such, relative to the user home directory.
  • Put all other locations, such as media directory path and similar to the configuration file from the point above.
  • If all other options are not available or practical use getClass().getProtectionDomain().getCodeSource().getLocation().getPath(). See more about this approach and some possible issues here: How to get the path of a running JAR file?
Community
  • 1
  • 1
Goran Jovic
  • 9,418
  • 3
  • 43
  • 75
3

It because when you execute from main class everything is fine, but this code runs on server it looks into current directory and current the directory structure is Apache 'bin' from where you have started the server(run.bat).

Vallabh Patade
  • 4,960
  • 6
  • 31
  • 40
1

you can use this code

String absolutePath = getClass().getProtectionDomain().getCodeSource().getLocation().getPath();
        absolutePath = absolutePath.substring(0, absolutePath.lastIndexOf("/"));

this code is working before for me! it will return the full path of folder in windows or linux.

goravine
  • 266
  • 3
  • 11
0

There are different context we are talking about here. 1. Running the application in standalone mode. 2. Running the application in container on server side. In #1, The application is run from the directory it is invoked.

But #2 case, the application is run relative to the container, so you see the location of server directory. This also shields the application code.

pratikch
  • 670
  • 5
  • 19