3

I am developing a Java application which displays a big amount of images. The problem is I can't figure out how to make Java find these images.

I have followed several tutorials and answers here at Stackoverflow, but I still haven't managed to find a solution that works regardless of OS (Linux or Windows) and running method (embedded on eclipse or exported jar file). This might be due to the fact that I am still a newbie, though.

I have created a class, which I call myIcon and through this class I mean to access all of these images. In the following code, I want to pass the string "resources/icon/image.gif" to the function getIconPath. The output should be a ImageIcon of this image, since this file exists. Despite that, if I pass to this function the path to an image that doesn't exist, null should be returned. In this case, my application will display a default image (a red x).

public class MyIcon {
    // some other functions and properties

    private static final ImageIcon getIconPath(String path) {
        File f = new File(path);

        if (f.exists()) {
            return new ImageIcon(path, "");
        } else {
            return null;
        }
    }
}

The resources folder is a sibling to the src folder in my directory structure. That is, both resources and src are subfolders of the root directory.

When I run the code above, no image is ever found. The default image is thus always displayed and getIconPath returns null.

I am also aware of the getResource method of ClassLoader, but I still don't really understand how these things should be used.

diogo
  • 69
  • 1
  • 8
  • I believe you need to pass in a path from your system root (ex. C:/), not one relative to your project root. – Zarwan Aug 29 '15 at 21:14

3 Answers3

0

While running in eclipse, you should print out f.getAbsolutePath(). It probably doesn't point to your file. More generally, you want to access the file as a resource. See:

https://docs.oracle.com/javase/tutorial/deployment/webstart/retrievingResources.html

Diego Basch
  • 12,764
  • 2
  • 29
  • 24
0

There are two parts to this - making sure that when you build a jar, the images are part of it, and accessing a resource within the jar.

For the first part, I am guessing that whatever you're using to build a jar, it will put your images into the META-INF folder, which should work without too much issue (if its not there, or in with the Java class/source in the generated jar, that might cause the lookup to fail)

There is another Stack Overflow Post for the second part. The key is to make sure the images you want to load are on the classpath.

Hope this helps!

romeara
  • 1,426
  • 1
  • 17
  • 26
0

If resources is in classpath, you can find for the image at classpath, using getResource(String name) or getResourceAsStream(String name).

However, in a simple Java Project, even adding resources to build path, like this:

Java Build Path

it will just put the folder content, in other words, just icon/..., to bin folder, something like this:

Bin folder

So, since resources isn't in classpath, just icon, to retrieve the images, you'll need to inform as path only /icon/image.gif, like this:

URL url = YourClass.class.getResource("/icon/image.gif");
ImageIcon icon = new ImageIcon(url);
Bruno Ribeiro
  • 5,956
  • 5
  • 39
  • 48