0

enter image description here
This is my project structure I want to load image Text.gif in code

Image firstimage=new Image("resources/Text.gif");

I don't understand whats wrong here As per Oracle documentation

// The image is located in my.res package of the classpath<br>
Image image2 = new Image("my/res/flower.png", 100, 150, false, false);

I am doing correct as per oracle documentation as in general classPath is up to src

As experimenting I even tried

Image firstimage=new Image("resources/Text.gif");
Image firstimage=new Image("@resources/Text.gif");
Image firstimage=new Image("@/resources/Text.gif");
Image firstimage=new Image("@/Text.gif");
Image firstimage=new Image("@Text.gif");

What should I do to get this image working and where @ is used?

Bashir
  • 2,057
  • 5
  • 19
  • 44
vikalp rusia
  • 95
  • 1
  • 11
  • 1
    `@` is used only in FXML, as far as I know. – James_D Jun 04 '20 at 14:20
  • `Image firstimage = new Image(main.class.getResource("resources/Text.gif").toString());` – Doros Barmajia Jun 04 '20 at 14:31
  • @Tajrib That won't work: it will look for a `resources` subpackage of `sample`, which doesn't appear to exist. The correct path depends on how the build is configured. – James_D Jun 04 '20 at 14:35
  • Thanks for help i understood that solution but what is the problem here?@Tajrib @James_D – vikalp rusia Jun 04 '20 at 15:05
  • You have 2 possibilities. The `1st` is to put your `resources` package inside `sample`, then use what I said in my previous reply. The `2nd`, is to down 2 levels like this: `String parentDir = new File(Main.class.getResource("../").toString()).getParent(); String sourceImage = parentDir.concat("/resources/Text.gif");` – Doros Barmajia Jun 04 '20 at 15:28
  • @Tajrib i am newbie and trying to learn javafx so can you elaborate problem here instead of solution first i want to understand problem here as per oracle i am quite correct but still it donot work – vikalp rusia Jun 04 '20 at 15:29
  • @vikalprusia You can see that `resources` and `sample` packages are in the same level in the directories hierarchy. So when you look for `resources` you do that in `sample`, and this is wrong. For that, you have to down one level to get out from `sample` by using `../`, then get in `resources` to find your image file. – Doros Barmajia Jun 04 '20 at 15:46
  • I think you are wrong when i move text.gif to src folder or sample level it works by using Image image=new Image("Text.gif"); so in my views it seraches in src folder – vikalp rusia Jun 04 '20 at 15:57
  • @Tajrib no, dots as part of the lookup param are __not__ supported - please read the api doc (and also the duplicate) – kleopatra Jun 04 '20 at 16:03
  • @kleopatra I totaly agree with you. This is why i said that I prefer to move `resources` to be subpackage of `sample`. – Doros Barmajia Jun 04 '20 at 16:11
  • @vikalprusia I tried my previous code and it works fine in Eclipse IDE. I see that you use intellij IDE that I don't know if it handles things differently. Try to move `resources` to be subpackage of `sample`. – Doros Barmajia Jun 04 '20 at 16:12
  • All of the comments saying we can see which packages are at which levels and how they relate are wrong: we can't see that at all, because we don't know how the build is configured. E.g. is `resources` really a package, or is it (as would be more usual) configured as a source folder for the build (putting `Text.gif` at the root of the classpath)? As explained in the duplicate question, to troubleshoot this you need to look at the content of the `out` folder, which is not shown here. – James_D Jun 04 '20 at 16:19
  • *Probably* what is happening is that `resources` is a source folder for the build, so the content of `resources` (including `Text.gif`) gets placed at the root of the classpath in the build folder (`out`). Then the correct path would be `getClass().getResource("/Text.gif").toExternalForm()` (or just use `new Image("Text.gif")`). Again, you can check this by examining the build folder. And again, all of this is explained in the duplicate question. – James_D Jun 04 '20 at 16:24
  • I checked it build folder is similar to this one resource folder is created at sample level in , i have one more doubt why it works in fxml in this format @../resource/Text.gif. @James_D – vikalp rusia Jun 04 '20 at 20:55
  • `@` is a special syntax in FXML that instructs the `FXMLLoader` to convert a relative file-path-like string to a resource name. But this question is now answered, correct? – James_D Jun 04 '20 at 21:01

0 Answers0