0

I've been trying to load this image.png file that's located in my images folder:

In eclipse I've made a folder in my src folder called images and put the image there, so it should be part of my classpath.

However, when I try to use this image it does not work:

I've tried these:

Image image; 
String craft = "myApp/src/images/image.png";
ImageIcon ii = new ImageIcon(craft); 
        image = ii.getImage();

Image image; 
String craft = "myApp/src/images/image.png"; 
ImageIcon ii = new ImageIcon(this.getClass().getResource(craft));
        image = ii.getImage();

And various variations for String craft:

"images/image.png", "/images/image.png", "/myApp/src/images/image.png"
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Harrison
  • 430
  • 2
  • 6
  • 13
  • Do a `jar -tvf the.jar` on the Jar that is supposed to contain the image. I bet it is either a) Not in the Jar or more likely b) not at the path you specified. – Andrew Thompson Dec 02 '13 at 07:16
  • @AndrewThompson Who says it's a jar? – Boann Dec 02 '13 at 07:19
  • 1
    I might suggest that eclipse doesn't include your `src` path as part of it's classpath. Instead you should consider create a "resources" directory within your project and adding your resources to it. To be generally safer, using `/` is a good idea when using `getClass().getResource(...)`, so long as the path to the resources is absolute... – MadProgrammer Dec 02 '13 at 07:40
  • @Boann It was the same person that told *you* the OP needed ***you*** to ask obtuse questions on their behalf, as opposed to the (much more sensible) route of them ..just telling me that themselves. – Andrew Thompson Dec 02 '13 at 08:22

3 Answers3

2

If you have the image in:

src/images/foo.png

And use this code:

ImageIcon ii = new ImageIcon("images/foo.png");
Image image = ii.getImage();
System.out.println(image != null);

The output will be:

true

... that is, the Image is not null... ... if your classpath is the default build path eclipse provides when creating your project (note: should be the same with Netbeans, etc.).

If you're using a different building script, be it Ant or Maven, you might need to change the path. You will certainly if it's a web application.

Additional notes

  • Here is a good SO thread on resource paths for web applications.
  • Here is the typical Maven folder layout - useful to get you started if you use Maven as your build/package script.
Community
  • 1
  • 1
Mena
  • 47,782
  • 11
  • 87
  • 106
0

Whenever you are trying to load a resource in classpath, you need to specify the correct relative path. if you have your images folder in classpath as that of the classes, then you have to load it as follows

String craft = "images/image.png"; 
ImageIcon ii = new ImageIcon(this.getClass().getResource(craft));

or if you have all the files in images folder in classpath, you can just specify the file name directly as well

   String craft = "image.png"; 
    ImageIcon ii = new ImageIcon(craft);

So, i am sure the path you have given is wrong and that's why the resource is not getting loaded

Keerthivasan
  • 12,760
  • 2
  • 32
  • 53
0

Try this out

String craft = "images/image.png"; 
ImageIcon ii = new ImageIcon(craft);

File structure

ProjectRoot
          images
               imgage.png
          src
          bin
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720