wood burning stoves 2.0*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes How can I find my images?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "How can I find my images??" Watch "How can I find my images??" New topic

How can I find my images??

leo tian

Joined: Jan 06, 2003
Posts: 1
i put my images in "images" folder..for example.i have one class in same folder use the image one.gif...when i compile it in default path..it work exactly...but when i add "package" to this class ...as following:
package test;
public class Test {...}
then i compile it to F:/, and also copy one.gif to f:/test....but after do such works,,,the class can't find the image....why??? what should i do??? please help me .....
Jai Karuppuswamy

Joined: Jan 22, 2002
Posts: 11
You need to pass in the path name of the image, from your working folder.
Here is how I use an image in my toolbar button.


If everything is under control, you're going too slow.<br /> Mario Andretti
jiaquan sun

Joined: Oct 05, 2002
Posts: 12
Would you tell me if i add images to the jar file,
how can i use these images?
Peter den Haan
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
Having images and other read-only resources on the filesystem is a Very Bad Idea IMHO. They should be packaged neatly inside the application jar. There are a number of methods in java.lang.Class (or java.lang.ClassLoader) that will help you.This call will return a URL pointing to the "/images/image.gif" file inside your application jar; you can use this URL in the ImageIcon constructor.This call will return an InputStream for the same file.
In the above snippets, we used the methods in java.lang.Class. So what's the difference with the methods of the same name in java.lang.ClassLoader?
In the code snippets so far, they would both work. The difference only shows up when you use relative paths, e.g. "images/image.gif" (note the lack of a slash "/" at the start). The callWill try to find a resouce relative to the given class, in this case, "/yourpackage/images/image.gif". This is not the case when using the ClassLoader calls, they always start at the CLASSPATH locations.
- Peter
[ January 07, 2003: Message edited by: Peter den Haan ]
jiaquan sun

Joined: Oct 05, 2002
Posts: 12
Peter,thank you very much!
can you give me more sample code?
which class does the 'Yourclass' means?
in this way,if user unpackage the jar file,what will happen?
will the application still run properly?
sorry for my english...
please help me.thanks!
Brian Blignaut
Ranch Hand

Joined: Jan 08, 2003
Posts: 61
This is the code I use to load Images. The class has a constant ICON_PREFIX which is the location of all my images.
private static final String ICON_PREFIX = "/suncertify/framework/gui/icons/";
public static Icon getIcon(String iconName) {
Icons icons = new Icons();
InputStream in = icons.getClass().getResourceAsStream(ICON_PREFIX + iconName);
if (in == null) {
System.out.println("No icon found for name " + iconName);
return null;
Icon icon = null;
try {
byte[] bytes = new byte[in.available()];
icon = new ImageIcon(bytes);
} catch (IOException e) {
System.out.println("Error reading image");
return icon;
Hope that helps
Peter den Haan
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
Brian, did you try getResource()? It might simplify your code.
Jiaquan, this method loads resources using the classloader; this means that as long as you can run the application (i.e. load the application classes) you will be able to load the resources. Regardless of whether they're in a jar or somewhere on the filesystem. And if you'd rewrite your application as an applet and load classes over the web, it would still work. See why this is best practice package resources?
- Peter
jiaquan sun

Joined: Oct 05, 2002
Posts: 12
Brian,Peter,I have looking for the solution for very long time.I am so happy to know this!
Samual Harvey
Ranch Hand

Joined: Aug 06, 2002
Posts: 119
What if the images are in a folder up in the directory hierarchy. Then can we say we can use getParent() on the File and get to the folder.


Samual Harvey<br />SCJP2<br />SCJD2
Peter den Haan
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
Originally posted by Samual Harvey:
What if the images are in a folder up in the directory hierarchy.
Up relative to what? It is best not to make any assumptions about the current working directory, and I wouldn't want to fix my images relative to the location of the database file.
- Peter
Aruna Raghavan
Ranch Hand

Joined: May 14, 2002
Posts: 194
You are awesome!
This works real well in my code with getResource().

Aruna A. Raghavan<br />SCJP, SCJD, SCWCD
I agree. Here's the link: http://aspose.com/file-tools
subject: How can I find my images??
Similar Threads
Where to store image files?
about splash screen with jar file...
Url from String
URL - Placement of Image Files
how to copy resources with maven?