Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   ClassLoader.getResourceAsStream (http://www.velocityreviews.com/forums/t127617-classloader-getresourceasstream.html)

Digby 10-14-2003 06:45 PM

ClassLoader.getResourceAsStream
 
Hi all,

I've just written some code (see below) which relies on the
getResourceAsStream of the ClassLoader object to get a file as an
InputStream. Strangely, though it works when I run it locally, when I stick
the code and the file on a Web server, I get null returned instead. I guess
it's a classpath issue, but I thought that this method was supposed to get
round that.

Classes:

package a.b.c;
public class D {
public InputSteam getfileAsStream() {
return ClassLoader.getResourceAsStream("filename.txt");
}
}

package a.b;
public class E {
public static void main(String[] args) {
D d = new D();
System.out.println(d.getFileAsStream());
}
}

File structure:

a/b/c/D
a/b/c/filename.txt
a/b/E

I am executing E.

Any ideas? The Web server is WebSphere 3.5.

TIA

Dig


---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.525 / Virus Database: 322 - Release Date: 10/10/2003



Jon Skeet 10-14-2003 08:04 PM

Re: ClassLoader.getResourceAsStream
 
Digby <digbyk@hotmail.com> wrote:
> I've just written some code (see below) which relies on the
> getResourceAsStream of the ClassLoader object to get a file as an
> InputStream. Strangely, though it works when I run it locally, when I stick
> the code and the file on a Web server, I get null returned instead. I guess
> it's a classpath issue, but I thought that this method was supposed to get
> round that.
>
> Classes:
>
> package a.b.c;
> public class D {
> public InputSteam getfileAsStream() {
> return ClassLoader.getResourceAsStream("filename.txt");
> }
> }


That won't actually compile, as getResourceAsStream isn't a static
method. However, if you want to get it relative to the current package,
you need Class.getResourceAsStream rather than ClassLoader anyway. I'm
surprised it worked at all.

Are you sure you don't have another version of D in another jar file
somewhere? I suggest you look at what getResource("/a/b/c/D.class");
gives as a full URL, and check that it looks right.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Digby 10-14-2003 09:52 PM

Re: ClassLoader.getResourceAsStream
 
Good point - I was actually using ClassLoader.get*System*ResourceAsStream()
which is static, but I'll try Class.getResourceAsStream() and see if it
helps.
Thanks a lot.
Dig

"Jon Skeet" <skeet@pobox.com> wrote in message
news:MPG.19f66739bae1133e9896b3@10.1.1.14...
> Digby <digbyk@hotmail.com> wrote:
> > I've just written some code (see below) which relies on the
> > getResourceAsStream of the ClassLoader object to get a file as an
> > InputStream. Strangely, though it works when I run it locally, when I

stick
> > the code and the file on a Web server, I get null returned instead. I

guess
> > it's a classpath issue, but I thought that this method was supposed to

get
> > round that.
> >
> > Classes:
> >
> > package a.b.c;
> > public class D {
> > public InputSteam getfileAsStream() {
> > return ClassLoader.getResourceAsStream("filename.txt");
> > }
> > }

>
> That won't actually compile, as getResourceAsStream isn't a static
> method. However, if you want to get it relative to the current package,
> you need Class.getResourceAsStream rather than ClassLoader anyway. I'm
> surprised it worked at all.
>
> Are you sure you don't have another version of D in another jar file
> somewhere? I suggest you look at what getResource("/a/b/c/D.class");
> gives as a full URL, and check that it looks right.
>
> --
> Jon Skeet - <skeet@pobox.com>
> http://www.pobox.com/~skeet
> If replying to the group, please do not mail me too



---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.525 / Virus Database: 322 - Release Date: 09/10/2003



Jon Skeet 10-15-2003 03:36 AM

Re: ClassLoader.getResourceAsStream
 
Digby <digbyk@hotmail.com> wrote:
> Good point - I was actually using ClassLoader.get*System*ResourceAsStream()
> which is static, but I'll try Class.getResourceAsStream() and see if it
> helps.


Ah - it *absolutely* will! getSystemResourceAsStream only gets
resources in the main classpath, rather than any resources loaded by
the specified classloader (or the one that loaded the class you call
getResourceAsStream on).

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


All times are GMT. The time now is 08:06 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.