【问题标题】:NoClassDefFoundError spring boot mavenNoClassDefFoundError spring boot maven
【发布时间】:2016-03-07 10:04:16
【问题描述】:

我正在使用 Spring Boot 编写一个 REST API,它使用一些外部库,这些库通过依赖项包含在我的 pom.xml 中。 如果我通过 mvn spring-boot:run 在 IntelliJ 中启动项目,一切正常,但如果我尝试通过 mvn package 将所有内容打包到 jar 中,所有外部依赖项都是除了 spring-boot 之外,其他的都丢失了。但是,对应的 jarfile 会被复制到 jar 的 lib 文件夹中。 因此,如果我启动 jar 一切正常(回答 getRequests 等)但只要我想初始化一个 FFmpegFrameGrabber 类型的变量(来自 bytedeco)我得到一个 NoClassDefFoundError

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

    <groupId>spring-boot-test</groupId>
    <artifactId>test</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.2.7.RELEASE</version>
    </parent>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.bytedeco</groupId>
            <artifactId>javacv</artifactId>
            <version>1.1</version>
        </dependency>

    </dependencies>

    <build>
        <finalName>${project.artifactId}</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

genrerated jar 的结构(部分):
罐子
+ 你好
+ 库
+ META-INF
+ 组织
----+ 弹簧框架
----+ 这里应该是 bytedeco (?)

提前致谢

编辑:最小(非)工作示例

package hello;

import org.bytedeco.javacv.FFmpegFrameGrabber;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;

@RestController
public class HelloController {

    @RequestMapping("/")
    public String index() {
        String path = "D:\\TestVideos\\1\\original.mp4";
        FFmpegFrameGrabber frameGrabber;
        System.out.println("Starting Frame Grabber for: "  + path);
        frameGrabber = new FFmpegFrameGrabber(path);
        try {
            frameGrabber.start();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return "Greetings from Spring Boot! Opening: " + path;
    }

}

还有 Application.java

package hello;

import java.util.Arrays;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        ApplicationContext ctx = SpringApplication.run(Application.class, args);

        System.out.println("Let's inspect the beans provided by Spring Boot:");

        String[] beanNames = ctx.getBeanDefinitionNames();
        Arrays.sort(beanNames);
        for (String beanName : beanNames) {
            System.out.println(beanName);
        }
    }

}

这直接来自 Spring Boot 教程。 又是坦克

【问题讨论】:

  • 我不熟悉 bytedeco,但它的 jar 也应该在你的 jar 的 lib 文件夹中,并且它的所有类都应该可用。也许它做了一些不寻常的事情?你能提供一个重现问题的小样本吗?
  • 它是 ffmpeg 的 hava 包装器。所有 ffmpeg jar 都在 libfolder 中,但在适当的地方没有类文件。

标签: java maven spring-boot dependencies noclassdeffounderror


【解决方案1】:

在您的 pom.xml &lt;build&gt;&lt;plugins&gt; ... &lt;/plugins&gt;&lt;/build&gt; 部分添加下面的插件。 构建maven项目并执行jar命令。 这个插件会将你所有的依赖jar打包到最终的可执行jar中。

        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.6</version>
            <configuration>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
            </configuration>
        </plugin>

此页面有更多关于 maven 程序集插件的信息 - https://maven.apache.org/plugins/maven-assembly-plugin/usage.html

【讨论】:

  • 这不起作用,因为它有点弄乱了 Spring Boot 的路径。通常, spring-boot:repackage 会在 maven 生成的 jar 中调整这些名称(如果我是正确的)。因此,我需要在具有依赖关系的 jar 上调用 spring-boot:repackage。有任何想法吗?使用依赖项启动jar时的错误:org.springframework.context.ApplicationContextException: Unable to start embedde d container;嵌套异常是 org.springframework.context.ApplicationContextE xception: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedS ervletContainerFactory bean。
  • 好的。你试过this
猜你喜欢
  • 2016-01-14
  • 1970-01-01
  • 2021-08-01
  • 2015-12-08
  • 2019-04-16
  • 2017-07-15
  • 2012-02-02
  • 2019-10-19
  • 2022-01-16
相关资源
最近更新 更多