SitaNVGS SitaNVGS - 15 days ago 12
Java Question

JavaFX, can't get application icon to show up after exporting to *.JAR in eclipse

I basically want to have an app icon (the one in top left corner), but every time I export my project into *.jar in eclipse I lose all my images. It is easy to open *.jar and make throw in there background image (definined in CSS), but I absolutely cannot make an app icon working.

Here is the line that creates this icon:

primaryStage.getIcons().add(new Image("file:icon.png"));


Here is what I mean by the app icon

Sorry for poor english :c

Answer

You're loading the image from the working directory, not the jar itself. This is why it's working in development, and not in production. If you move the image to the same folder as the jar is located it should work, but I'm assuming you want the image to be contained inside your jar file.

Move the image to one of your packages (any, as long as it gets bundled with the jar). Then you need to access the image, use the ClassLoader.getResourceAsStream(String) for this. It looks like you're using javafx, so you can use this to get your image: primaryStage.getIcons().add(new Image(ClassLoader.getSystemResourceAsStream("the/path/to/your/image.png"));.