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

1
  • Why you are not using a document-oriented database like MongoDB? Commented Aug 5, 2012 at 20:58

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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.
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.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.