File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes URL - Placement of Image Files Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "URL - Placement of Image Files" Watch "URL - Placement of Image Files" New topic

URL - Placement of Image Files

Landon Blake
Ranch Hand

Joined: Oct 15, 2004
Posts: 44
I've got the following statement in a class that creates an imageIcon:

"ImageIcon hiImage = new ImageIcon("images/hi.jpg");"

In order for this image to be loaded when the program is executed, I know that the "hi.jpg" file needs to be in a folder named "images". But where do I put that folder? Do I need to somehow zip it up in the JAR file for my finished program? I'm a little confused about where the JVM is going to look for the "images" folder....

Thanks for the help,

Ransika deSilva
Ranch Hand

Joined: Feb 18, 2003
Posts: 524
Hi Landon Blake,
What the JVM does is it goes to the default directory in search of the source files and images etc. The dafult directory can be the JAVA_HOME\bin or the folder that you have mentioned when setting the CLASS path. In your case the image folder should reside inside this default folder.
Hope I was able to clear your doubt. Just reply if you need any more clarifications....

SCJP 1.4, SCMAD 1.0<br />SCWCD, SCBCD (in progress)
Landon Blake
Ranch Hand

Joined: Oct 15, 2004
Posts: 44

First, I want to thank you for your response. It did answer my original question. However, it did raise another question.

If I package up my program into a jar file, "test.jar", and I want a user on another computer to execute the jar file and run my program, where do I place the images so that they display properly in the Swing GUI? Must my program install a directory on the users file system that is in the classpath? Isn't there a way to "wrap up" the images in the jar file so the JVM looks for the files in the jar itsel or in the same directory that the jar is in? (Or perhaps I can package the jar and the directory for the images in a zip file. When the user unzips the file, the directory with the images and the jar file is extracted to the same location on the file system. Would this work?)


I agree. Here's the link:
subject: URL - Placement of Image Files
It's not a secret anymore!