Help with getClass().getResourceAsStream() in static context

Discussion in 'Java' started by Knute Johnson, Mar 31, 2006.

  1. I've got a Swing app where I need to load an image out of the jar file
    when the GUI is created. As a result I have no 'this' to call
    getClass() on. How do I get a class that I can call
    getResourceAsStream() on in a static context?

    --

    Knute Johnson
    email s/nospam/knute/
     
    Knute Johnson, Mar 31, 2006
    #1
    1. Advertising

  2. Knute Johnson wrote:
    > I've got a Swing app where I need to load an image out of the jar file
    > when the GUI is created. As a result I have no 'this' to call
    > getClass() on. How do I get a class that I can call
    > getResourceAsStream() on in a static context?
    >


    Stupid me, you can get there from ClassLoader too.

    --

    Knute Johnson
    email s/nospam/knute/
     
    Knute Johnson, Mar 31, 2006
    #2
    1. Advertising

  3. Knute Johnson wrote:
    > Knute Johnson wrote:
    >> I've got a Swing app where I need to load an image out of the jar file
    >> when the GUI is created. As a result I have no 'this' to call
    >> getClass() on. How do I get a class that I can call
    >> getResourceAsStream() on in a static context?
    >>

    >
    > Stupid me, you can get there from ClassLoader too.
    >


    Turns out that doesn't work. Help!

    import java.io.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.image.*;
    import javax.imageio.*;

    public class test {
    public static void main(String[] args) {
    Runnable r = new Runnable() {
    public void run() {
    try {
    BufferedImage bi = null;
    InputStream is = ClassLoader.getSystemClassLoader().
    getResourceAsStream("background.jpg");
    if (is == null)
    System.out.println(is);
    else
    bi = ImageIO.read(is);
    } catch (IOException ioe) { ioe.printStackTrace(); }
    }
    };
    EventQueue.invokeLater(r);
    }
    }

    --

    Knute Johnson
    email s/nospam/knute/
     
    Knute Johnson, Mar 31, 2006
    #3
  4. Knute Johnson wrote:
    > Knute Johnson wrote:
    >> Knute Johnson wrote:
    >>> I've got a Swing app where I need to load an image out of the jar
    >>> file when the GUI is created. As a result I have no 'this' to call
    >>> getClass() on. How do I get a class that I can call
    >>> getResourceAsStream() on in a static context?
    >>>

    >>
    >> Stupid me, you can get there from ClassLoader too.
    >>

    >
    > Turns out that doesn't work. Help!


    I love it when I answer my own questions just not when I'm talking to
    myself in public. The answer is:

    test.class.getResourceAsStream()

    'test' being the static class name.

    --

    Knute Johnson
    email s/nospam/knute/
     
    Knute Johnson, Mar 31, 2006
    #4
  5. Knute Johnson

    Roedy Green Guest

    On Thu, 30 Mar 2006 16:15:49 -0800, Knute Johnson
    <> wrote, quoted or indirectly quoted someone
    who said :

    >
    >I love it when I answer my own questions just not when I'm talking to
    >myself in public. The answer is:
    >
    >test.class.getResourceAsStream()


    Alternatively, you could have looked in the Java glossary under
    either resource or getResourceAsStream.

    http://mindprod.com/jgloss/resource.html
    http://mindprod.com/jgloss/getresourceasstream.html
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
     
    Roedy Green, Mar 31, 2006
    #5
  6. Knute Johnson, Mar 31, 2006
    #6
    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. Gary
    Replies:
    1
    Views:
    4,062
    Ryan Stewart
    Jan 16, 2004
  2. Attiq ur Rehman
    Replies:
    4
    Views:
    2,739
    Malte
    Mar 4, 2005
  3. Boki
    Replies:
    34
    Views:
    28,090
  4. www
    Replies:
    1
    Views:
    799
    Manish Pandit
    Jan 26, 2007
  5. zyng
    Replies:
    6
    Views:
    777
Loading...

Share This Page