ClassLoader.getResourceAsStream

Discussion in 'Java' started by Digby, Oct 14, 2003.

  1. Digby

    Digby Guest

    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
    Digby, Oct 14, 2003
    #1
    1. Advertising

  2. Digby

    Jon Skeet Guest

    Digby <> 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 - <>
    http://www.pobox.com/~skeet
    If replying to the group, please do not mail me too
    Jon Skeet, Oct 14, 2003
    #2
    1. Advertising

  3. Digby

    Digby Guest

    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" <> wrote in message
    news:MPG.19f66739bae1133e9896b3@10.1.1.14...
    > Digby <> 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 - <>
    > 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
    Digby, Oct 14, 2003
    #3
  4. Digby

    Jon Skeet Guest

    Digby <> 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 - <>
    http://www.pobox.com/~skeet
    If replying to the group, please do not mail me too
    Jon Skeet, Oct 15, 2003
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. iksrazal
    Replies:
    1
    Views:
    602
  2. F C
    Replies:
    2
    Views:
    10,777
    gpenia
    Mar 31, 2009
  3. khutch
    Replies:
    1
    Views:
    1,794
    khutch
    May 27, 2004
  4. Roger Varley
    Replies:
    2
    Views:
    1,898
    Thomas Weidenfeller
    Feb 18, 2005
  5. zyng
    Replies:
    6
    Views:
    697
Loading...

Share This Page