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

K

Knute Johnson

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?
 
K

Knute Johnson

Knute said:
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.
 
K

Knute Johnson

Knute said:
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);
}
}
 
K

Knute Johnson

Knute said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top