Loading Images

Hello, I'm using NetBeans and trying to utilize Image Icons in my program. I am using the example code Sun provided in their Image Icon example program to load the images:

protectedstatic ImageIcon createAppImageIcon(String path)

{

int MAX_IMAGE_SIZE = 124000;// Change this to the size of

// your biggest image, in bytes

int count = 0;

BufferedInputStream imgStream =new BufferedInputStream(

Test.class.getResourceAsStream(path));

if (imgStream !=null){

byte buf[] =newbyte[MAX_IMAGE_SIZE];

try{

count = imgStream.read(buf);

}catch (IOException ieo){

System.err.println("Couldn't read stream from file: " + path);

}

try{

imgStream.close();

}catch (IOException ieo){

System.err.println("Can't close file " + path);

}

if (count <= 0){

System.err.println("Empty file: " + path);

returnnull;

}

returnnew ImageIcon(Toolkit.getDefaultToolkit().createImage(buf));

}else{

System.err.println("Couldn't find file: " + path);

returnnull;

}

}

This works perfectly when I'm simply compiling and running the program from the IDE (or any other compiler for that matter) however as soon as I make it into a Jar and attempt to run it from command prompt I begin to get errors. Namely I get the "Couldn't read stream from file" and "Empty file" errors. I assume it has something to do with including certain directories or something, but I can't for the life of me get it to work. I've tried creating Jars with NetBeans and JGrasp with the same outcome. I can find the files fine enough and even list them using the list() method, however no images will show up. Thanks for you help, I'm sure it's just something little I overlooked, it always seems to be.

[3162 byte] By [KyleMcGuinna] at [2007-11-27 3:53:57]
# 1
I don't know NetBeanz, but are the images being placed in the JAR file, and in the samerelative location as they are when running the code outside of the JAR?
DrLaszloJamfa at 2007-7-12 8:58:02 > top of Java-index,Java Essentials,Java Programming...
# 2
Yes, they are in the same relative location, I've opened the Jar file checked, and placed the images anywhere I could think to try and get it to work, still nothing.
KyleMcGuinna at 2007-7-12 8:58:02 > top of Java-index,Java Essentials,Java Programming...
# 3
Is your Test class in a package? Does that "path" variable start with a slash character? Read the documentation for Class's getResourceAsStream() if you don't see what I am getting at there.
DrClapa at 2007-7-12 8:58:02 > top of Java-index,Java Essentials,Java Programming...
# 4
Try putting the file, say "example.jpeg" in the same "folder" as Test.class. Then the path is just "example.jpeg". Watch out: paths are case sensitive in Jar files.
DrLaszloJamfa at 2007-7-12 8:58:02 > top of Java-index,Java Essentials,Java Programming...
# 5

No package, I've tried all sorts of slash characters with the directory and file. I've moved all the images into the same directory as the class file. I've also tried changing the getResourceAsStream() method call around, like:

this.getResourceAsStream(path));

getClass().getResourceAsStream(path));

Still no luck, I'll go glance over the documentation again to be sure I'm not missing something stupid, but I still don't see why it would work when I run it from the compiler and not from the Jar.

KyleMcGuinna at 2007-7-12 8:58:02 > top of Java-index,Java Essentials,Java Programming...