My code is very simple, really there is no problem in writing my code. I'm getting the file not found error as an error, but I'm 100% sure the file path is correct, I've checked hundreds of times. There are no other errors. Some different image upload methods give no error but as a result, the image never appears.
ChatGPT: can't fix.
Image: 16x16, .png, 1.72KB
Compiler IDE: Eclipse
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
public class main {
public static void main(String[] args) {
JFrame window = new JFrame("JavaTests");
window.setVisible(true);
window.setResizable(true);
window.setSize(800,600);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.getContentPane().setBackground(Color.PINK);
try {
File imageFile = new File("icon.jpg");
BufferedImage image = ImageIO.read(imageFile);
JLabel label = new JLabel(new ImageIcon(image));
label.setSize(image.getWidth(), image.getHeight());
label.setBackground(Color.BLACK);
label.setOpaque(false);
window.add(label);
} catch (Exception e) {
System.out.println("LOG: " + e.getMessage());
e.printStackTrace();
}
}
}


First mistake:
Your screenshot shows the file name "icon.png", but in your Java code you try to load "icon.jpg".
Second mistake:
When you specify
then the file is assumed to be in the current directory, which in your case is your project directory (i.e. the directory also containing your
srcandbinfolder). But that is not where your image file actually is.A safe way to load the image file from the same directory where your
.classfile is (i.e. thebinfolder), is like this:And be aware calling
getResource(...)will givenullif the file is not found.