-1

I'm trying to create a jar file of my simple java gui application. I'm using Intellij IDEA and maven. I have imported Mig Layout as a maven dependency, when I run the program inside Intellij IDEA everything works fine, but when I create a jar by doing mvn clean install or mvn clean package, although maven says BUILD SUCCESSFUL, when I try to open the jar file I get the following Stacktrace (I believe it's a stack trace). The image Stacktrace Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: net/miginfocom/swing/MigLayout at BotGui.<init>(BotGui.java:29) at java.awt.event.InvocationEvent.dispatch(Unknown Source) at java.awt.EventQueue.dispatchEventImpl(Unknown Source) at java.awt.EventQueue.access$500(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source) Caused by: java.lang.ClassNotFoundException: net.miginfocom.swing.MigLayout at java.net.URLClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) ... 15 more

this is my dependencies in maven.

<?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>com.ptcontact.testdiscord</groupId>
    <artifactId>Discord_Bot</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>com.miglayout</groupId>
            <artifactId>miglayout-swing</artifactId>
            <version>5.0</version>
        </dependency>

        <dependency>
            <groupId>net.dv8tion</groupId>
            <artifactId>JDA</artifactId>
            <version>3.8.0_436</version>
        </dependency>
    </dependencies>

    <repositories>
        <repository>
            <id>jcenter</id>
            <name>jcenter-bintray</name>
            <url>https://jcenter.bintray.com</url>
        </repository>
    </repositories>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <configuration>
                    <target>8</target>
                    <source>8</source>
                </configuration>
            </plugin>
            <plugin>
                <!-- Build an executable JAR -->
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.1.0</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib/</classpathPrefix>
                            <mainClass>BotGui</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-site-plugin</artifactId>
                <version>3.7.1</version>
            </plugin>
        </plugins>
    </build>

</project>

Yes I have tried out version 5.0, 4.2 and 5.2. Additionally, I have tried to add miglayout-core as a dependency as well, but that didn't change anything.

Community
  • 1
  • 1
PtContact
  • 23
  • 5
  • How do you try to run your jar? – WoAiNii May 04 '20 at 18:12
  • Does https://stackoverflow.com/a/45125398/104891 help? – CrazyCoder May 04 '20 at 18:47
  • @WoAiNii 2 two ways: either by double clicking it in windows file explorer or by doing java -jar MyAppName.jar – PtContact May 04 '20 at 18:56
  • @CrazyCoder thank you for trying to help me but unfortunately my issue is entirely different, the issue in that link is caused by Intellij's GUI designer and I'm not using it for this simple project. – PtContact May 04 '20 at 18:57
  • Do you have mig layout classes packaged inside the jar that you are running? Do you use fat jar Maven plug-in to build the jar with all the dependencies? – CrazyCoder May 04 '20 at 18:59
  • @CrazyCoder I have edited the question including the entire maven pom.xml – PtContact May 05 '20 at 08:29
  • How do you place the dependent jars under `lib/` directory next to the main jar? – CrazyCoder May 05 '20 at 08:32
  • @CrazyCoder actually I copied that from the internet idk what that does... – PtContact May 05 '20 at 08:41
  • So you need to learn how to package dependencies in the jar properly, google for Maven fat jar. – CrazyCoder May 05 '20 at 08:42
  • @CrazyCoder so basically if I understood correctly what I was compiling to jar was just my source code, but by creating a "fat" jar I tell maven to add my source code AND all of the repositories I'm using all in a single .jar file. Is that correct? Also, is there any good place when I can learn maven properly? Thanks for the help btw! – PtContact May 05 '20 at 11:55

1 Answers1

0

I Resolved the problem! Like @CrazyCoder said, I needed to create a Fat Jar, in other words I needed to actually include the dependencies inside my jar file. Because I'm a beginner I thought that maven did that for you automatically, oh well.

To create a fat jar, I followed this guide: http://tutorials.jenkov.com/maven/maven-build-fat-jar.html

PtContact
  • 23
  • 5