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



我使用IntelliJ 11.0.7版(2020.1.3(创建了一个简单的maven项目,并通过将我的jar添加到其中

文件->项目结构->新建项目库->Java->选择了我的罐子->好的->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.

您的jar文件是否托管在maven存储库中?如果是,只需在mavenpom.xml文件中声明它,它将管理所有可传递的依赖项。如果它不在maven存储库中,则需要运行mvn install命令将其安装到本地maven存储库中,稍后在pom.xml文件中引用它。如果您正确地打包jar文件(其中包含正确的pom.xml(,它也将自动解析您的可传递依赖项。

最后,我在pom.xml中添加了它,在那里我生成了Jar文件

为了让Maven从您的项目中构建一个FatJAR,您必须在项目的POM文件中包含一个Fat JAR构建配置。通过在POM文件的插件部分中包含Maven汇编插件,您可以配置Maven从您的项目构建一个FatJAR。Maven指的是它作为程序集构建的输出产品。因此命名为maven汇编插件。

<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

相关内容

最新更新