如何创建一个识别 AbsoluteLayout 而不抛出 NoClassDefFoundError 的 jar?

How to create a jar that recognises AbsoluteLayout without throwing NoClassDefFoundError?

None 个 Jar 文件是 运行。我创建了一个在 Netbeans 中运行良好的项目,但 Jar 没有显示任何内容。 运行 它通过 cmd 显示没有 classdefounderror 等。我创建了一个简单的项目,其中一个 class 设置 JFrame 可见,它只显示 "MY FIRST APP" JFrame 具有绝对布局,它添加Netbeans 项目中的库,但 jar 没有该库,因此它抛出错误

package app;
import app.Look;
public class App 
{
    public static void main(String[] args) 
    {
        System.out.println("Run");
        Look n = new Look();
        n.setVisible(true);
    }
}

和JFrame文件代码

package app;
public class Look extends javax.swing.JFrame {
    public Look() {
        initComponents();
    }

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

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jPanel1.setBackground(new java.awt.Color(0, 0, 0));
        jPanel1.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(255, 255, 255));
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("MY FIRST APP");
        jPanel1.add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(240, 140, 230, 70));

        getContentPane().add(jPanel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 10, 730, 340));

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

    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(Look.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Look.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Look.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Look.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(() -> {
            new Look().setVisible(true);
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;

线程中的异常 "AWT-EventQueue-0" java.lang.NoClassDefFoundError: org/netbeans/lib/awtextra/AbsoluteLayout 在 app.Look.initComponents(Look.java:15) ....

我用你的文件制作了一个 Netbeans 项目,一切正常。以下是步骤。

首先,创建一个新的 Java 项目,添加两个文件并根据这些说明导入正确的库(也许您已经这样做了):

https://www.youtube.com/watch?v=B0prYbtdKNI

然后通过 "Clean and Build" 构建项目。在项目级别,您将获得一个包含以下内容的 "dist" 文件夹:

dist/lib/AbsoluteLayout.jar
dist/README.TXT
dist/Whosebug1.jar

我将我的项目命名为 "Whosebug1"。所以要从命令行 运行 它,你可以这样做:

cd <project_dir>/dist
java -jar Whosebug1.jar

如果您想四处移动 .jar,例如将它移动到上面一个目录,您可以 运行 按以下方式进行:

cd <project_dir>
copy dist/Whosebug1.jar ./
java -cp dist/lib/AbsoluteLayout.jar;Whosebug1.jar app.App

你不能用下面的方法,这会报错

java -cp dist/lib/AbsoluteLayout.jar -jar Whosebug1.jar

诀窍是,如果您使用 -jar 选项,class路径将仅通过 Whosebug1.jar 的清单文件中的条目 "Class-Path:" 定义。因此,为了从另一个位置使用 AbsoluteLayout.jar,您必须将 AbsoluteLayout.jar 和 Whosebug1.jar 都放在 class 路径中,然后通过调用主 [=43] 来调用您的应用程序=].在这里查看更多

Differences between "java -cp" and "java -jar"?