I am attempting to use SpringBoot along with JavaFX. To do this, the main class extends the JavaFX Application. I then start the spring context from the JavaFx thread during the init method.
When attempting to use mvn native:compile -Pnative
via the native maven plugin, the following error comes up:
Exception in thread "main" java.lang.IllegalStateException: No application context available after calling main method of 'com.example.JavaFxNative.problemDemo.ProblemDemoApplication'. Does it run a SpringApplication?
at org.springframework.boot.SpringApplicationAotProcessor$AotProcessorHook.run(SpringApplicationAotProcessor.java:120)
at org.springframework.boot.SpringApplicationAotProcessor.prepareApplicationContext(SpringApplicationAotProcessor.java:60)
at org.springframework.context.aot.ContextAotProcessor.doProcess(ContextAotProcessor.java:83)
at org.springframework.context.aot.ContextAotProcessor.doProcess(ContextAotProcessor.java:49)
at org.springframework.context.aot.AbstractAotProcessor.process(AbstractAotProcessor.java:82)
at org.springframework.boot.SpringApplicationAotProcessor.main(SpringApplicationAotProcessor.java:80)
Is there a good way to use AOT with JavaFx and springboot where the spring context is created by JavaFX?
See the below code and config files to reproduce the behavior.
package com.example.JavaFxNative.problemDemo;
import javafx.application.Application;
import javafx.stage.Stage;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.ConfigurableApplicationContext;
@SpringBootApplication
public class ProblemDemoApplication extends Application {
protected ConfigurableApplicationContext springContext;
public static void main(String[] args) {
Application.launch(args);
}
@Override
public void init() throws Exception {
springContext = configurableApplicationContext();
}
@Override
public void start(Stage stage) throws Exception {
stage.show();
}
@Override
public void stop() throws Exception {
springContext.close();
}
private ConfigurableApplicationContext configurableApplicationContext(){
SpringApplicationBuilder springApplicationBuilder = new SpringApplicationBuilder(ProblemDemoApplication.class);
String[] args = getParameters().getRaw().toArray(String[]::new);
return springApplicationBuilder.run(args);
}
}
The pom.xml can be found below:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example.JavaFxNative</groupId>
<artifactId>problemDemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>problemDemo</name>
<description>Demo for JavaFx and SpringBoot native</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>21.0.3</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.graalvm.buildtools</groupId>
<artifactId>native-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
And the module-info:
open module demo{
requires javafx.base;
requires javafx.controls;
requires spring.boot;
requires spring.boot.autoconfigure;
requires spring.context;
}
Louis Creager is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.