NoClassDefFoundError 当我运行一个使用 Maven 打包的 JAR 文件时发生

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

NoClassDefFoundError as I run a Maven packaged JAR file

问题

我正在使用Maven从命令行编译和打包Java应用程序:

mvn clean
mvn compile
mvn package

这些命令可以正常运行。但是当我尝试运行生成的JAR文件时:

java -jar target/JarFile.jar

我遇到了以下错误:

Error: Unable to initialize main class com.company.Main
Caused by: java.lang.NoClassDefFoundError: com/google/api/client/json/JsonFactory

这是一个来自外部依赖的类。这些依赖项也由Maven管理(即,我没有在本地提供它们,而是在我的POM文件中列出它们)。由于我能够在集成开发环境(IDE)中运行应用程序,我认为我缺少一个使依赖项在JAR文件中可用的步骤。以下是我的POM文件:

<?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>

    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <groupId>test-maven-project</groupId>
    <artifactId>test</artifactId>
    <version>1.0</version>
    
    <dependencies>
        <dependency>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
        </dependency>
        <dependency>
            <groupId>com.google.oauth-client</groupId>
            <artifactId>google-oauth-client</artifactId>
            <version>1.30.6</version>
        </dependency>
        <!-- other dependencies... -->
    </dependencies>

    <build>
        <plugins>
            <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>com.company.Main</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

你可以指导我如何使依赖项在运行时可访问吗?

英文:

I am using Maven to compile and package a Java application from the command line:

mvn clean
mvn compile
mvn package

These commands run without a problem. But when I try to run the generated JAR file

java -jar target/JarFile.jar

I get the following error:

Error: Unable to initialize main class com.company.Main
Caused by: java.lang.NoClassDefFoundError: com/google/api/client/json/JsonFactory

Which is a class from an external dependency. These dependencies are also managed by Maven (i.e., I am not supplying them locally, but listing them in my POM file.) Since I am able to run the application inside the IDE, I assume I am missing a step to make the dependencies available to the JAR file. Here is my POM file:

&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;properties&gt;
        &lt;maven.compiler.source&gt;1.8&lt;/maven.compiler.source&gt;
        &lt;maven.compiler.target&gt;1.8&lt;/maven.compiler.target&gt;
    &lt;/properties&gt;

    &lt;groupId&gt;test-maven-project&lt;/groupId&gt;
    &lt;artifactId&gt;test&lt;/artifactId&gt;
    &lt;version&gt;1.0&lt;/version&gt;
    
    &lt;dependencies&gt;
        &lt;dependency&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.1&lt;/version&gt;
        &lt;/dependency&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;com.google.oauth-client&lt;/groupId&gt;
            &lt;artifactId&gt;google-oauth-client&lt;/artifactId&gt;
            &lt;version&gt;1.30.6&lt;/version&gt;
        &lt;/dependency&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;com.google.http-client&lt;/groupId&gt;
            &lt;artifactId&gt;google-http-client&lt;/artifactId&gt;
            &lt;version&gt;1.34.2&lt;/version&gt;
        &lt;/dependency&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;com.google.api-client&lt;/groupId&gt;
            &lt;artifactId&gt;google-api-client&lt;/artifactId&gt;
            &lt;version&gt;1.30.9&lt;/version&gt;
        &lt;/dependency&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;com.google.http-client&lt;/groupId&gt;
            &lt;artifactId&gt;google-http-client-jackson2&lt;/artifactId&gt;
            &lt;version&gt;1.34.2&lt;/version&gt;
        &lt;/dependency&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;com.google.apis&lt;/groupId&gt;
            &lt;artifactId&gt;google-api-services-customsearch&lt;/artifactId&gt;
            &lt;version&gt;v1-rev20200401-1.30.9&lt;/version&gt;
        &lt;/dependency&gt;
    &lt;/dependencies&gt;

    &lt;build&gt;
        &lt;plugins&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;com.company.Main&lt;/mainClass&gt;
                        &lt;/manifest&gt;
                    &lt;/archive&gt;
                &lt;/configuration&gt;
            &lt;/plugin&gt;
        &lt;/plugins&gt;
    &lt;/build&gt;
&lt;/project&gt;

Could you point me in the right direction so that the dependencies are accessible at runtime?

答案1

得分: 1

运行jar包的命令行命令时,您必须在命令行中显式指定类路径,或者构建一个可执行的jar包:

链接:https://stackoverflow.com/q/574594/927493

顺便提一下:mvn package命令已经会执行mvn compile,因此您不需要显式调用mvn compile命令。

英文:

To run your jar from commandline, you must either explicitly specify the classpath on the command line or build an executable jar:

https://stackoverflow.com/q/574594/927493

BTW: mvn package already executes mvn compile, so you need not call mvn compile explicitly.

答案2

得分: -1

使用以下内容替换您的 pom.xml 文件,并重新构建项目:

<dependency>
    <groupId>com.google.api.client</groupId>
    <artifactId>google-api-client-json</artifactId>
    <version>1.1.0-alpha</version>
</dependency>
英文:

Use this in your pom.xml and rebuild your project.

&lt;dependency&gt;
&lt;groupId&gt;com.google.api.client&lt;/groupId&gt;
&lt;artifactId&gt;google-api-client-json&lt;/artifactId&gt;
&lt;version&gt;1.1.0-alpha&lt;/version&gt;
&lt;/dependency&gt;

huangapple
  • 本文由 发表于 2020年4月10日 02:28:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/61127825.html
匿名

发表评论

匿名网友

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

确定