Solved

Getting path outside container

Posted on 2006-07-21
8
345 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 75 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 27

Expert Comment

by:rrz
ID: 17159921
For JSP  you can use  
String path = application.getRealPath("/");  
for a servlet use  
String path = getServletContext().getRealPath("/");  
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 1

Author Comment

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

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 27

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Read about the ways of improving workplace communication.
Knowing where your website is hosted is as important as the features you receive, the monthly fee, and the support you receive. Due diligence should be done when choosing your next hosting provider.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

776 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