'Java: Image is not loading from resources folder

I am currently working on a school project. However, I am working with JFrame and JPanel. I need to add an image as a logo from my resources folder inside my project, I am not allowed to move it somewhere else.

The tree of three project looks like this: tree

Eclipse shows me this path: eclipse path

file:///Users/myname/git/M0/myname/src/resources/siegel.jpg

When I try to load it with siegel.jpg it fails. with resources/siegel.jpg and src/resources/siegel.jpg too.

What did I miss? Down below is my code.

package gui;

import java.awt.BorderLayout;
import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class ImageFrame extends JPanel {

    public ImageFrame() {
        this.setLayout(new BorderLayout());

        try {
            BufferedImage logo = ImageIO.read(this.getClass().getResource("src/resources/siegel.jpg"));
            JLabel image = new JLabel(new ImageIcon(logo));
            this.add(image);

        } catch (IOException e) {
            // TODO Auto-generated catch block
            System.out.println("Fuck you");
//          e.printStackTrace();
        }

    }
}

The exact error msg:

    at java.desktop/javax.imageio.ImageIO.read(ImageIO.java:1402)
    at gui.ImageFrame.<init>(ImageFrame.java:20)
    at gui.LeftPanel.<init>(LeftPanel.java:28)
    at gui.MyFrame.<init>(MyFrame.java:31)
    at gui.GUI_main.main(GUI_main.java:9)


Solution 1:[1]

When I try to load it with siegel.jpg it fails. with resources/siegel.jpg and src/resources/siegel.jpg too

This should be the exact error,

 javax.imageio.IIOException: Can't read input file!
          at java.desktop/javax.imageio.ImageIO.read(ImageIO.java:1402)
          at gui.ImageFrame.<init>(ImageFrame.java:20)
          at gui.LeftPanel.<init>(LeftPanel.java:28)
          at gui.MyFrame.<init>(MyFrame.java:31)
          at gui.GUI_main.main(GUI_main.java:9)

Try with "/siegel.jpg"

BufferedImage logo = ImageIO.read(this.getClass().getResource("/siegel.jpg"));
     

If you want to give a complete path,

 BufferedImage logo = ImageIO.read(
                        new File("/home/user/Downloads/myapp/src/main/resources/siegel.jpg"));

The below code is working at least for me,

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class ImageFrame extends JPanel {

    public ImageFrame() {
        this.setLayout(new BorderLayout());
        try {

            BufferedImage logo = ImageIO.read(this.getClass().getResource("/siegel.jpg"));
            JLabel image = new JLabel(new ImageIcon(logo));
            this.add(image);

        } catch (Exception e) {
            // handle
            e.printStackTrace();
        }

    }

    // My Test Driver 
    public static void main(String args[]) {

        JFrame frame = new JFrame();
        frame.add(new ImageFrame());
        frame.setSize(400, 400);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new GridLayout(2, 2));
        frame.setVisible(true);
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1