Jump to content

I cant access files I've put in the build path. (Eclipse Java)

Hi,

I'm building a project in java and I need to import an image file inside the res folder, but I've tried to add it to the build path but i still can't access. Although it looks like it's added as you can see from the pictures.

 

This is where the code throws an exception:

 

class someclass{

private static BufferedImage image = ImageReader.readToBufferedImage("/textures/letters.png");
/*

....

*/

}

 

public class ImageReader{

    public static BufferedImage readToBufferedImage(String path){
        BufferedImage image;
        try{
            image = ImageIO.read(new File(path));
            return image;
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

 

The exception that gets thrown by the image reader is:

 

javax.imageio.IIOException: Can't read input file!
    at javax.imageio.ImageIO.read(Unknown Source)

 

Have I missed something. Thanks in advance.

Capture.PNG

Capture2.PNG

Capture.PNG

Capture2.PNG

!!CATS!!CATS!!CATS!!CATS!!CATS!!CATS!!CATS!!CATS!!CATS!!CATS!!CATS!!CATS!!CATS!!CATS!!CATS!!CATS!!CATS!!CATS!!CATS!!CATS!!

Link to comment
Share on other sites

Link to post
Share on other sites

  • 2 weeks later...

First use file.exists() to check if the file is accessible by your code or not it returns true or false and then do whatever you want to do .

By error mentioned it looks like a image format error.

Link to comment
Share on other sites

Link to post
Share on other sites

You sure the path is correct, it may not be ideal to start from root.

 

Try simply removing the front slash.

 

File f = new File("textures/letters.png");
if(f.exists() && !f.isDirectory()) { 
    BufferedImage image = ImageReader.readToBufferedImage("textures/letters.png");
}

Try that, not tested though.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×