Search code examples
javajavafxjavafx-2classpathgetresource

JavaFX getResource return null when running in browser


I have a javafx application and I store all fxml files in src/foo/bar/fxui folder. I use the code below to refer to fxml file:

FXMLLoader loader = new FXMLLoader(getClass().getResource("/foo/bar/fxui/file.fxml"));
Parent root = (Parent) loader.load();

The calling class is in src/foo/bar.

This code works well when I run it in Eclipse. To deploy it I export it as JAR using efxclipse. It also works well when I directly run the JAR file. However, the code breaks when I try to launch this application inside a browser (Chrome/IE). It tells me "Location is not set". I have troubleshooted that the following line returns null:

getClass().getResource("/foo/bar/fxui/file.fxml")

Therefore, it seems to me that it is a problem of not able to get the file by the path specified. I have tried putting fxml files into main/resources/foo/bar/fxui folder. It does not work either.

I am sure that required file is in the JAR archive. The issue is, the JAR file runs when I open it by double click, but it does not run in browser.

I have spent one whole day on this problem. Any help is much appreciated!


Solution

  • I resolved this problem myself. The root cause is that I did not sign my jar properly.

    Java getResource() use reflection which will not execute correctly without as valid certificate. A self-signed jar will be fine in this case.

    Thanks for every input above as well.