File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes How to read a resource file from war file ?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "How to read a resource file from war file ??" Watch "How to read a resource file from war file ??" New topic
Author

How to read a resource file from war file ??

Javan Li
Ranch Hand

Joined: Jul 24, 2002
Posts: 84
Hi all, i've developed an application. But when i make it a war file. some error appear. for example:
When i try to read a xml file from /WEB-INF/xml/ as follow, it cannot find it:
String dir=context.getRealPath("/");
String file=dir + "/WEB-INF/xml/res.xml";
and this code doesn't work. What should i do??

i appreciate if u can help u!
Rangarajan Suresh
Ranch Hand

Joined: Jan 08, 2002
Posts: 50
Originally posted by Javan Li:
Hi all, i've developed an application. But when i make it a war file. some error appear. for example:
When i try to read a xml file from /WEB-INF/xml/ as follow, it cannot find it:
String dir=context.getRealPath("/");
String file=dir + "/WEB-INF/xml/res.xml";
and this code doesn't work. What should i do??

i appreciate if u can help u!


getRealPath() gives the path like this...
D:\tomcat4\webapps\examples\
remove the / in "/WEB-INF/xml/res.xml"
It should work.
Carl Trusiak
Sheriff

Joined: Jun 13, 2000
Posts: 3340
If what you are after is to load the file using File. You need a correctly formed path. One of the things you need to get is the proper file seperator char for the system you are on.

However, if what you really want is the InputStream from the file, try

[ August 22, 2003: Message edited by: Carl Trusiak ]

I Hope This Helps
Carl Trusiak, SCJP2, SCWCD
Javan Li
Ranch Hand

Joined: Jul 24, 2002
Posts: 84
Thanks very much!
But i guess you misunderstand me. What i mean is context.getRealPath("/") cannot return a path , but a null. Because if u use a war file instead of expanded directory , getRealPath(String s) doesn't work. then what can i do??

getRealPath
public String getRealPath(String path)
This method returns null if the servlet container cannot translate the virtual path to a real path for any reason (such as when the content is being made available from a .war archive).

Thanks!
Javan Li
Ranch Hand

Joined: Jul 24, 2002
Posts: 84
Nobody know???
Manish Hatwalne
Ranch Hand

Joined: Sep 22, 2001
Posts: 2581

Tomcat does expand the .war file, so you won't have this problem with Tomcat. But if you are using other container (e.g. weblogic), then .war file will not be expanded and you'll indeed get "null" returned by getRealPath(). In this case, a workaround would be to keep your xml file outside WEB-INF, and treat it as a normal URL, you might need to get input stream on this URL to read it depending on your requirement.
HTH,
- Manish
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

if you place it in the web-inf/classes, the file will be available on the web classpath and therefore available via ClassLoader.getResource(), but I believe the best suggestion so far is Carl's:

This solution shouldn't matter whether the file is in a war or on the file system.
Dave
darius darius
Greenhorn

Joined: Aug 04, 2003
Posts: 1
Originally posted by Javan Li:
Hi all, i've developed an application. But when i make it a war file. some error appear. for example:
When i try to read a xml file from /WEB-INF/xml/ as follow, it cannot find it:
String dir=context.getRealPath("/");
String file=dir + "/WEB-INF/xml/res.xml";
and this code doesn't work. What should i do??

i appreciate if u can help u!
Javan Li
Ranch Hand

Joined: Jul 24, 2002
Posts: 84
Thanks very much! I'm using jboss-3.2.1_tomcat-4.1.24, but it didn't expand the war archive.
And i use getResource(String path) instead of getResourceAsStream(String str) , any difference ???
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

It should be fine, since it returns a URL and not a File. A URL can still provide a reference to resources inside a WAR, but a File cannot.
Dave
Javan Li
Ranch Hand

Joined: Jul 24, 2002
Posts: 84
Thanks very much, David !
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to read a resource file from war file ??