![]() |
problem Using getResourceAsStream()
hi
i m using JBuilderX... the problem i m facing is very basic yet i could'nt find the sol...i m retrieving the contents of an xml file using InputStream is = another_class.getResourceInputStream("xyz.XML"); In another_class:- protected static InputStream getResourceInputStream(String xml_file) throws IOException { System.out.println(xml_file) ; ClassLoader cl = ResourceManager.class.getClassLoader(); return cl.getResourceAsStream(xml_file); } the code is fine but it cant get the xyz.xml file..and it returns NULL. i have used every option... i want to keep the file in a separate directory in project files say dir1 .... what should i give in the path(as an argument)... how can i get the path of that file as a part of my program...? Thanks a lot |
Re: problem Using getResourceAsStream()
On Wed, 12 Oct 2005 22:55:49 +0100, i_lk <afia_zafar82@yahoo.com> wrote:
> hi > i m using JBuilderX... the problem i m facing is very basic yet i > could'nt find the sol...i m retrieving the contents of an xml file > using > > > InputStream is = another_class.getResourceInputStream("xyz.XML"); > > > In another_class:- > > > protected static InputStream getResourceInputStream(String xml_file) > throws IOException { > System.out.println(xml_file) ; > ClassLoader cl = ResourceManager.class.getClassLoader(); > return cl.getResourceAsStream(xml_file); > } > > the code is fine but it cant get the xyz.xml file..and it returns > NULL. > > i have used every option... i want to keep the file in a separate > directory in project files say dir1 .... what should i give in the > path(as an argument)... how can i get the path of that file as a part > of my > program...? > > Thanks a lot > I can see that, as you say, the code is fine, except it's not working... :) If the resource is on the classpath, you'll want to think about prefixing the filename with a '/'. If, however, it's in a file outside the classpath, as you indicate toward the end of your message, then have a look at http://java.sun.com/j2se/1.5.0/docs/...putStream.html instead. You might want to consider renaming that 'another_class' before anyone else notices it, too ;) -- Ross Bamford - rosco@roscopeco.remove.co.uk |
Re: problem Using getResourceAsStream()
On Wed, 12 Oct 2005 19:39:38 -0700, Abhijat Vatsyayan
<abhijatdotvatsyayan@bmsdotnospam.com> wrote or quoted : > ClassLoader cl = ResourceManager.class.getClassLoader(); >> return cl.getResourceAsStream(xml_file); I think in your jar you need a resource named something like this: com/bms/rm/xyz.XML I would need to know ResourceManager's full class name to give it to you precisely. you can also shorten that to: ResourceManger.class.getResourceAsStream(); -- Canadian Mind Products, Roedy Green. http://mindprod.com Again taking new Java programming contracts. |
Re: problem Using getResourceAsStream()
In all probability, this is a classpath issue. getResourceAsStream in
the simplest case, uses classpath of the classloader to search for the named resource. If we represent all files as (d,f) where "d+<system dependent file path separator>+f" is the absolute path of the file and you want to load "f" using getResourceAsStream(f) , "d" (which is a directory) must be in the classpath of the classloader being used to locate the resource. Note that custom classloader implementations might change the findResource implementation to do custom handling. In this case, you will need to know how your classloader is locating(loading) resources . Your code does not provide us with any information regarding your classloader, classpath and directory structure. Hence it is difficult for me to get more specific. Abhijat i_lk wrote: > hi > i m using JBuilderX... the problem i m facing is very basic yet i > could'nt find the sol...i m retrieving the contents of an xml file > using > > > InputStream is = another_class.getResourceInputStream("xyz.XML"); > > > In another_class:- > > > protected static InputStream getResourceInputStream(String xml_file) > throws IOException { > System.out.println(xml_file) ; > ClassLoader cl = ResourceManager.class.getClassLoader(); > return cl.getResourceAsStream(xml_file); > } > > the code is fine but it cant get the xyz.xml file..and it returns > NULL. > > i have used every option... i want to keep the file in a separate > directory in project files say dir1 .... what should i give in the > path(as an argument)... how can i get the path of that file as a part > of my > program...? > > Thanks a lot > |
| All times are GMT. The time now is 08:25 PM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.