簡體   English   中英

IntelliJ - Maven 添加外部 jar 文件但 java.lang.NoClassDefFoundError

[英]IntelliJ - Maven adding external jar file but java.lang.NoClassDefFoundError

我正在使用 IntelliJ 版本 11.0.7 (2020.1.3) 創建了一個簡單的 maven 項目並將我的 jar 添加到其中

File -> Project Structure -> New Project Library -> Java -> Selected my jar -> Ok -> Ok

在該 jar 文件中,存在運行應用程序所需的所有依賴項。

沒有編譯時錯誤,但是當我運行我的 Maven 項目時,它拋出了這個異常:

Exception in thread "main" java.lang.NoClassDefFoundError: com/fasterxml/jackson/core/JsonProcessingException

添加此 jar 后,它會拋出有關下一個丟失的 jar 的異常,同樣,當我添加該 jar 中使用的所有依賴項時,一切正常。

當我添加到 jar 時,有什么方法可以自動生成所有依賴項並從 jar 添加到外部庫中嗎?

in that jar file, there are all the dependencies present which requires to run the application.

您確定所有依賴項都存在嗎? 您的下After adding this jar it is throwing exception about the next missing jar, likewise when I added all the dependencies which are used inside that jar then everything works fine.語句說After adding this jar it is throwing exception about the next missing jar, likewise when I added all the dependencies which are used inside that jar then everything works fine.

您的 jar 文件是否托管在 Maven 存儲庫中? 如果是,只需在 maven pom.xml文件中聲明它,它將管理所有傳遞依賴項。 如果它不在 maven 存儲庫中,則需要運行mvn install命令將其安裝到本地 maven 存儲庫中,稍后在pom.xml文件中引用它。 如果您正確打包了包含正確pom.xml jar 文件,它也會自動解析您的傳遞依賴項。

最后,我在生成 Jar 文件的 pom.xml 中添加了這個

要讓 Maven 從您的項目構建 Fat JAR,您必須在項目的 POM 文件中包含 Fat JAR 構建配置。 通過在 POM 文件的插件部分中包含 maven-assembly-plugin,您可以將 Maven 配置為從您的項目構建 Fat JAR。 Maven 指的是它作為程序集構建的輸出產品。 因此名稱為 maven-assembly-plugin。

<plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>3.3.0</version>
        <configuration>
            <descriptorRefs>
                <descriptorRef>jar-with-dependencies</descriptorRef>
            </descriptorRefs>
        </configuration>
        <executions>
            <execution>
                <id>make-assembly</id>
                <phase>package</phase>
                <goals>
                    <goal>single</goal>
                </goals>
            </execution>
        </executions>
    </plugin>

現在它按預期工作。

參考: http : //tutorials.jenkov.com/maven/maven-build-fat-jar.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM