1

I'm running an application from within the Tomcat container. The user clicks a link and this ultimately causes a method in a helper class to create a file and save it to the file system. When this code is run from a unit test / eclipse it saves the file in the applications root directory but when this is run from the browser / in Tomcat the file is stored in Tomcat's bin directory.

How can I find the applications root so I can choose where the save the file from there?

  • I need to programatically find the root so this can be deployed onto other environments running tomcat.

Thanks

csilk
  • 188
  • 1
  • 15

2 Answers2

5

You should not rely on the position of your Tomcat installation, and your application should not write into the Tomcat installation folder, never. It is quite possible that the Tomcat installation is not even writable for the user running Tomcat in a production environment.

Therefore, use full (absolute) paths only.

Michael Piefel
  • 18,660
  • 9
  • 81
  • 112
  • This application is just a proof of concept and will likely be run on other developers local tomcat installations. I understand why this would not be good for production. I will probably put an absolute path in a configuration file so this can be edited by the developer so he/she can use a path that they can read / write to. – csilk Aug 05 '12 at 20:24
2

To avoid file permission issues you may want to use the user's home directory using System.getProperty("user.home"). That way it will work consistently across environments and operating systems.

Uri Cohen
  • 331
  • 1
  • 4
  • This looks to be a better answer than the one I've accepted but I ended up implementing the suggestions in the other answer. Thanks for the help though. – csilk Oct 23 '12 at 12:25