-5

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();
        }
    }
}

Path File: PATH FILES

Error Code ERROR CODE

  • 3
    The path would be correct if your program ran from the src folder. It does not. – Elliott Frisch Mar 21 '23 at 18:31
  • Images are *resources* : Better to load your files/images as resources: [Netbeans](https://technojeeves.com/index.php/aliasjava1/78-loading-files-as-resources-in-java-with-netbeans) [Eclipse](https://technojeeves.com/index.php/aliasjava1/80-loading-files-as-resources-in-java-with-eclipse) – g00se Mar 21 '23 at 18:32
  • g00se When you mean source, are you talking about the directly decoded image? – Bilal Akol Mar 21 '23 at 18:34
  • 2
    The [current directory](https://technojeeves.com/index.php/aliasjava1/91-find-the-current-directory-in-java) is a concept you need to know – g00se Mar 21 '23 at 18:34
  • I didn't understand your comment question btw – g00se Mar 21 '23 at 18:35
  • The current directory I found is: "eclipse-workspace\JavaLessons" so I edited "File imageFile = new File("src/icon.png");" but still no picture. The error is still the same. – Bilal Akol Mar 21 '23 at 18:37
  • See: [Loading Images Using getResource](https://docs.oracle.com/javase/tutorial/uiswing/components/icon.html#getresource). The file must then be found on your "classpath". – camickr Mar 21 '23 at 18:37
  • camickr I tried this before. "getClass().getResorce("bla bla")" didn't work, still gave the same error. – Bilal Akol Mar 21 '23 at 18:38
  • *so I edited "File imageFile = new File("src/icon.png");* Well I wasn't actually suggesting that as a solution. It should be done with `getResource`. Real programs don't run from IDEs, for one thing – g00se Mar 21 '23 at 18:40
  • `getClass().getResource("/icon.png");` – g00se Mar 21 '23 at 18:41
  • Is your "classpath" set up correctly? You need to have your "bin" directory on the classpath. I don't use an IDE so I don't know how to do it in Eclipse – camickr Mar 21 '23 at 18:42
  • camickr Correct, Thanks for the information, but I'm not doing a serious project, I'm testing. – Bilal Akol Mar 21 '23 at 18:43
  • I've been struggling for 2 days now and I just had a look and this time it happened. I don't know what caused it, but it's probably "The path would be correct if your program ran from the src folder. It does not." i fixed my code based on this comment. So it worked, so solution: " new File("src/icon.png");" I didn't add src/ before. – Bilal Akol Mar 21 '23 at 18:45
  • Actually, just noticed your code is attempting to access "icon.jpg" but you questions shows the file name is "icon.png". – camickr Mar 21 '23 at 18:46
  • Yes, changing "icon.jpg" to "src/icon.png" fixed it. Thank you to everyone who replied. – Bilal Akol Mar 21 '23 at 18:47
  • 3
    (1-) No, you should not be using "src". The image is also located in the "bin" director with all your other class files. Did you not read the tutorial link I provided? It shows common directory structures to use so you don't have a problem. The image belong in a directory structure with your class files. – camickr Mar 21 '23 at 18:51
  • 4
    **There is no guarantee that icon.png exists,** because you don’t know what the [current working directory](https://en.wikipedia.org/wiki/Working_directory) of your Java process will be. Your code is broken because it relies on an unsafe assumption about the current directory. The Class.getResource method is designed to address this safely and reliably. – VGR Mar 21 '23 at 18:58
  • 3
    *"Yes, changing "icon.jpg" to "src/icon.png" fixed it"* - No, it's just masked the bigger problem. Don't use `File`, in your context, it's looking for a file in the CURRENT WORKING DIRECTORY, which IS NOT you "src" directory AND your "src" directory will NOT exist when your export the program. Your image is now an EMBEDDED image, you MUST use `Class#getResource` to load it. – MadProgrammer Mar 21 '23 at 21:30
  • 3
    When you post your next question about why you can't load your images when your program is exported out to a jar, we will point you back to this question – MadProgrammer Mar 21 '23 at 21:31
  • asides: stick to java naming conventions when showing java code publicly, no screenshots of plain text - instead c&p the stacktrace (and format it as code) – kleopatra Mar 22 '23 at 07:46

1 Answers1

0

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

File imageFile = new File("icon.png")

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 src and bin folder). 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. the bin folder), is like this:

URL imageUrl = main.class.getResource("icon.png");
BufferedImage image = ImageIO.read(imageUrl);

And be aware calling getResource(...) will give null if the file is not found.

Thomas Fritsch
  • 9,639
  • 33
  • 37
  • 49