0

I'm trying to setup a Java FX application with Spring Boot and AdoptOpen JDK 11.

Unfortunately I'm not able to read the FXML from the resources folder and I'm very lost because no solution I've found with Google was working in my case.

That's my application setup:

src - main - java - com.sandbox.Application

src - main - resources - primary.fxml

I tried it those way's and nothing worked (got either null or a FileNotFound exception):

Application.class.getResource("primary.fxml");
Application.class.getClassLoader().getResource("primary.fxml");
new ClassPathResource("primary.fxml", Application.class).getFile();

I also tried to make the folder structure in the resources folder the same like in the java folder so that the fxml files are under resources\com\sandbox but that's also not working.

What am I doing wrong?

Why is this so unbelievable complicated with Java?! Very annoying.

thmspl
  • 2,267
  • 2
  • 17
  • 45
  • Since the fxml file is at the root of the classpath (and the `Application` class isn't at the root), you need an absolute resource path. – James_D Jun 28 '20 at 13:42
  • It's also not working if the fxml resource file is in the same folders under resources as the application is. So there must be something I'm missing with Spring Boot. – thmspl Jun 28 '20 at 13:44
  • Then you need to check the actual build folder (not the source folder). See the "Troubleshooting" section in the linked Q/A. – James_D Jun 28 '20 at 13:45
  • How can I access the files if they are under `target\classes\primary.fxml`? – thmspl Jun 28 '20 at 13:49
  • That's the root of the classpath, so as explained in the question I linked you should use an absolute path: `Application.class.getResource("/primary.fxml")` – James_D Jun 28 '20 at 13:50
  • Make sure `Application.class` is referencing your `com.sandbox.Application` class, and not the library class `javafx.application.Application`, btw. – James_D Jun 28 '20 at 14:04

0 Answers0