-1

My code is in something like this:

project/src/com/controller/controller.java

And the image is in

 project/resources/user/default_browser.png

Shouldn´t I reach the image with this route

        Image imageOk = new Image(getClass().getResourceAsStream("../../../resources/user/default_browser.png"));
        btn.setGraphic(new ImageView(imageOk));
daniel gon
  • 159
  • 2
  • 14
  • 2
    please read the api doc of getResourceAsStream - it doesn't mention that it supports anything like "../../", does it :) – kleopatra Feb 27 '19 at 13:34

1 Answers1

0

The contents of the resources directory are put at the root of your jar file, so you should access them as follows:

Image imageOk = new Image(getClass().getResourceAsStream("user/default_browser.png"));
btn.setGraphic(new ImageView(imageOk));
Forketyfork
  • 7,416
  • 1
  • 26
  • 33
  • still get "Input stream must not be null" – daniel gon Feb 27 '19 at 13:26
  • Use folder sturcture like `src/main/java/com/controller/controller.java` and `src/main/resources/user/default_browser.png`. Then try as @Sergei mentioned - `getClass().getResourceAsStream("user/default_browser.png")` – Abbin Varghese Feb 27 '19 at 13:32
  • This is only valid if OP uses Maven as their build tool. @danielgon can you clarify what build system do you use (Maven, Gradle, Eclipse, IDEA...?) – Forketyfork Feb 27 '19 at 13:33
  • I´m in Eclipse, not using Maven – daniel gon Feb 27 '19 at 13:43
  • Did you create the project from some kind of a template? Or did you just create the `resources` directory yourself? To add the directory to the classpath of a running application, Eclipse has to "know" about it, so you need to add this directory to the build path. See e.g. here: https://stackoverflow.com/questions/27934796/how-do-i-add-a-resources-folder-to-my-java-project-in-eclipse – Forketyfork Feb 27 '19 at 15:54
  • 1
    `"user/default_browser.png"` is a relative path but from the question it looks the the `Class` is in a different package than the resource. @danielgon What if you try `"/user/default_browser.png"`? – Slaw Feb 27 '19 at 16:19