7

I create very simple Gui using WindowBuilder plugin for Eclipse. I'm using Swing (maybe a problem?) I've got plenty of runtime errors:

Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: com/jgoodies/common/base/Preconditions
    at com.jgoodies.forms.layout.FormSpec.<init>(FormSpec.java:179)
    at com.jgoodies.forms.layout.ColumnSpec.<init>(ColumnSpec.java:147)
    at com.jgoodies.forms.layout.FormSpecs.<clinit>(FormSpecs.java:62)
    at com.jgoodies.forms.layout.LayoutMap.createRoot(LayoutMap.java:569)
    at com.jgoodies.forms.layout.LayoutMap.getRoot(LayoutMap.java:217)
    at com.jgoodies.forms.layout.ColumnSpec.decode(ColumnSpec.java:199)
    at it.myweb.project.GUI.TestGUI.initialize(TestGUI.java:50)
    at it.myweb.project.GUI.TestGUI.<init>(TestGUI.java:39)
    at it.myweb.project.GUI.TestGUI$1.run(TestGUI.java:26)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756)
    at java.awt.EventQueue.access$500(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:709)
    at java.awt.EventQueue$3.run(EventQueue.java:703)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:726)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
Caused by: java.lang.ClassNotFoundException: com.jgoodies.common.base.Preconditions
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 23 more

Code:

import java.awt.EventQueue;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.RowSpec;

public class TestGUI {

    private JFrame frame;
    private JTextField textField;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    ChatGUI window = new ChatGUI();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public ChatGUI() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 1006, 737);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(new FormLayout(new ColumnSpec[] {
                ColumnSpec.decode("default:grow"),
                ColumnSpec.decode("right:max(50dlu;default)"),},
            new RowSpec[] {
                RowSpec.decode("fill:default:grow"),
                RowSpec.decode("bottom:max(30dlu;default)"),}));

        JTextArea textArea = new JTextArea();
        frame.getContentPane().add(textArea, "1, 1, 2, 1, fill, fill");

        textField = new JTextField();
        frame.getContentPane().add(textField, "1, 2, fill, center");
        textField.setColumns(10);

        JButton btnNewButton = new JButton("Invia");
        frame.getContentPane().add(btnNewButton, "2, 2, center, center");
    }
}

Please note that jgoodies is included in the referenced libraries.

user1315621
  • 3,044
  • 9
  • 42
  • 86

3 Answers3

9

Make sure the project contains both jgoodies-common-x.x.x.jar and jgoodies-forms-x.x.x.jar

If you create a new window project and add a jgoodies component to the UI you will get the jar file, then you can just copy it into your original project and reference it.

tapsey
  • 456
  • 3
  • 14
  • 1
    The "forms" jar was imported automatically (Java 8, Eclipse 2018-12, WindowBuilder 1.9.1) after adding jgoodies' `FormLayout` but the "commons" one wasn't. I had to download version 1.8.1 from the official [homepage](http://www.jgoodies.com/downloads/archive/). – Neph Sep 10 '19 at 09:50
  • Exactly my problem. Thanks – Moritz Schmidt Nov 04 '20 at 18:50
3

jgoodies jar should be included in runtime classpath of your application. You mention that is is included in referenced libraries, which presumably means that it is on compile classpath, but this is not enough. To include jar on runtime classpath use -cp switch when running java application with command line.

hgrey
  • 3,033
  • 17
  • 21
  • Thanks. So how can I do it in Eclipse? – user1315621 Jun 21 '16 at 09:36
  • There are several ways of doing that in Eclipse. For example in run configuration of the application go to "Classpath" tab and add jgoodies jar there by clicking on "Add JARs...". – hgrey Jun 21 '16 at 09:53
  • In run\debug setting I add jgoodies jar in both boostrap entries and user entries but I still got the same error – user1315621 Jun 21 '16 at 12:02
  • what is the name of the jar you have added there? – hgrey Jun 21 '16 at 12:31
  • 1
    jgoodies-forms-1.8.0.jar and jgoodies-forms-1.8.0-sources.jar – user1315621 Jun 21 '16 at 13:52
  • 1
    ok, you need to include jgoodies-common-1.x.0.jar, most likely 1.8.0 would be correct version. Also you don't need to include jgoodies-forms-1.8.0-sources.jar, but it would not hurt. – hgrey Jun 21 '16 at 14:03
  • I just got the same error in Eclipse 2018-12 (with Java 8, WindowBuilder 1.9.1): Adding a `FormLayout` automatically imported both `jgoodies-forms-1.8.0` jars but it didn't add the "common" one, which caused the error. I had to download "JGoodies Common 1.8.1" (`jgoodies-common-1.8.1.jar`) myself from [here](http://www.jgoodies.com/downloads/archive/), then added it to the Java Build Path (Project - Properties - Java Build Path - Libraries) and it's working. No need to add it to "Run" too (Run - Run Configurations - Classpath). – Neph Sep 10 '19 at 09:47
3

If your project is maven, it is better to add jgoodies as maven dependency:

<dependency>
    <groupId>com.jgoodies</groupId>
    <artifactId>jgoodies-forms</artifactId>
    <version>1.8.0</version>
</dependency>

This solve all problems.

Mostafa Barmshory
  • 1,849
  • 24
  • 39