Note that there are some explanatory texts on larger screens.

plurals
  1. POimageio.IIOException: Can't read input file
    text
    copied!<p>I've started Java a week ago, and now I would like to insert an image into my window. Whatever I try I keep having this in Eclipse: <strong>javax.imageio.IIOException: Can't read input file!</strong></p> <pre><code>package graphics; import java.awt.Color; import java.awt.Graphics; import java.awt.Image; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; import javax.swing.JFrame; import javax.swing.JPanel; import src.Common; public class Window extends JFrame { public class Panel extends JPanel { public void paintComponent(Graphics g) { Image img; try { img = ImageIO.read(new File("/logo.jpg")); g.drawImage(img, 0, 0, this); } catch (IOException e) { e.printStackTrace(); } } } public Window(String title, int width, int height) { this.setTitle(title); this.setSize(width, height); this.setLocationRelativeTo(null); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setContentPane(new Panel()); this.setVisible(true); } </code></pre> <p>}</p> <p>I think the code is pretty self-explaining. I tried to solve the problem with <a href="https://stackoverflow.com/questions/12841741/java-unknown-source-with-imageio?rq=1">this</a>, <a href="https://stackoverflow.com/questions/13130982/imageio-cant-read-input-file?rq=1">this</a>, and <a href="https://stackoverflow.com/questions/5713096/javax-imageio-iioexception-for-apparently-no-reason">that</a> .</p> <p>What I'm trying to do is a desktop program, and my sources are stored like that : training/src/graphics/Window training/src/src/main</p> <p>I did put the image I want to read in every folder, and still getting the issue :/</p> <p>What did I do wrong?</p> <p><strong>EDIT Finally solved, here the answer</strong></p> <p>nIcE cOw gave me the <a href="https://stackoverflow.com/a/9866659/1057230">link</a> that helped. So I did put my images into a folder, and change the way to access to them, as described in the link.</p> <pre><code>getClass().getResource("/images/yourImageName.extension"); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload