wrahool
wrahool

Reputation: 1141

Working directory of a Tomcat webapp

I have a Java web application that is deployed on an Apache Tomcat 6 Server. The application includes an info.properties file that is added to the classpath, and has several hard coded values, which are read during runtime.

I now need to remove a certain value from the info.properties file, and build the value (a String, that of path to the bin folder of the same Tomcat installation) instead.

How do I get the working directory of my application during runtime and then add a relative path (from the info.properties file ) to access the bin folder?

Thanks

Upvotes: 4

Views: 5239

Answers (2)

Tonino
Tonino

Reputation: 1166

Tomcat home directory or Catalina directory is stored at the Java System Property environment. If the Java web application is deployed into Tomcat web server, we can get the Tomcat directory with the following command:

System.getProperty("catalina.base");

Here you can move inside Tomcat folder and open bin easily

Upvotes: 4

DavidPostill
DavidPostill

Reputation: 7921

Try the following code to get the current working directory:

public static String getCWD() {
    File file;
    int index;
    String pathSeparator;
    String cwd = null;

    file = new File(".");
    pathSeparator = File.separator;
    index = file.getAbsolutePath().lastIndexOf(pathSeparator);

    try {
      cwd = file.getAbsolutePath().substring(0, index);
    } catch (StringIndexOutOfBoundsException e) {
      System.err.println("Caught Exception: " + e.getMessage() + "\n");
    }

    return cwd;
  }

Upvotes: 0

Related Questions