简体   繁体   中英

NetBeans - Java/Swing - No images appear in executable .jar

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM