MigLayout 在创建 Maven JAR 时出现 ClassNotFoundException。

huangapple go评论77阅读模式
英文:

MigLayout ClassNotFoundException when creating maven jar

问题

这是我在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>

是的,我尝试过版本 5.0、4.2 和 5.2。另外,我还尝试将 miglayout-core 作为一个依赖项添加,但没有改变任何事情。

英文:

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 &quot;AWT-EventQueue-0&quot; java.lang.NoClassDefFoundError: net/miginfocom/swing/MigLayout at BotGui.&lt;init&gt;(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.

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot;
xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
&lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
&lt;groupId&gt;com.ptcontact.testdiscord&lt;/groupId&gt;
&lt;artifactId&gt;Discord_Bot&lt;/artifactId&gt;
&lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;
&lt;dependencies&gt;
&lt;dependency&gt;
&lt;groupId&gt;com.miglayout&lt;/groupId&gt;
&lt;artifactId&gt;miglayout-swing&lt;/artifactId&gt;
&lt;version&gt;5.0&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;net.dv8tion&lt;/groupId&gt;
&lt;artifactId&gt;JDA&lt;/artifactId&gt;
&lt;version&gt;3.8.0_436&lt;/version&gt;
&lt;/dependency&gt;
&lt;/dependencies&gt;
&lt;repositories&gt;
&lt;repository&gt;
&lt;id&gt;jcenter&lt;/id&gt;
&lt;name&gt;jcenter-bintray&lt;/name&gt;
&lt;url&gt;https://jcenter.bintray.com&lt;/url&gt;
&lt;/repository&gt;
&lt;/repositories&gt;
&lt;build&gt;
&lt;plugins&gt;
&lt;plugin&gt;
&lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
&lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
&lt;version&gt;3.8.0&lt;/version&gt;
&lt;configuration&gt;
&lt;target&gt;8&lt;/target&gt;
&lt;source&gt;8&lt;/source&gt;
&lt;/configuration&gt;
&lt;/plugin&gt;
&lt;plugin&gt;
&lt;!-- Build an executable JAR --&gt;
&lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
&lt;artifactId&gt;maven-jar-plugin&lt;/artifactId&gt;
&lt;version&gt;3.1.0&lt;/version&gt;
&lt;configuration&gt;
&lt;archive&gt;
&lt;manifest&gt;
&lt;addClasspath&gt;true&lt;/addClasspath&gt;
&lt;classpathPrefix&gt;lib/&lt;/classpathPrefix&gt;
&lt;mainClass&gt;BotGui&lt;/mainClass&gt;
&lt;/manifest&gt;
&lt;/archive&gt;
&lt;/configuration&gt;
&lt;/plugin&gt;
&lt;plugin&gt;
&lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
&lt;artifactId&gt;maven-site-plugin&lt;/artifactId&gt;
&lt;version&gt;3.7.1&lt;/version&gt;
&lt;/plugin&gt;
&lt;/plugins&gt;
&lt;/build&gt;
&lt;/project&gt;

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.

答案1

得分: 0

我解决了这个问题!
就像 @CrazyCoder 说的那样,我需要创建一个“Fat Jar”,换句话说,我实际上需要在我的 jar 文件内部包含这些依赖项。因为我是初学者,我以为 Maven 会自动为你做这些,哦,算了。

为了创建一个“Fat Jar”,我遵循了这个指南:
http://tutorials.jenkov.com/maven/maven-build-fat-jar.html

英文:

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

huangapple
  • 本文由 发表于 2020年5月5日 00:23:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/61596915.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定