Eclipse 错误:javax.imageio.IIOException:无法读取输入文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12048848/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 18:50:08  来源:igfitidea点击:

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

javaeclipsejavax.imageio

提问by paraguma

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

以下代码在 BlueJ IDE 中成功运行,但在 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.

我的图像路径在 IDE 中是相同的。此外,我观察到 *.class 文件和图像文件的目录结构相同。

Why this happens in eclipse only?

为什么这只发生在 eclipse 中?

回答by Prasad

You must use

你必须使用

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

回答by Pazhaniapa

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

这不是 Eclipse 错误。您需要将图像文件复制到 Eclipse 项目主文件夹(不是 src 子文件夹)中。

回答by Sai Ye Yan Naing Aye

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

请确保您的图像文件夹是资源文件夹(这意味着它在 CLASSPATH 上)并写入

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

回答by st1ph1n

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.

Eclipse 将默认文件位置设置为根 bin 文件夹,而不是根或包文件夹。确保您的文件位于 bin 文件夹中。

回答by Mossaddeque Mahmood

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

只需检查您的图像路径 System.out.println(System.getProperty("user.dir"));

回答by Kumar Vivek Mitra

Try this..

尝试这个..

String path="d:\images\pic1.jpg";

BufferedImage myPicture = null;
    try {
        myPicture = ImageIO.read(new File(path));
    } catch (IOException e) {
        e.printStackTrace();
    }

回答by Asanga Ranasinghe

Default libraries in eclipse does not support "ImageIO".

eclipse 中的默认库不支持“ImageIO”。