1

I have a Java project in Eclipse IDE. If I create new File object. Like File file = new File("someFile.txt"). It expects someFile.txt to be present in project home. And I can put it in some directory like config/someFile.txt and can use this in new File constructor. All good here.

When I make the JAR of this project and put it in WEB-INF/lib. Then new File("someFile.txt") code expects the file in TOMCAT_HOME/bin. I checked it with file.getAbsolutePath();. It is out of my control. My question is why in java web application new File expects to be in the bin director. Why not the the project home. So that i can navigate to my configuration dir easily. Even ../../ navigation also does not work. Because they are getting appended with TOMCAT_HOME/bin/../../someFile.txt and cause File Not Exist.

I hope I have explained well my problem.

skaffman
  • 398,947
  • 96
  • 818
  • 769
Tahir
  • 3,344
  • 14
  • 51
  • 69
  • 1
    This might be interesting for you [java-relative-path-of-a-file-in-a-java-web-application](http://stackoverflow.com/questions/2395737/java-relative-path-of-a-file-in-a-java-web-application) – Sridhar G Apr 24 '12 at 12:09

1 Answers1

5

TOMCAT_HOME/bin is the working directory of the JVM process, and so every relative path will be resolved relative to that directory.

There's not much you can do about, other than not using relative file paths in webapps. Better still, use ServletContext.getRealPath(), or Class.getResourceAsStream().

skaffman
  • 398,947
  • 96
  • 818
  • 769