?
Solved

Getting path outside container

Posted on 2006-07-21
8
Medium Priority
?
350 Views
Last Modified: 2010-08-05
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.

-Sam
0
Comment
Question by:samblake
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 9

Accepted Solution

by:
owenli27 earned 225 total points
ID: 17158640
File f=new File("");            
System.out.println(f.getAbsolutePath());
0
 
LVL 1

Author Comment

by:samblake
ID: 17159527
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).
0
 
LVL 28

Expert Comment

by:rrz
ID: 17159921
For JSP  you can use  
String path = application.getRealPath("/");  
for a servlet use  
String path = getServletContext().getRealPath("/");  
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:samblake
ID: 17161749
I repeat; outside the container
0
 
LVL 28

Expert Comment

by:rrz
ID: 17161859
What does my path return ?
What is the path you are looking for ?
0
 
LVL 9

Expert Comment

by:owenli27
ID: 17162419
rrz@871311,
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.
 
0
 
LVL 28

Expert Comment

by:rrz
ID: 17164156
owen, thanks for clearing that up.  But, I guess the class could always request the path from a servlet.
0
 
LVL 29

Expert Comment

by:bloodredsun
ID: 17171099
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.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article lists the top 5 free OST to PST Converter Tools. These tools save a lot of time for users when they want to convert OST to PST after their exchange server is no longer available or some other critical issue with exchange server or impor…
Introducing Priority Question, our latest feature.
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question