1

I have searched Google, SE, and Oracle's documentation for information on how to do this, but I can't seem to make it work.

I have found several posts regarding whether to use getResource() or getResourceAsStream() and how to format the file name, but none that I can apply to my situation.

My directory structure looks like this (with some extra levels in the resource folder):

Root Folder
|
|_____ bin (compiled .class files go here)
|
|_____ src
|         |
|         |_____ packageA
|         |         |
|         |         |_____ packageAa
|         |                   |
|         |                   |_____ images.java
|         |                   |
|         |                   |_____ resources
|         |                             |
|         |                             |_____ icon1.gif
|         |
|         |_____ packageB
|         |
|         ... (More packages with sub-packages)
|
|_____ resources (should it go here?)
          |
          |_____ icon2.gif

In my actual program I have 107 gif images with file names that can be derived from object values. These images are currently located in both resource folders because I have been playing around with both to try to get one to work.

My current code tries this:

String imageFileName = getFileName(obj);  // returns a string such as "/resources/cardImages/acespades.gif"
URL imageURL = getClass().getClassLoader().getResource(imageFileName);
ImageIcon icon = new ImageIcon(imageURL);

I keep getting a null pointer exception regardless of leading backslashes.

How should I format the file name and or create a URL object to get the images?

Note: I am using Sublime Text 3 with Javatar to compile my code. The folder structure (the bin folder) is created by Javatar. My "Source folder" is set to src and the "root folder" shown above is my flash drive.

====================================================
Edit:

I tried Peter's suggestions and these are the results:

URL imageURL = getClass().getResource("resources/cardImages/back1.gif");
System.out.println(imageURL);
ImageIcon icon = new ImageIcon("resources/cardImages/back1.gif");

The println statement prints out null, but the ImageIcon does correctly display the image. I understand it this loads the image from the folder next to the .java file.

URL imageURL = getClass().getResource("/resources/cardImages/back1.gif");
System.out.println(imageURL);
ImageIcon icon = new ImageIcon("/resources/cardImages/back1.gif");

The println still prints null, but now the image is not displayed. I copied and pasted the entire resource folder to make sure it was identical in both locations.

Eric Roch
  • 125
  • 2
  • 10
  • How do you start your program? What is the classpath? – Mike Tunnicliffe Mar 27 '15 at 18:43
  • I start the class using Javatar. This is the project page for reference: http://javatar.readthedocs.org/en/latest/index.html – Eric Roch Mar 27 '15 at 19:02
  • Are you sure all the directory and image file names are all lower case? BTW - *"I have searched Google, SE, and Oracle's documentation for information on how to do this, but I can't seem to make it work. I have found several posts .. but none that I can apply to my situation."* plus one for **demonstrating** in your question that you understood that the directory structure was vital. I wish I had a dollar for every 'cannot find image' where the OP claimed to have done extensive research yet left out that basic and vital info. – Andrew Thompson Mar 28 '15 at 02:30
  • @AndrewThompson The image files were saved as .GIF, which I don't imagine would matter, but I changed them to .gif anyways. The entire file path is lowercase, except folders higher than my project folder (my flashdrive has several folders before my Java projects). Are these important too? – Eric Roch Mar 29 '15 at 01:31
  • *"Are these important too?"* No. – Andrew Thompson Mar 29 '15 at 01:33

1 Answers1

3

When loading resources over a class, the path is relative to the package path, unless you have a leading slash /.

As for your icon1.gif, using

URL url = getClass().getResource("resources/icon1.gif");

should return the correct URL.

I recommend keeping the Java sources and resources separate. If resources is a source folder (whose output folder is the root of bin/the JAR), use:

URL url = getClass().getResource("/icon2.gif");

(note the leading slash).


EDIT (question updated):
Corrected the second URL (resources is a source folder, and icon2.gif lies in its root).
I just verified it in Eclipse, and I get the correct (non-null) URLs for both variants. Using the URLs in ImageIcons also gives the correct results, the images are shown.

  • Make sure that resources is a source folder. Not all project files land in the output folder (usually bin) and later the JAR.
  • Check your output folder to verify it contains the compiled classes as well as the resources. Source and output folders (Eclipse)
Peter Walser
  • 15,208
  • 4
  • 51
  • 78
  • 1
    Ok, so it turns out that the "IDE" I am using doesn't handle resources correctly. When I compile my code, the program creates a `bin` folder at the same level as the source folder. It does not, however check for resources and place those in the bin folder as well. When I copied the `resources` folder to `bin`, everything worked like a charm – Eric Roch Mar 30 '15 at 21:14