0

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) ....

theduck
  • 2,589
  • 13
  • 17
  • 23
  • 1
    `AbsoluteLayout` is not a standard library included in the JRE, it's an external library added by netbeans, which "should" be included in the `lib` directory with the jar file when it's built. In order to run the jar, you will need to be in the same directory as the `lib` directory and the jar file (they need to be kept together) – MadProgrammer Aug 15 '19 at 07:30

1 Answers1

0

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"?

  • okay I don't need to add the library in netbeans manually because it is already there but when I clean and build then only jar file appears there. So i manually created a 'lib' folder and pasted the absoluteLayout.jar library there and then ran the project by your given command then again this error occured. –  Aug 16 '19 at 05:46
  • C:\Users\Zayn Jutt\Documents\NetBeansProjects\App\dist>java -cp dist/lib/AbsoluteLayout.jar;App.jar app.App Run Exception in thread "main" java.lang.NoClassDefFoundError: org/netbeans/lib/awtextra/AbsoluteLayout at app.Look.initComponents(Look.java:15) at app.Look.(Look.java:4) at app.App.main(App.java:10) Caused by: java.lang.ClassNotFoundException: org.netbeans.lib.awtextra.AbsoluteLayout at java.base/jdk.i –  Aug 16 '19 at 05:46
  • corrected the path in the command " C:\Users\Zayn Jutt\Documents\NetBeansProjects\App>java -cp dist/lib/AbdoluteLayout.jar; -jar dist/App.jar " and then ran it same error " Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: org/netbeans/lib/awtextra/AbsoluteLayout... " –  Aug 16 '19 at 06:04
  • And Now When i tried to do this " C:\Users\Zayn Jutt\Documents\NetBeansProjects\App>java -cp dist/lib/AbsoluteLayout.jar;App.jar app.App " this happened " Error: Could not find or load main class app.App Caused by: java.lang.ClassNotFoundException: app.App " –  Aug 16 '19 at 06:25
  • Note: I have selected app.App as my main class in project properties and the library is there in the lib folder. now when everything is right why can't it load the main class? –  Aug 16 '19 at 06:27
  • this did the trick for me and it's running. thankyou! " C:\Users\Zayn Jutt\Documents\NetBeansProjects\App\dist>java -cp lib/AbsoluteLayout.jar;App.jar app.App " –  Aug 16 '19 at 06:44