线程“main"中的异常spring boot java中的java.awt.HeadlessException [英] Exception in thread "main" java.awt.HeadlessException in spring boot java

查看:21
本文介绍了线程“main"中的异常spring boot java中的java.awt.HeadlessException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用intellij运行由netbeans IDE创建的JFrameForm,我从netbeans复制源代码并将其粘贴到新的java类并在main函数中创建对象并设置jframe包含的类的可见性并运行它给我以下错误

I tried to run JFrameForm created by netbeans IDE using intellij, I copied source code from netbeans and paste it to new java class and create object in main function and set visible of jframe included class and run it it gives me below error

2019-09-21 17:36:31.363Exception in thread "main" java.awt.HeadlessException
at java.desktop/java.awt.GraphicsEnvironment.checkHeadless(GraphicsEnvironment.java:208)
INFO    at java.desktop/java.awt.Window.<init>(Window.java:548)
4744    at java.desktop/java.awt.Frame.<init>(Frame.java:423)
---     at java.desktop/java.awt.Frame.<init>(Frame.java:388)
[       Thread-1]   at java.desktop/javax.swing.JFrame.<init>(JFrame.java:180)
j.LocalContainerEntityManagerFactoryBean    at com.sunTravel.frontend.uiComponents.MainFrame.<init>    
(MainFrame.java:19)
:   at com.sunTravel.frontend.FrontendApplication.main(FrontendApplication.java:12)
Closing JPA EntityManagerFactory for persistence unit 'default'
2019-09-21 17:36:31.364  INFO 4744 --- [       Thread-1] .SchemaDropperImpl$DelayedDropActionImpl :         
HHH000477: Starting delayed evictData of schema as part of SessionFactory shut-down'
2019-09-21 17:36:31.367  INFO 4744 --- [       Thread-1] com.zaxxer.hikari.HikariDataSource       :     
HikariPool-1 - Shutdown initiated...
2019-09-21 17:36:31.370  INFO 4744 --- [       Thread-1] com.zaxxer.hikari.HikariDataSource       : 
HikariPool-1 - Shutdown completed.

Process finished with exit code 1

我的源代码如下,如何解决?

My source code available below and how to resolve this?

@SpringBootApplication
public class FrontendApplication {

    public static void main(String[] args) {
        SpringApplication.run(FrontendApplication.class, args);
        MainFrame mf = new MainFrame();
        mf.setVisible(true);
    }

}

MainFrame 类

MainFrame class

public class MainFrame extends javax.swing.JFrame {


public MainFrame() {
    initComponents();
}

@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGap(0, 400, Short.MAX_VALUE)
    );
    layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGap(0, 300, Short.MAX_VALUE)
    );

    pack();
}// </editor-fold>

/**
 * @param args the command line arguments
 */
public static void main(String args[]) {
    /* Set the Nimbus look and feel */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
     */
    try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {

 java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, 
 null, ex);
    } catch (InstantiationException ex) {

java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, 
 null, ex);
    } catch (IllegalAccessException ex) {

java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, 
null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {

java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, 
null,ex);
        }

        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new MainFrame().setVisible(true);
            }
        });
    }
}

推荐答案

如图所示尝试禁用 headless 属性 此处.您必须在创建/显示 JFrame 之前立即执行此操作.

Try to disable headless property as shown here. You will have to do it right before you create/show the JFrame.

System.setProperty("java.awt.headless", "false"); //Disables headless

示例:

SpringApplication.run(MyClass.class, args);
System.setProperty("java.awt.headless", "false");
SwingUtilities.invokeLater(() -> {
    JFrame f = new JFrame("myframe");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);
});

这篇关于线程“main"中的异常spring boot java中的java.awt.HeadlessException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆