Search code examples
javajarlibrariesabsolutelayout

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


None of my Jar files are running. I have created a project which runs fine in Netbeans but the Jar isn't showing anything. Running it through cmd shows noclassdeffounderror etc. I created a simple project with one class that sets the JFrame visible which simply displays "MY FIRST APP" the JFrame has Absolute layout which adds library in Netbeans project but jar does not have that library so it throws error

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);
    }
}

and JFrame file code

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;

Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: org/netbeans/lib/awtextra/AbsoluteLayout at app.Look.initComponents(Look.java:15) ....


Solution

  • I made a Netbeans project out of your files and everything works ok. Here are the steps.

    First, create a new Java project, add the two files and import the proper library according to these instructions (maybe you already did this):

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

    Then build the project via "Clean and Build". On project level you will get a "dist" folder with the following contents:

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

    I named my project "Stackoverflow1". So to run it from the command line you can do:

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

    And if you want to move the .jar around, for example move it one dir above, you can run it in the following way:

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

    You cannot do it in the following way, this will give you the reported error

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

    The trick is that if you use the -jar option, the classpath will be defined only via the entry "Class-Path:" inside the manifest file of StackOverflow1.jar. So in order to use AbsoluteLayout.jar from another location, you have to put both AbsoluteLayout.jar and StackOverflow1.jar in the classpath and then invoke your application by calling the main class. See more here

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