Home » Java » java – Unable to display an image from within Jar file-Exceptionshub

java – Unable to display an image from within Jar file-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment


I know this has been asked before but I can’t get it to work after trying all variations.I have a small program that I’m wanting the launch frame to have an image displayed on it. I can achieve this by having the jar in the same folder as the image and referencing it, however when I try to reference within the jar file itself I keep getting error:

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException

The code I am using is this:

private void imagePanel() {
    setLayout(new BorderLayout());
    BufferedImage image;
    try {
        image = ImageIO.read(this.getClass().getResource("/src/ticketMaster/img/logo.png"));
    } catch (IOException e1) {
        // TODO Auto-generated catch block
    add(new JLabel(new ImageIcon(image)), BorderLayout.CENTER);

I’m not sure if the path is incorrect, I have made a img folder inside the package named img which contains the logo. I have tried all variations of it


I am not able to get the image to load like I would just referencing the image outside of the jar. I’ve used resourceasstream as well and not had any luck with that.

Any ideas here?

How to&Answers:

I dont know your folder or your package structure, but you can try to remove the first ‘/’ for the path you given in the getRessource() method, it may be looking for a absulute path.