How do I make this simple project work using maven?

I am trying tof figure out how to distribute a simple Hello World app as a jar file using maven.  Project code is a s follows:

import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;

import javax.swing.*;

/**
* Created by Ian on 5/3/14.
*/
public class HelloWorlds {
    private JPanel panel1;
    private JButton clickMeButton;
    boolean toggle = false;

    public static void main(String[] args) {
        JFrame frame = new JFrame("HelloWorlds");
        frame.setContentPane(new HelloWorlds().panel1);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }

    {
// GUI initializer generated by IntelliJ IDEA GUI Designer
// >>> IMPORTANT!! <<<
// DO NOT EDIT OR ADD ANY CODE HERE!
        $$$setupUI$$$();
    }

    /**
     * Method generated by IntelliJ IDEA GUI Designer
     * >>> IMPORTANT!! <<<
     * DO NOT edit this method OR call it in your code!
     *
     * @noinspection ALL
     */
    private void $$$setupUI$$$() {
        panel1 = new JPanel();
        panel1.setLayout(new FormLayout("fill:d:grow", "center:d:noGrow,top:3dlu:noGrow,center:max(d;4px):noGrow"));
        final JLabel label1 = new JLabel();
        label1.setText("Hello Worlds");
        CellConstraints cc = new CellConstraints();
        panel1.add(label1, cc.xy(1, 1, CellConstraints.CENTER, CellConstraints.DEFAULT));
        clickMeButton = new JButton();
        clickMeButton.setText("Click Me");
        panel1.add(clickMeButton, cc.xy(1, 3));
    }

    /**
     * @noinspection ALL
     */
    public JComponent $$$getRootComponent$$$() {
        return panel1;
    }
}

POM file is as follows:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>net.wegr.apps</groupId>
    <artifactId>HelloWorld</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>com.jgoodies</groupId>
            <artifactId>jgoodies-forms</artifactId>
            <version>1.7.2</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>HelloWorlds</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                     <source>1.7</source>
                     <target>1.7</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

</project>

I keep getting the following error

Ian42: ~ >:java -jar HelloWorld-1.0-SNAPSHOT.jar
Exception in thread "main" java.lang.NoClassDefFoundError: com/jgoodies/forms/layout/FormLayout
 at HelloWorlds.$$$setupUI$$$(HelloWorlds.java:38)
 at HelloWorlds.<init>(HelloWorlds.java:26)
 at HelloWorlds.main(HelloWorlds.java:16)
Caused by: java.lang.ClassNotFoundException: com.jgoodies.forms.layout.FormLayout
 at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
 at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
 at java.security.AccessController.doPrivileged(Native Method)
 at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
 at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
 at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
 at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
 ... 3 more

Ian42: ~ >:


Any help would be appreciated!
1 comment

You need to tell maven jar plugin to add your project classpath to the manifest as described here:
http://maven.apache.org/shared/maven-archiver/examples/classpath.html

PS Your problem is not really related to any kind of IDE (including IntelliJ IDEA of course) and I think it is better to post this kind of questions on websites like stackoverflow.com.

0

Please sign in to leave a comment.