problem Using getResourceAsStream()

Discussion in 'Java' started by i_lk, Oct 12, 2005.

  1. i_lk

    i_lk Guest

    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_lk, Oct 12, 2005
    #1
    1. Advertising

  2. i_lk

    Ross Bamford Guest

    On Wed, 12 Oct 2005 22:55:49 +0100, 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
    >


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

    --
    Ross Bamford -
    Ross Bamford, Oct 13, 2005
    #2
    1. Advertising

  3. i_lk

    Roedy Green Guest

    On Wed, 12 Oct 2005 19:39:38 -0700, Abhijat Vatsyayan
    <> 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.
    Roedy Green, Oct 13, 2005
    #3
  4. 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
    >
    Abhijat Vatsyayan, Oct 13, 2005
    #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:
    597
  2. Digby
    Replies:
    3
    Views:
    12,979
    Jon Skeet
    Oct 15, 2003
  3. F C
    Replies:
    2
    Views:
    10,768
    gpenia
    Mar 31, 2009
  4. khutch
    Replies:
    1
    Views:
    1,790
    khutch
    May 27, 2004
  5. Fencer
    Replies:
    19
    Views:
    3,171
    Arne Vajhøj
    Jan 30, 2010
Loading...

Share This Page