繁体   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