-1

I am developing a small Gui Application and want to load my Images from the resource folder. This does work on Windows, but on Linux (Ubuntu 19.04 and Raspbian Buster) it does not.

I've been trying to switch from

getClass().getResource("black.jpg").getFile()

to

getClass().getClassLoader().getResource("black.jpg").getFile()

This is my View Class, extending from JFrame (shorted)

public View() {

        try {
            bI = ImageIO.read(new File(getClass().getClassLoader().getResource("black.jpg").getFile()));
        } catch (IOException e) {
            e.printStackTrace();
        }

[...]

contentPane = new BackgroundPanel(bI);

Error Message in terminal:

javax.imageio.IIOException: Can't read input file!
    at java.desktop/javax.imageio.ImageIO.read(ImageIO.java:1308)
    at de.grapatin.escaperoom.view.View.<init>(View.java:38)
    at de.grapatin.escaperoom.controller.Controller.<init
(Controller.java:11)
    at de.grapatin.escaperoom.App.main(App.java:11)

I expect the image to be shown, but it does not happen. The background is black (like the image black.jpg in my code) but exchanging it does not solve the problem.

Project Structure:

─src
│   ├───main
│   │   ├───java
│   │   │   └───de
│   │   │       └───marvin
│   │   │           └───uitest
│   │   │               ├───controller
│   │   │               ├───model
│   │   │               │   └───enums
│   │   │               ├───util
│   │   │               └───view
│   │   └───resources
│   │       └───images

The output of

System.out.println(getClass().getResource("/black.jpg").getFile());

is

file:/C:/Users/marvin/uitest/target/uitest-0.0.1-SNAPSHOT.jar!/black.jpg

The ! does not come from my side

I just discovered that Windows does also not read from the Image File from the jar. But it is there in the root of the folder.

This question is not a duplicate of the following: imageio.IIOException: Can't read input file because the accepted Answer does not work in my case and the syntax is entirely different and against the accepted answer in my question. Also the edit does not clearly show how to adapt the solution.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Marvin
  • 129
  • 1
  • 13
  • Just a guess it may possible because it is unable to find your image You should use `getClass().getResource("/images/yourImageName.extension");`. – Dushyant Tankariya Jul 15 '19 at 11:01
  • I highly doubt that, because it is able to find the images in Windows 10 Pro x64. Of course Ill try if there's any difference in doing so with a subfolder, but I dont think there will be any difference. – Marvin Jul 15 '19 at 11:06
  • As expected it is the same Error Message with bI = ImageIO.read(new File(getClass().getClassLoader().getResource("images/black.jpg").getFile())); – Marvin Jul 15 '19 at 11:15
  • How it is possible that you got the same error `getClass().getClassLoader().getResource("images/black.jpg").getFile())` for `getClass().getResource("images/black.jpg")`. Can you update your question with your project structure! – Dushyant Tankariya Jul 15 '19 at 11:17
  • 1
    Possible duplicate of [imageio.IIOException: Can't read input file](https://stackoverflow.com/questions/18309868/imageio-iioexception-cant-read-input-file) – Dushyant Tankariya Jul 15 '19 at 11:18

1 Answers1

2

Do not use the getFile() method of URL. It does not return a valid file name. It actually returns the path portion of a URL, with all percent-escapes intact. It is named getFile because the URL class was part of Java 1.0, which was released in the mid 90s, back when nearly all URLs referred to physical files, either on the same computer or on a different computer.

Fortunately, you don’t need it. You can just use the read method of ImageIO which takes a URL:

bI = ImageIO.read(getClass().getResource("/images/black.jpg"));

An important benefit of treating resources as URLs, and never as Files, is that your code will work when you package your program in a .jar file.

VGR
  • 40,506
  • 4
  • 48
  • 63