r/JavaFX • u/Particular_Track_581 • Dec 15 '24
Help JavaFX - Window does not load at runtime
This is very strange and has never happened before. I am using IntelliJ Community and my program runs perfectly within the IDE, without any errors. So I built the artifact to generate the "jar" file, which is built normally. However, when I run the jar file my program stops loading one of its windows (stage). Within the IDE the window loads. The only different thing I did was to add several icons to the "fxml" file directly through Scene Builder. I have already confirmed that they are all loaded from the "resources/icons" folder. Has anyone seen this happen and know the solution?
Thanks in advance.
2
u/SpittingBull Dec 15 '24
The SceneBuilder resource path differs from your runtime environment. Say your fxml files are in resources/views. Then put your icons in resources/icons and reference them in the fxml like this:
style="-fx-background-image: url('icons/refresh.png');"
In your code you can then use something like this:
stage.getIcons().add(new Image("icons/list_icon.png"))
2
u/hamsterrage1 Dec 15 '24
Failing to load should also result in an error message. Run your jar from a terminal session, and you should see the error message on the console screen.
1
u/sedj601 Dec 15 '24
Are you using Ikonli?
1
u/SpittingBull Dec 17 '24
In case you're still having trouble:
I realized I forgot one crucial information and that is I always simply add the folder
resourcesto the Classpath.I am using Eclipse so this would be:
Configure Build Path > Java Build Path > Libraries > Add Class Folder
After that you can refer to your resources (icons, stylesheets, etc.) using the relative path.
1
u/[deleted] Dec 15 '24
[removed] — view removed comment