当我们尝试获取 Clipboard 实例时。
Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard();
Run Code Online (Sandbox Code Playgroud)
我还尝试通过设置头部来运行 Spring Boot 应用程序。
SpringApplicationBuilder builder = new SpringApplicationBuilder(SpringBootApplication.class,args);
builder.headless(false).run(args);
Run Code Online (Sandbox Code Playgroud)
我们正在低于异常。
java.awt.HeadlessException
at sun.awt.HeadlessToolkit.getSystemClipboard(HeadlessToolkit.java:309)
at com.kpit.ecueditor.core.utils.ClipboardUtility.copyToClipboard(ClipboardUtility.java:57)
Run Code Online (Sandbox Code Playgroud)
有人可以建议我在这里缺少什么。
如果我在简单的 java 应用程序中运行相同的剪贴板代码,它可以正常工作,但不能在 Spring Boot 应用程序中运行。
以下代码有效:
import javax.swing.*;
public class HeadlessExceptionDemo {
public static void main(String[] args) {
JFrame frame = new JFrame("HeadlessExceptionDemo");
frame.setSize(800, 600);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
Run Code Online (Sandbox Code Playgroud)
以下代码也有效:
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.swing.*;
@Configuration
public class HeadlessExceptionDemo {
@Bean
public JFrame frame() {
JFrame frame = new JFrame("HeadlessExceptionDemo");
frame.setSize(800, 600);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
return frame;
}
public static void main(String[] args) {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(HeadlessExceptionDemo.class);
JFrame frame = ctx.getBean(JFrame.class);
frame.setVisible(true);
}
}
Run Code Online (Sandbox Code Playgroud)
而以下代码不:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import …Run Code Online (Sandbox Code Playgroud)