Getting path outside container

Hi, I want to be able to find the path of an application outside the container. For example I want to have a class that saves a file into the webapp foler (i.e. c/:tomcat/webapps/myapp). I don't want to hard code in c/:tomcat/webapps/myapp though. I wanto to be able to call like Config.getPath() or something to get it. How can I do this within Java? I am by no means a weak programmer but I don't know an elegant solution to this problem. Cheers.

Who is Participating?
Weiping DuConnect With a Mentor Senior Web DeveloperCommented:
File f=new File("");            
samblakeAuthor Commented:
Unfortunatly that gives me C:\Tomcat\bin. I was looking for a solution that would give me the context path too (so include the webapps/myWebApp path).
For JSP  you can use  
String path = application.getRealPath("/");  
for a servlet use  
String path = getServletContext().getRealPath("/");  
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

samblakeAuthor Commented:
I repeat; outside the container
What does my path return ?
What is the path you are looking for ?
Weiping DuSenior Web DeveloperCommented:
Sam is saying his Java class is a sort of util Java class used for write data into a file.  It is not a Servlet or JSP and it cannot get Servlet context.   I am not sure how to do that and it may need to pass path to class as parameter.
owen, thanks for clearing that up.  But, I guess the class could always request the path from a servlet.
as rrz has already mentioned, the standard method would be to use application.getRealPath("/");  As it returns an absolute path, you can then navigate outside the container with ease. All you would need to do is pass this parameter in to the class that does the writing as I assume that you are calling it from a servlet or JSP.

Other options include using init parameters, properties files, or JNDI resources.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.