IntelliJ – Maven adding external jar file but java.lang.NoClassDefFoundError

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

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

问题

我正在使用 IntelliJ 版本 11.0.7 (2020.1.3) 创建了一个简单的 Maven 项目,并通过以下步骤将我的 JAR 文件添加到其中:

> 文件 -> 项目结构 -> 新建项目库 -> Java -> 选择我的 JAR 文件 -> 确定 -> 确定

在该 JAR 文件中,包含了运行该应用程序所需的所有依赖项。

虽然在编译时没有错误,但当我运行我的 Maven 项目时,它抛出了以下异常:

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

在添加了这个 JAR 后,它会抛出关于下一个缺失的 JAR 的异常,类似地,当我添加了所有在该 JAR 内部使用的依赖项后,一切正常运行。

是否有任何方法可以在将 JAR 添加到项目中时,自动生成所有依赖项并添加到外部库中?

英文:

I am using IntelliJ version 11.0.7 (2020.1.3) created a simple maven project and added my jar to it by

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

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

There are no compile-time errors but when I run my maven project then it is throwing this exception:

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

After adding this jar it is throwing an exception about the next missing jar, likewise when I added all the dependencies which are used inside that jar then everything works fine.

Is there any way to auto-generate all the dependencies and add to External Libraries from the jar when I added to it?

答案1

得分: 0

在那个 JAR 文件中,包含了运行该应用程序所需的所有依赖项。

你确定所有依赖项都存在吗?你接下来的陈述是:当我添加了这个 JAR 后,它抛出关于下一个缺失的 JAR 的异常,类似地,当我添加了该 JAR 内部使用的所有依赖项时,一切都正常工作。`

你的 JAR 文件是否托管在 Maven 仓库中?如果是的话,只需在 Maven 的 pom.xml 文件中声明它,它将管理所有的传递性依赖关系。如果不在 Maven 仓库中,你需要运行 'mvn install' 命令将其安装到本地 Maven 仓库,然后在 pom.xml 文件中引用它。如果你正确地将正确的 pom.xml 包含在你的 JAR 文件中,它还将自动解析你的传递性依赖关系。

英文:

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

Are you sure that all dependencies present? Your next statement saying 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.

Is your jar file hosted in maven repository? If yes, simply declare it in maven pom.xml file, it will manage all the transitive dependencies. If it is not in maven repository, you need to run mvn install command to install that into your local maven repository, later on refer it in your pom.xml file. It will auto resolve your transitive dependencies as well, if you package your jar file properly which include correct pom.xml inside.

答案2

得分: 0

最后,我在我的pom.xml中添加了以下内容,这是在我生成的Jar文件中添加的:

要让Maven从项目构建一个Fat JAR,您必须在项目的POM文件中包含一个Fat JAR构建配置。您可以通过在POM文件的插件部分中包含maven-assembly-plugin来配置Maven从项目构建一个Fat JAR。Maven将其构建的输出产品称为装配件(assembly)。因此有了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
英文:

Finally, I added this in my pom.xml where I generated the Jar file

To get Maven to build a Fat JAR from your project you must include a Fat JAR build configuration in your project's POM file. You configure Maven to build a Fat JAR from your project by including the maven-assembly-plugin in your POM file's plugin section. Maven refers to an output product that it builds as an assembly. Hence the name maven-assembly-plugin.

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

Now it is working as expected.

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

huangapple
  • 本文由 发表于 2020年9月30日 13:03:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/64131217.html
匿名

发表评论

匿名网友

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

确定