Doug
Doug

Reputation: 387

Java ImageIO.read() fails in browser

I've made a game in Java, which naturally uses multiple images in it. I developed the game using Eclipse, and running it in Eclipse has no problems. At this point, the game is pretty much done, so I'm trying to put it on the internet, but I cannot get this game to run in a browser.

Loading it in a browser just shows a white screen, no error, just white. I know Java is up to date, I have run other java apps to test it and none of them have any problems. For a while I couldn't view the console because clicking on the blank app did nothing, but eventually I noticed that going to the notification in the taskbar allowed me to see the console. In there it lists the error as

javax.imageio.IIOException: Can't read input file!

and points me to the first image I load in my code. I have about 2 dozen images but it goes to the first one, upon commenting it out, the error redirects to the second, so I presume this is the same problem with every image. These images load fine in Eclipse, so I know the file paths are correct, somehow it doesn't show up in the browser.

The Java code which loads the first image:

image = ImageIO.read(new File("images/debug/player.jpg"));
Constants.imgPlayer = new Picture(getImage(getDocumentBase(), "images/debug/player.jpg"), image.getWidth(), image.getHeight());

This is being run when the program loads and is saving the image into this Picture class I created. All the Picture class does is hold the Image object as well as the width and height for later when I need to actually use the image. The only reason I have to use the ImageIO.read() is so I can get the width and height of each image.

For debugging I changed some of the code to this:

File testFile = new File("images/debug/player.jpg");
System.out.println("exists() = " + testFile.exists());
System.out.println("canRead() = " + testFile.canRead());

testFile.setReadable(true);
System.out.println("setReadable(true)");

System.out.println("canRead() = " + testFile.canRead());
image = ImageIO.read(testFile);
Constants.imgPlayer = new Picture(getImage(getDocumentBase(), "images/debug/player.jpg"), image.getWidth(), image.getHeight());

In Eclipse, everything comes out as true, it exists, can be read, and then is read and works. In the browser (Chrome by the way), everything is false, it doesn't exist, cannot be read, and then throws an error when it tries to be read.

The HTML file is at [project]/bin/FinalProject.htm, though the main class is at [project]/bin/main/FinalProject.class. I'm wondering if because they are in separate folders, the file paths need to be different. I tried placing the HTML file in the same directory as the class, but then it would say it couldn't find the class though I'm certain I changed the file path to match.

I'm not really sure what to do at this point, Google searching hasn't really helped. Any help is appreciated.

Upvotes: 0

Views: 1089

Answers (1)

Eric J.
Eric J.

Reputation: 150158

You are trying to read a file from the local file system. Java, running in a browser sandbox, does not allow that. After all, you would not want someone else's game to be able to read your home banking records or similar.

Use image resources instead.

image = getImage(getDocumentBase(),"player.jpg");

See getImage in the Java docs.

Upvotes: 1

Related Questions