0

I'm new to Java/Swing and I wanted to do a little silly application to spice up my interest and to learn something along the way.

The initial idea behind my little Java application was loading the image onto my jLabel with the click of the button, and I made it work with this piece of code.

try {

        Image img = ImageIO.read(getClass().getResource("/folder/14643106_10210357322307073_1328313792_n.jpg"));
        jLabel8.setIcon(new ImageIcon(img));
    }
    catch(IOException e)
            {
                e.printStackTrace();
            }

Even though it works perfectly in my NetBeans surroundings, the image does not appear if I run my .jar file. If I open my .jar file with .rar tools I can see the folder within which I've saved my image in my NetBeans project, therefore I assume it's included in the .jar file yet somehow it's not being accessed when the application is run.

I've tried saving my image as a set of HEX values and then passing it onto the jLabel as such, but I was presented with the same problem even though that solution works within my NetBeans projects properly as well.

My questions are : should I load the image differently? Is my path possible wrong?

I've see this question in multiple different versions, but unfortunately none of them helped me in any way. I apologise if this was asked too frequently.

  • Are you saying your .jar file contains a `folder/14643106_10210357322307073_1328313792_n.jpg` entry? – VGR Oct 19 '16 at 20:43
  • Possible duplicate of [Add images to jar](http://stackoverflow.com/questions/5485034/add-images-to-jar) – nhouser9 Oct 19 '16 at 20:50
  • @VGR Yes, I've opened it with winrar and there's a folder "folder", with my image inside along with the rest. Which was kinda confusing to me, because wouldn't it be able to reach for that image in this case? – Cats_Coffee _Guitars Oct 20 '16 at 10:09
  • I would first move the `getClass().getResource` call to a separate line, and print the returned value, just to verify that it’s not null. And of course, make sure you aren’t getting an exception. (In general, it’s good practice to propagate the exception, with something like `throw new RuntimeException(e);`, instead of just printing the stack trace. You don’t want your application to continue running if it can’t build the window properly, right?) – VGR Oct 20 '16 at 13:24

1 Answers1

0

First of all you have to put your image folder in to src folder. If you did it right then please check spellings of your folder and image file because java is case sensitive.