仅Maven错误:NoClassDefFoundError:伪装/编解码器/编码器

问题描述

我使用Maven向导在Eclipse中创建了一个项目,并编辑了pom.xml文件以包含我的依赖项。我的使用Open Feign的项目在Eclipse中构建和运行,但是当我在命令行中使用Maven构建它时,出现以下运行时错误

Error: Unable to initialize main class edu.mills.cs180a.BookRepositoryImplFeign
Caused by: java.lang.NoClassDefFoundError: feign/codec/Encoder

这是我的pom.xml文件

<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>edu.mills.cs.180a</groupId>
  <artifactId>book-client-example</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>book-client</name>
  <description>Book REST API example</description>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.target>15</maven.compiler.target>
        <maven.compiler.source> 15</maven.compiler.source>
    </properties>
  
   <dependencies>
    <!-- Begin Open Feign dependencies -->
    <!-- https://mvnrepository.com/artifact/io.github.openfeign/feign-core -->
    <dependency>
        <groupId>io.github.openfeign</groupId>
        <artifactId>feign-core</artifactId>
        <version>11.0</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/io.github.openfeign/feign-jackson -->
    <dependency>
        <groupId>io.github.openfeign</groupId>
        <artifactId>feign-jackson</artifactId>
        <version>11.0</version>
    </dependency>
    <!-- End Open Feign dependencies -->
    <dependency>
        <groupId>io.github.openfeign.form</groupId>
        <artifactId>feign-form</artifactId>
        <version>3.8.0</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-openfeign</artifactId>
        <version>2.2.5.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
        <version>2.2.5.RELEASE</version>
    </dependency>
  </dependencies>
</project>

要在Eclipse中构建和运行它,我只需要前两个依赖项。我添加了其余内容,以消除错误

这是一个命令行记录:

$ mvn -v
Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)
Maven home: C:\Program Files\apache-maven-3.6.3
Java version: 15.0.1,vendor: Oracle Corporation,runtime: C:\Program Files\Java\jdk-15.0.1
Default locale: en_US,platform encoding: Cp1252
OS name: "windows 10",version: "10.0",arch: "amd64",family: "windows"

$ mvn clean
[INFO] Scanning for projects...
[INFO]
[INFO] ---------------< edu.mills.cs.180a:book-client-example >----------------
[INFO] Building book-client 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ book-client-example ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  0.153 s
[INFO] Finished at: 2020-11-03T10:54:18-08:00
[INFO] -----------------------------------------------------------------------

$ mvn package
[INFO] Scanning for projects...
[INFO]
[INFO] ---------------< edu.mills.cs.180a:book-client-example >----------------
[INFO] Building book-client 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ book-client-example ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\Users\ellen\eclipse-workspace9\book-client-example\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ book-client-example ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 4 source files to C:\Users\ellen\eclipse-workspace9\book-client-example\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ book-client-example ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\Users\ellen\eclipse-workspace9\book-client-example\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ book-client-example ---
[INFO] No sources to compile
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ book-client-example ---
[INFO] No tests to run.
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ book-client-example ---
[INFO] Building jar: C:\Users\ellen\eclipse-workspace9\book-client-example\target\book-client-example-0.0.1-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  1.748 s
[INFO] Finished at: 2020-11-03T10:54:25-08:00
[INFO] ------------------------------------------------------------------------

$ java -cp target/book-client-example-0.0.1-SNAPSHOT.jar edu.mills.cs180a.BookRepositoryImplFeign
Error: Unable to initialize main class edu.mills.cs180a.BookRepositoryImplFeign
Caused by: java.lang.NoClassDefFoundError: feign/codec/Encoder

另请参阅the repo

更新

below answer,我将其添加pom.xml

    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-dependency-plugin</artifactId>
                    <version>3.1.1</version>
                    <executions>
                        <execution>
                            <phase>compile</phase>
                            <goals>
                                <goal>copy-dependencies</goal>
                            </goals>
                            <configuration>
                                <outputDirectory>${project.build.directory}/lib</outputDirectory>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>

                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-jar-plugin</artifactId>
                    <version>3.2.0</version>
                    <configuration>
                        <archive>
                            <manifest>
                                <addClasspath>true</addClasspath>
                                <classpathPrefix>lib/</classpathPrefix>
                                <mainClass>edu.mills.cs180a.BookRepositoryImplFeign</mainClass>
                            </manifest>
                        </archive>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>

当我尝试构建和运行时,我仍然遇到相同的错误

$ java -jar target/book-client-example-0.0.1-SNAPSHOT.jar
Error: Unable to initialize main class edu.mills.cs180a.BookRepositoryImplFeign
Caused by: java.lang.NoClassDefFoundError: feign/codec/Encoder

解决方法

问题在于您最终如何运行代码。

在编译和构建项目时,Maven会将所有已编译的代码放入jar文件中。这是您添加到类路径以执行的代码。但是您已经知道还有两个未指定的依赖项。

您可能希望Maven进行的操作是将所有依赖项复制到目标文件夹。这可以通过maven dependency plugin完成。 接下来,您可能不想在运行代码时在类路径上指定所有必需的库。至少在我的项目中,我使用maven jar plugin将主类和类路径添加到清单中。

这是我pom.xml中的摘录:

gcc -g -Wall -I ../inc -L ../lib test.c -o test.exe -lFp 

使用pom中的代码,您应该可以像这样运行代码:

gcc -g -Wall -I ../inc test.c -o test.exe ../lib/libFp.a