Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > problem Using getResourceAsStream()

Reply
Thread Tools

problem Using getResourceAsStream()

 
 
i_lk
Guest
Posts: n/a
 
      10-12-2005
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

 
Reply With Quote
 
 
 
 
Ross Bamford
Guest
Posts: n/a
 
      10-12-2005
On Wed, 12 Oct 2005 22:55:49 +0100, i_lk <(E-Mail Removed)> 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 - http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      10-13-2005
On Wed, 12 Oct 2005 19:39:38 -0700, Abhijat Vatsyayan
<(E-Mail Removed)> 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.
 
Reply With Quote
 
Abhijat Vatsyayan
Guest
Posts: n/a
 
      10-13-2005
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
>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem using problem reporting Kue2 Windows 64bit 3 01-15-2010 08:07 PM
Problem problem problem :( Need Help Mike ASP General 2 05-11-2004 08:36 AM
Problem connecting to https using ZSI (openssl problem) - python2.3 Adil Hasan Python 3 01-29-2004 12:27 AM
Using GetOleDbSchemaTable to get SQL Server Field Description - using pete ASP .Net 1 08-29-2003 10:50 AM
Re: MVP? Index error on nested element using System.xml but NOT using msxml??? William F. Robertson, Jr. ASP .Net 1 06-25-2003 08:08 PM



Advertisments