0

How to get current working directory for struts2 web application?

String currentDir = System.getProperty("user.dir");
String currentDir = new File(".").getAbsolutePath();

return /usr/share/tomcat.

I need path to web application, so I could read a file from war.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
EkaYuda
  • 137
  • 1
  • 4
  • Related: [Where to place configuration properties files in a JSP/Servlet web application?](http://stackoverflow.com/questions/2161054/where-to-place-configuration-properties-files-in-a-jsp-servlet-web-application) – BalusC Sep 10 '12 at 15:06

1 Answers1

0

You need to get the ServletContext's real path: ServletContext.getRealPath(String)

That said, why do you want to read a file that's in a war file, and why do you want to do it this way?

Prefer putting the file on the classpath and using getResourceAsStream:

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
  • I want to get current directory to open db.properties file. But I have found another problem: it is possible to view in web browser contents of db.properties file and db password. How to limit access to db.properties file ? How to get db.properties using getResourceAsStream ? – EkaYuda Sep 10 '12 at 14:06
  • @EkaYuda The only way it would be possible to access a property file directly is if it's in a publicly-accessible location (something deployed, but not under `WEB-INF`) and it's not filtered. It would be Really Weird to put a property file in a place it could be access directly. To get the file using getResourceAsStream you follow every tutorial in the world on how to use getResourceAsStream. – Dave Newton Sep 10 '12 at 14:11
  • So, I need to put db.properties in WEB-INF ? – EkaYuda Sep 10 '12 at 14:15
  • @EkaYuda If you want it on the classpath, it should be in `WEB-INF/classes`, or a jar in `WEB-INF/lib`. – Dave Newton Sep 10 '12 at 14:34