仅Maven错误:NoClassDefFoundError:假装/编解码器/编码器 [英] Error only with maven: NoClassDefFoundError: feign/codec/Encoder

查看:74
本文介绍了仅Maven错误:NoClassDefFoundError:假装/编解码器/编码器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I created a project in Eclipse with the Maven wizard and edited the pom.xml file to include my dependencies. My project, which uses Open Feign, builds and runs in Eclipse, but I get the following runtime error when I build it at the command line with 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中构建和运行它,我只需要前两个依赖项.我添加了其余部分,以消除错误.

To build and run it in Eclipse, I only need the first two dependencies. I added the rest in an attempt to eliminate the error.

这是一个命令行记录:

$ 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

另请参见回购.

更新

每个下面的答案,我将其添加到 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>

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

I still get the same error when I try building and running:

$ 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

推荐答案

问题是您最终如何运行代码.

The problem is how you run your code in the end.

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

When you compile and build your project, Maven will put all your compiled code into your jar file. This is the one you add to the classpath for execution. But you already know there are two more dependencies which you did not specify.

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

What you may want Maven to do is copy all your dependencies to your target folder. This can be done via the maven dependency plugin. Next, you probably do not want to specify all the required libs on the classpath when you run your code. At least in my projects I add the main class and the classpath into the manifest using the maven jar plugin.

这是我pom.xml中的摘录:

Here is a snippet from my pom.xml:

<project>
  <build>
    <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>
  </build>
</project>

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

With that in your pom you should be able to run your code like this:

java -jar target\book-client-example-0.0.1-SNAPSHOT.jar

这篇关于仅Maven错误:NoClassDefFoundError:假装/编解码器/编码器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆