?
Solved

Getting path outside container

Posted on 2006-07-21
8
Medium Priority
?
357 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
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

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

Often times it's very very easy to extend a volume on a Linux instance in AWS, but impossible to shrink it. I wanted to contribute to the experts-exchange community a way of providing a procedure that works on an AWS instance. It can also be used on…
Strategic internal linking is often considered an SEO power technique, especially for content marketing. Do you need to hire an SEO agency to optimize you internal linking? No, this article will help you understand the basics of internal linking and…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

807 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