Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eclipse bug : javax.imageio.IIOException: Can't read input file

The following code is running successfully in BlueJ IDE, but not in Eclipse.

String path="images/pic1.jpg";

BufferedImage myPicture = null;
    try {
        myPicture = ImageIO.read(new File(path));
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

My image path is same in both the IDE. Also, I observed directory structure is same for *.class file and image files.

Why this happens in eclipse only?

like image 727
paraguma Avatar asked Aug 21 '12 05:08

paraguma


4 Answers

You must use

System.getProperty("user_dir")+File.separator+"image"+File.separator+"im0001.jpg";
like image 62
Prasad Avatar answered Nov 18 '22 10:11

Prasad


Please make sure your images folder is resource folder (which mean it is on the CLASSPATH) and write

  BufferedImage myPicture = null;
   try {
      myPicture = ImageIO.read("images/pic1.jpg");
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }

or use the alternative.

   BufferedImage myPicture = null;
   try {
      myPicture = ImageIO.read(this.getClass().getResource("/images/pic1.jpg"));
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
like image 1
Sai Ye Yan Naing Aye Avatar answered Nov 18 '22 10:11

Sai Ye Yan Naing Aye


Eclipse sets the default file location to the root bin folder, not to root or to the package folder. Make sure your files are in the bin folder.

like image 1
st1ph1n Avatar answered Nov 18 '22 11:11

st1ph1n


This is not an Eclipse bug. You need to copy the image files into the Eclipse Project Main Folder (not src subfolder).

like image 2
Pazhaniapa Avatar answered Nov 18 '22 12:11

Pazhaniapa