problem Using getResourceAsStream()

I

i_lk

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
 
R

Ross Bamford

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/api/java/io/FileInputStream.html
instead.

You might want to consider renaming that 'another_class' before anyone
else notices it, too ;)
 
R

Roedy Green

ClassLoader cl = ResourceManager.class.getClassLoader();

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();
 
A

Abhijat Vatsyayan

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top