Solved

Getting path outside container

Posted on 2006-07-21
8
343 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
 
LVL 1

Author Comment

by:samblake
ID: 17161749
I repeat; outside the container
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Are you unable to connect or configure Hotmail email account in Microsoft Outlook 2010, 2007? Or Outlook.com emails are not downloading to Outlook? Lets’ see the problem and resolve Outlook Connector error syncing folder hierarchy (0x8004102A).
What is Backup? Backup software creates one or more copies of the data on your digital devices in case your original data is lost or damaged. Different backup solutions protect different kinds of data and different combinations of devices. For e…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now