Error only with maven: NoClassDefFoundError: feign/codec/Encoder

白昼怎懂夜的黑 提交于 2021-02-05 11:26:27

问题


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

Here is my pom.xml file:

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

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.

Here is a command-line transcript:

$ 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

See also the repo.

Update

Per below answer, I added this to 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

回答1:


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

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.

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.

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>

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


来源:https://stackoverflow.com/questions/64668933/error-only-with-maven-noclassdeffounderror-feign-codec-encoder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!