You'll want to get the image via the system class loader.
You'll want to get the image via the system class loader: URL url = ClassLoader. GetSystemClassLoader(). GetResource("images/images2.
Gif"); Icon icon = new ImageIcon(url) images is at the root of the classpath. Note that the Java runtime will translate the separator (/) to the OS specific separator (\ for Windows).
This is the correct answer, but for some reason the system won't let me vote up, saying my vote is too old. But I currently don't have a vote for this answer... – Jorn Dec 6 '09 at 11:42.
You need to access those files through class-loader... Something like this: InputStream is = this.getClass().getClassloader(). GetResourceAsStream("images/image. Ico"); HTH UPD: note, that this will work both with JARed package and with plain directory structure.
Eventually I add the ImageIcon to a JLabel. – tally Dec 6 '09 at 7:11 1 You don't the "this" in front of method invocations. – Steve Kuo Dec 6 '09 at 7:41 You should change it to getResource() instead of getResourceAsStream(), since ImageIcon takes an URL, not an InputStream (as suggested by Steve).
– Jorn Dec 6 '09 at 11:58.
The basic issue is that the File class only knows how to work with what the underlying operating system consider a file, and a whole one. A jar file is essentially a zip file with some extra information so you cannot use File's with that. Instead Java provides the "resource" concept which roughly translates to "a chunk of bytes or characters which we don't care where is, as long as we have them when we need them".
You can ask the class loader for any resource in the class path - which is what you want here - or access it through an URL.
Try this: ImageIcon icon = new ImageIcon(this.getClass().getClassloader(). GetResource("images/images2. Gif")); if that doesn't work, replace this.getClass().getClassloader() with MyClass.
Class where MyClass is the name of your class. I remember having to edit this slightly to make it work in Eclipse, but when you deploy it, it works like a charm. Edit: To make it work in Eclipse, you may need to change it to: ImageIcon icon = new ImageIcon(this.getClass().getClassloader().
GetResource("bin/images/images2. Gif")); If that doesn't work, do the standard, replace this.getClass().getClassloader() with MyClass.class. If it still doesn't work, try replacing "bin" with "src".
Try jar'ing it with the first way and see what happens.
– Laurence Gonsalves Dec 6 '09 at 7:01 I can't get access to a getClassLoader() function. I do have java.lang. * imported, but it doesn't help.
The MyClass. Class didn't work either. – tally Dec 6 '09 at 7:10 If you don't have a Class.getClassLoader() method, then you are not using a standards-conforming Java.
It is never necessary to import java. Lang either. I suspect that your code runs in a static context and what does not work is using "this" - as the answer says, use a class literal then.
– Michael Borgwardt Dec 6 '09 at 7:48.
Here is a convenient utility class that can be used for loading image resources. The log4j logger can be removed of changed to whatever is more appropriate. Public class ResourceLoader { private static final Logger logger = Logger.
GetLogger(ResourceLoader. Class); public static Image getImage(final String pathAndFileName) { try { return Toolkit. GetDefaultToolkit().
GetImage(getURL(pathAndFileName)); } catch (final Exception e) { logger. Error(e.getMessage()); return null; } } public static ImageIcon getIcon(final String pathAndFileName) { try { return new ImageIcon(getImage(pathAndFileName)); } catch (final Exception e) { logger. Error(e.getMessage()); return null; } } public static URL getURL(final String pathAndFileName) { return Thread.currentThread().
GetContextClassLoader(). GetResource(pathAndFileName); } }.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.