Spring Boot : java.awt.HeadlessException

Spring Boot : java.awt.HeadlessException

instead of this line

 SpringApplication.run(Application.class, args);


SpringApplicationBuilder builder = new SpringApplicationBuilder(Application.class);


ConfigurableApplicationContext context = builder.run(args);

It will work

I had the same Exception, using Spring Boot 2 in a swing application.

Here is a sample of what worked for me:

In the main class:

public class Main implements CommandLineRunner {

    public static void main(String[] args) {
        ApplicationContext contexto = new SpringApplicationBuilder(Main.class)

    public void run(String... args) throws Exception {
        SwingUtilities.invokeLater(() -> {
            JFrame frame = new JFrame();

In the test class, youll need to set java.awt.headless propety, so that you wont get a java.awt.HeadlessException when testing the code:

public class MainTest {

    public static void setupHeadlessMode() {
        System.setProperty(java.awt.headless, false);

    public void someTest() { }

For those who are having this exception using JavaFX this answer might help.

Spring Boot : java.awt.HeadlessException

You can also just pass the a JVM parameter when running your application, no code change required:


Tested on springboot 2.2.5.RELEASE

Leave a Reply

Your email address will not be published. Required fields are marked *