PDA

View Full Version : Java and missing images


Nylex
Apr9-05, 04:44 PM
Hi, I'm working through the "Building an Application: Part 2: Introduction to Inheritance, Panels, and Layouts" (http://java.sun.com/developer/onlineTraining/new2java/divelog/part2/page6.jsp) on java.sun.com and the code I've written compiles fine, but the images that are supposed to appear in the JPanel aren't there.

jl = new JLabel("Java Technology Dive Log", new ImageIcon("images/diveflag.gif"), JLabel.CENTER);

and

diver = new JLabel("", new ImageIcon("images/diver.jpg"), JLabel.CENTER);

are where images are used.

The image files do exist. They're in a folder called "images", which itself is in a folder named "divelog". The whole code for the file is here (http://java.sun.com/developer/onlineTraining/new2java/divelog/part2/Welcome.java), although obviously this class is used in another.

If anyone can help, thanks.

wave
Apr9-05, 05:46 PM
Did you add those 3 lines to Welcome.java?


add(jl, BorderLayout.NORTH);
add(ta, BorderLayout.CENTER);
add(diver, BorderLayout.SOUTH);

Nylex
Apr9-05, 06:12 PM
Yep, I did.

faust9
Apr9-05, 06:36 PM
Do you have your images folder and java .jar file in the same directory?

Nylex
Apr10-05, 03:10 AM
Do you have your images folder and java .jar file in the same directory?

What .jar file?

so-crates
Apr10-05, 03:34 AM
I would use an absolute path to double check that is not a working directory problem. That would at least eliminate one possibility.

Nylex
Apr10-05, 03:38 AM
I would use an absolute path to double check that is not a working directory problem. That would at least eliminate one possibility.

It works with an absolute path, so what else could be the problem?

so-crates
Apr10-05, 03:46 AM
Well there you go. Your working directory is not what you think it is !

Nylex
Apr10-05, 03:47 AM
Why's it not the current directory?!

so-crates
Apr10-05, 03:49 AM
I believe its usually the directory in which the class file is located, though I could be wrong. Check the documentation on Sun's site: http://java.sun.com.

Nylex
Apr10-05, 03:50 AM
Will do, thanks :smile:.

so-crates
Apr10-05, 03:50 AM
Actually I think its the directory in which the interpreter is run. How are you running the interpreter?

Nylex
Apr10-05, 04:27 AM
Actually I think its the directory in which the interpreter is run. How are you running the interpreter?

I'm using BlueJ (http://www.bluej.org). I'll try from the command line and see if that makes any difference.

Edit: I can't run from the command line. I'm getting:

Exception in thread "main" java.lang.NoClassDefFoundError: DiveLog (wrong name:
divelog/DiveLog)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknow n Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)

when I try "java DiveLog". The class name is DiveLog, so I don't know what the problem is. It works in BlueJ though :confused:.

mtpoland
Aug21-08, 10:03 PM
New to the forum here.

I'm having the same problem with the images not being displayed.

Has anyone figured out the cure?

Mike

BryanP
Aug22-08, 01:03 AM
The interpreter's location is irrelevant because you can have work-related material in other directories.

A standard IDE for Java such as Eclipse can obviously be in a different location (such as c:\eclipse) and compile things fine from other directories because the compiler KNOWS where to find the standard libraries.

The thing with images is that they have to be in the /same/ folder as your classes, otherwise you have to explicitly define their entire paths.

If it's in a folder, then the folder has to be in the same folder as your main/test class.

If your main class as well as other classes are in lets say, C:\class

then the images should be in C:\class\images\<imageFile>