0

The code below works fine in Eclipse (both image-handling possibilities). But when exporting as a Runnable JAR File, and double-clicking the .JAR, nothing happens. If I comment out the image parts of the code, the .JAR runs fine as an export and the frame builds. So it seems the getting of the image is causing the .JAR to fail.

I've got the strawberry.jpg file sitting in 'C:\Users\sean\workspace\myApps\bin\testing' Could you advise if the issue is with my code?

(Code first modified here: Java Swing: unable to load image using getResource)

package testing;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.io.IOException;
import java.io.InputStream;



public class IconTest {


public static void main(String[] arguments) throws IOException {

    JFrame frame1 = new JFrame();
    frame1.setTitle("Frame1");
    frame1.setSize(500, 500);
    frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    FlowLayout flo = new FlowLayout();
    frame1.setLayout(flo);

    //POSSIBILITY 1 TO HANDLE IMAGE
      InputStream resourceAsStream = IconTest.class.getResourceAsStream("strawberry.jpg");
    Image image = ImageIO.read(resourceAsStream);
    JLabel label1 = new JLabel(new ImageIcon(image));

    //POSSIBILITY 2 TO HANDLE IMAGE
/*    java.net.URL url= IconTest.class.getResource("strawberry.jpg");
    BufferedImage watermarkImage = ImageIO.read(url);
    JLabel label1 = new JLabel(new ImageIcon(watermarkImage));*/


    frame1.add(label1);
    frame1.setVisible(true);
}

}

4
  • if you are executing in different system extract the jar and check whether the images are included.
    – Shriram
    Apr 2, 2014 at 5:08
  • One possibility is you can check if ( image != null ) before including it in JLabel as ImageIcon, and if it is null, you can create JLabel with some alternative text...
    – Not a bug
    Apr 2, 2014 at 5:11
  • 1
    Aren't resources needed by the application supposed to stored in the resources directory? This might need to be configured in Eclipse, not a user of it, but this happens all the time Apr 2, 2014 at 5:14
  • Yes - I managed to get it to work by creating two packages - one for resources and one for my code classes. Then I right-click > import on the resources package in Eclipse to get the image. The only thing to change in my code is ....getResourceAsStream("/resources/strawberry.jpg");
    – Synapse
    Apr 4, 2014 at 16:54

5 Answers 5

0

Put the image where you have the source file because it points to the source directory

InputStream resourceAsStream = IconTest.class.getResourceAsStream("strawberry.jpg");

and then generate runnable jar file and then right click the jar and give run as java platform binary

1
  • I could be wrong, but I don't thing eclipse likes resources in the arc directory and doesn't include the files in the src directory, I think they need to be in resources directory Apr 2, 2014 at 5:22
0

POSSIBILITY 3 TO HANDLE IMAGE :

You can check that resource that you are fetching is exist or not. It resource not found then you can create JLabel with alternative text also...

JLabel label1 ;
URL imageUrl = IconTest.class.getClassLoader().getResource("strawberry.jpg");
if ( imageUrl != null ) {
    label1 = new JLabel(new ImageIcon(imageUrl));
} else {
    label1 = new JLabel("Alternative text");
}
2
  • ImageIO can to a URL directly, not need to load the stream, which you're not closing... Apr 2, 2014 at 5:20
  • @Krishan - this was very helpful - thanks. I managed to get it to work by creating two packages in a new Project - one for resources and one for my code classes. Then I right-click > import on the resources package in Eclipse to get the image. The only thing to change in my code is ....getResourceAsStream("/resources/strawberry.jpg");
    – Synapse
    Apr 4, 2014 at 16:58
0

Following Maven standard directory layout might be helpful:

layout

0
0
InputStream resourceAsStream = IconTest.class.getResourceAsStream("/strawberry.jpg")

It should start from "/". Also check whether the jar actually have the image. Just unpack it and check whether the image is actually added.

0

The guidance offered here in troubleshooting the problem helped greatly - thank you to everyone. I tested contents of an exported JAR; I used diagnostic code suggested by Kishan to determine if my code could "see" the image. I believe it might have something to do with the way Eclipse works/refreshes if I move images around in the file system instead of the Eclipse import function. Finally, in order to get it to work, I made the following changes: I created a new project and two sub-packages - one for 'resources' and one for my code class. Then I right-clicked > import on the resources package in Eclipse to get the image.

The only thing to change in my code is ....getResourceAsStream("/resources/strawberry.jpg");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.