1

I want to set a window icon for a stage in JavaFX (with a local graphic file).

I tried this code:

stage.getIcons().add(new Image("src/main/resources/images/icon.jpg"));

But I alway get the following error:

Exception in Application start method
Caused by: java.lang.IllegalArgumentException: Invalid URL or resource not found at 
javafx.graphics/javafx.scene.image.Image.validateUrl(Image.java:1099)

I already tried a absolute path, a relative path, copying the image into the root directory, etc. but I always get this error message, exept if I try to use a URL to a graphic on a webserver, e.g.

stage.getIcons().add(new Image("http://example.com/icon.jpg"));

This works.

Edit: I changed the code to

stage.getIcons().add(new Image("file:src/main/resources/images/icon.jpg"));

and it works now!

Simon
  • 13
  • 3
  • try adding "file:" before your path, also try to stay away from the absolute path for files in your src folder, that might change if you create a jar file – Ali Jun 21 '19 at 17:50
  • @Ali Thank you very much! I added "file:" and it works now! – Simon Jun 21 '19 at 17:52
  • 1
    Possible duplicate of [Define a relative path of image in Java FX](https://stackoverflow.com/questions/27446360/define-a-relative-path-of-image-in-java-fx) – Ali Jun 21 '19 at 17:53
  • 2
    _Don't add `file:` to your path._ You're trying to access a resource, not an external file. Use `new Image("images/icon.jpg")`. – Slaw Jun 21 '19 at 17:56

0 Answers0