Eclipse error: javax.imageio.IIOException: cannot read the input file

The following code works successfully in the 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 the same as the IDE. In addition, I noticed that the directory structure is the same for * .class files and image files.

Why does this only happen in eclipse?

+6
source share
7 answers

You have to use

 System.getProperty("user_dir")+File.separator+"image"+File.separator+"im0001.jpg"; 
+3
source

Please make sure that your image folder is a resource folder (which means that it is on 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 an alternative.

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

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

+1
source

Try it.

 String path="d:\\images\\pic1.jpg"; BufferedImage myPicture = null; try { myPicture = ImageIO.read(new File(path)); } catch (IOException e) { e.printStackTrace(); } 
0
source

just check the image path with System.out.println(System.getProperty("user.dir"));

0
source

This is not an Eclipse error. You need to copy the image files to the main folder of the Eclipse Project (not to the src subfolder).

0
source

The default libraries in eclipse do not support "ImageIO".

0
source

Source: https://habr.com/ru/post/923343/


All Articles