1

When I try to run a Spring Boot JavaFX project with

mvn spring-boot:run

I get an error "JavaFX runtime components are missing". The JDK version is 17.

I did an online search on the error message. Two solutions I find. One is to make the application modular. I don't see that is a valid solution for the Spring Boot application. And the other one is to add command line arguments for module path and modules names. The Maven command doesn't take arguments "--module-path" nor "--add-modules".

I also try to run the project with JDK only

 java -jar app.jar

Although there aren't any errors, the GUI doesn't show up.

How to resolve this issue? A good solution would be that it is easy to run without many downloading, local setup etc.

Rainbow
  • 61
  • 1
  • 7
  • Spring Boot 2.x.x is not quite friendly with modular applications. I have a working **non-modular** example [here](https://github.com/ehayik/jfx-playground) Spring Boot + JFX + JDK 17 + Maven/Gradle – eHayik Jan 02 '23 at 21:31
  • @eHayik Thanks for sharing your work. Your Spring + JavaFX project setup is better than mine https://github.com/vw98075/javafx-spring-boot-sample. – vic Jan 04 '23 at 03:29
  • @eHayik I upgrade JDK to 19 of your project and I need to remove Lombok for the change. I notice a few things in your project. (1) I am unable to retrieve those properties in the application.properties with @Value{...}. – Rainbow Jan 07 '23 at 06:15
  • I don't know whether that is the reason you have the AppProperties class to retrieve the property data or not. Some log messages are interesting. > Task :bootRun Jan 06, 2023 9:56:42 PM com.sun.javafx.application.PlatformImpl startup – Rainbow Jan 07 '23 at 06:17
  • WARNING: Unsupported JavaFX configuration: classes were loaded from 'unnamed module @4a499116' (java:29938): dbind-WARNING **: 21:56:43.189: Error retrieving accessibility bus address: org.freedesktop.DBus.Error.ServiceUnknown: – Rainbow Jan 07 '23 at 06:18
  • The name org.a11y.Bus was not provided by any .service files -- it says about unsupported JavaFX configuration, dbind warning, and Dbus error. I have no ideal what they are about. – Rainbow Jan 07 '23 at 06:20
  • Hey, tnks for the feedback. [Lombok doesn't work with JDK 19 yet](https://github.com/projectlombok/lombok/issues/3264). Regarding the warning it was introduced by OpenJDK team, [It can be ignored](https://stackoverflow.com/questions/67854139/javafx-warning-unsupported-javafx-configuration-classes-were-loaded-from-unna) – eHayik Jan 07 '23 at 20:34
  • Regarding your issue injecting properties via @Value. It should work, if the configuration is ok, [see this commit](https://github.com/ehayik/jfx-playground/commit/c5de2fbb16d81fc0de32359f775ba1f428dd76ad). – eHayik Jan 07 '23 at 21:07
  • @eHayik Thanks for your clarification. The StageRouter design is interesting. I also notice the application property file in YAML format doesn't work in all Spring Boot + JavaFX projects I have run into. Let me know if you know the reason(s) – Rainbow Jan 11 '23 at 19:07
  • @Rainbow you may create an [issue](https://github.com/vw98075/javafx-spring-boot-sample/issues) on my repo, adding more details in order to reproduce the issue you are facing with yml files – eHayik Jan 26 '23 at 07:30

2 Answers2

1

Since Java 11, JavaFX was removed from the SDK. It is now in its own separate module, and if you want to use it in your application, you will need to specifically include it.

Regarding, "to make the application modular":

Spring 5 and Spring Boot are not friendly with Java Platform Module System (JPMS). It won't really be built for modules until Spring 6/Springboot 3 is released.

I do try the modular approach with Spring Boot 2. However, compilation failed due to a known Lombok error.

Regarding, "to add command line arguments for module path and modules names. The Maven command doesn't take arguments "--module-path" nor "--add-modules":

You can use non-modular approach without --module-path and --add-modules.

Maven configuration is pretty straightforward.

Firstly, Add the JavaFX modules you need as maven dependencies. For instance:

<properties>  
    <java.version>17</java.version>  
    <javafx.version>17.0.2</javafx.version>  
</properties>

<dependency>  
    <groupId>org.openjfx</groupId>  
    <artifactId>javafx-controls</artifactId>  
    <version>${javafx.version}</version>  
</dependency>  
  
<dependency>  
    <groupId>org.openjfx</groupId>  
    <artifactId>javafx-fxml</artifactId>  
    <version>${javafx.version}</version>  
</dependency>

The project can be built and run using Spring Boot Maven Plugin

NOTE: There is a maven plugin available for running JavaFX 11+ applications. So far, it is not required since the application will be packed and run using Spring Boot.

You may find a working example here, showcasing how to bootstrap JavaFX applications within Spring Boot application.

How to bootstrap JavaFX application within Spring Boot application?

The bootstrap process is heavily inspired by Mr. Awesome Josh Long’s Spring Tips: JavaFX installment.

Instead of calling SpringBootApplication.run() use a custom bootstrap class inheriting from JavaFX Application. This is needed to initialize JavaFX correctly.

JavaFxApplication class does the heavy lifting for creating a proper JavaFX application with initialized Spring Context. It's responsible for:

  • Set Spring Boot web server type to NONE.
  • Programmatically create a Spring Boot context in the Application#init() method.
  • Kick off application logic by sending a StageReadyEvent containing the primary Stage as payload.
  • Support graceful shutdown for both Spring context and JavaFX platform.
eHayik
  • 2,981
  • 1
  • 21
  • 33
  • Thanks for the info. I had tried to get Josh's sample code to work before. I tried again and I got it to work this time. Your project has some differences from his project. – vic Jan 28 '23 at 03:26
-1

Im literally searching for the same... But I found there are some mods to do in your run-config to get this problem solved

Craftoncu
  • 11
  • 2
  • Thanks for the info. I found that such an app needs to run as a JavaFX app, but not a Spring Boot app. In terms of the two solutions are not user-friendly. – Rainbow Aug 29 '22 at 15:58
  • Please post commenst for suggestions and write self contained answer. – Vencat Sep 01 '22 at 16:01