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?
You must use
System.getProperty("user_dir")+File.separator+"image"+File.separator+"im0001.jpg";
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();
}
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.
This is not an Eclipse bug. You need to copy the image files into the Eclipse Project Main Folder (not src subfolder).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With