22

When we are trying to get the Clipboard instance.

Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard();

Also i have tried to run the Spring boot application by setting the head.

SpringApplicationBuilder builder = new SpringApplicationBuilder(SpringBootApplication.class,args);
        builder.headless(false).run(args);

we are getting below exception.

java.awt.HeadlessException
    at sun.awt.HeadlessToolkit.getSystemClipboard(HeadlessToolkit.java:309)
    at com.kpit.ecueditor.core.utils.ClipboardUtility.copyToClipboard(ClipboardUtility.java:57)

Can someone suggest me what i am missing here.

If i run the same clipboard code in simple java application , it is working but not in the spring boot application.

mahesh
  • 909
  • 2
  • 18
  • 37
  • 2
    Spring is a web framework, and the context it's going to be operating in is that of a web server (or a console application if you will). It's unlikely to be able to access the clipboard, or `awt` more generally in any way. – Horia Coman Jun 23 '18 at 19:34
  • 2
    Spring Core is a DI framework and in no way tied to web applications. You could use Spring in Swing or JavaFX or other GUI applications as well. – dunni Jun 23 '18 at 20:33
  • If we try to access the jframe and rest of awt then it works but for clipboard i am having issue.. Is it possible to get the system clipboard instance by any other mean – mahesh Jun 25 '18 at 09:24

4 Answers4

30

instead of this line

 SpringApplication.run(Application.class, args);

use

SpringApplicationBuilder builder = new SpringApplicationBuilder(Application.class);

builder.headless(false);

ConfigurableApplicationContext context = builder.run(args);

It will work

ozan
  • 33
  • 1
  • 9
VRadhe
  • 529
  • 5
  • 8
  • 1
    I tried this suggestion but I still get the headless exception. Nothing I've tried works. The graphics environment still says isHeadless is true regardless of System.setPropety("java.awt.headless", "false") or trying to change the Spring config as shown above and the service crashes every time, reliably at the "showOpenDialog". This is strange as the same code works fine in SPARKJAVA REST service (not the framework for this application, however.) – Morkus Oct 23 '19 at 10:23
  • 2
    @Morkus did you find solution for your problem? – milos Jun 08 '20 at 12:11
15

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:

//Main.java
@SpringBootApplication
public class Main implements CommandLineRunner {

    public static void main(String[] args) {
        ApplicationContext contexto = new SpringApplicationBuilder(Main.class)
                .web(WebApplicationType.NONE)
                .headless(false)
                .bannerMode(Banner.Mode.OFF)
                .run(args);
    }

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

In the test class, you'll need to set java.awt.headless propety, so that you won't get a java.awt.HeadlessException when testing the code:

//MainTest.java
@RunWith(SpringRunner.class)
@SpringBootTest
public class MainTest {

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

    @Test
    public void someTest() { }
}

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

Carlos Nantes
  • 1,197
  • 1
  • 12
  • 23
4

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

-Djava.awt.headless=false

Tested on springboot 2.2.5.RELEASE

hesparza
  • 131
  • 5
0

I was facing same issue, all of the solutions shown here didn't work out. Finally noticed DB server user id was disabled hence this was happening no code change was really required. This error was hella misleading.

Suggestion for whoever is facing similar to validate stacktrace and if db connection class is getting this error test out database through simple java class/IDE instead of Spring boot.

priyanka_rao
  • 465
  • 1
  • 4
  • 20
  • Your issue might something else. This is specifically related to copying to clipboard. DB is not needed anywhere – mahesh Mar 30 '23 at 06:28