Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2005

Tip: Looking for answers? Try searching our database.

problem Using getResourceAsStream()

Thread view: 
i_lk - 12 Oct 2005 22:55 GMT
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
Abhijat Vatsyayan - 13 Oct 2005 00:39 GMT
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

> hi
> i m using JBuilderX... the problem i m facing is very basic yet i
[quoted text clipped - 22 lines]
>
> Thanks a lot
Roedy Green - 13 Oct 2005 01:42 GMT
> 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();
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.

Ross Bamford - 13 Oct 2005 00:51 GMT
> hi
> i m using JBuilderX... the problem i m facing is very basic yet i
[quoted text clipped - 22 lines]
>
> 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 ;)

Signature

Ross Bamford - rosco@roscopeco.remove.co.uk

gbruno - 15 Nov 2005 03:14 GMT
Slightly off topic,
I have success showing rtf files in a JEditorPane loaded from a jar
I use the "stub"  ResourceAnchor class, I'm not sure why,
but I was told it was necessary for webservices, which I dont use at present..
.

Here are some code scraps:

     ClassLoader cl = ResourceAnchor.class.getClassLoader();  //webservices
version: // Get current classloader

 RTFEditorKit rtf;
 JEditorPane rtfeditor;
..
   rtf = new RTFEditorKit();
   rtfeditor = new JEditorPane();  
   rtfeditor.setEditorKit(rtf);
   rtfeditor.setBackground(Color.white);
   rtfeditor.setEditable(true);
   rtfeditor.setMinimumSize(new Dimension(42, 42));
   Insets i = new Insets(5, 5, 5, 21); // draw to fill the entire component
   // some tinkering with margins, which is why Editable is set true (?)
   rtfeditor.setMargin(i);
..
   String ozall = docdir + rtfname;   //  "doc/somename.rtf"   someone said
"dont use root"
..
   InputStream in = cl.getResourceAsStream(ozall);   // get the rtf files
from the jar
..    
   rtf.read(in, rtfeditor.getDocument(), 0);

[not so lucky attempting to show pdf in java, so for that I spawn an external
viewer]

cheers
gbruno

ps I havnt tried this remotely yet,

Signature

gbruno
http://gbruno2.blogspot.com/



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.