Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   problem Using getResourceAsStream() (http://www.velocityreviews.com/forums/t146893-problem-using-getresourceasstream.html)

i_lk 10-12-2005 09:55 PM

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


Ross Bamford 10-12-2005 11:51 PM

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

Roedy Green 10-13-2005 12:42 AM

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.

Abhijat Vatsyayan 10-13-2005 02:39 AM

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.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57