Issue
So I've built a snake game and it worked.
I wanted to update it with more Java-related technology like Springboot
but when I tried to run the exact same code that worked without Spring,
a Headless Exception has been thrown at me.
I tried to search online but I haven't found any useful information...
Any idea what caused it?
The original main method:
package run;
import game.GameFrame;
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
new GameFrame();
}
}
The original GameFrame:
package game;
import javax.swing.*;
public class GameFrame extends JFrame {
public GameFrame(){
this.add(new GamePanel());
this.setTitle("Kevin The Snake");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
this.pack();
this.setVisible(true);
this.setLocationRelativeTo(null);
}
}
The 'Springboot' main method:
package app.core;
import static app.core.statics.Globals.*;
import app.core.game.GameFrame;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class WiseKevinApplication {
public static void main(String[] args) {
SpringApplication.run(WiseKevinApplication.class, args);
new GameFrame(); // TODO -- Check exceptions (Headless Exception) and maybe try launching GameFrame as an entity
}
}
The 'Springboot' GameFrame:
package app.core.game;
import javax.swing.*;
public class GameFrame extends JFrame {
public GameFrame(){
this.add(new GamePanel());
this.setTitle("Wise Kevin");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
this.pack();
this.setVisible(true);
this.setLocationRelativeTo(null);
}
}
Solution
The moment you call "new" your GameFrame is not under Spring's control. You would need to create that instance using Spring configuration.
I'd recommend that you read this.
Answered By - duffymo
Answer Checked By - Marilyn (JavaFixing Volunteer)