• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to read a resource file from war file ??

 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Javan Li
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nobody know???
 
Ranch Hand
Posts: 2596
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks very much, David !
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic