1

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!

Li Siyao
  • 83
  • 9
  • Try to extract files from `JAR` which cause `Location is not set` error (this is just `ZIP` archive) and check that it really has `*.fxml` files inside of `/foo/bar/fxui` directory. – Maxim Mar 09 '15 at 15:14
  • Both "/foo/bar/fxui/file.fxml" and "fxui/file.fxml" should work. You can check the path, opening the jar as zip file. `getClass()` could theoretically be a derived class in another jar, then it would break. Does the `getResource` really return null? Probably the error lies in FXML XML. – Joop Eggen Mar 09 '15 at 15:16
  • Hi maxd, yes I have checked that they are in the JAR file. In fact the JAR file runs well when I double click it. It just does not run in browser. – Li Siyao Mar 09 '15 at 15:17
  • Hi Joop, yes both work when I double click it. The problem arises only ini browser, and getResource really returns null. I checked by throwing an exception when getResource returns null. That's the only way I can debug in browser although it sounds stupid. – Li Siyao Mar 09 '15 at 15:19
  • Could you please provide output of `unzip -l ` command? – Maxim Mar 09 '15 at 15:38
  • Hi maxd, there are hundred of files. Among the outputs I do see this line: "1958 2015/03/09 23:44 foo/bar/fxui/Login.fxml" which is the file I am trying to access. – Li Siyao Mar 09 '15 at 15:52

1 Answers1

3

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.

Li Siyao
  • 83
  • 9