当前位置: 首页 > 知识库问答 >
问题:

NoClassDefFoundError spring boot maven

鲁烨熠
2023-03-14

我用spring boot编写了一个REST API,它使用一些外部库,这些库是通过pom中的依赖项包含的。xml。如果我通过mvn spring boot在IntelliJ中启动项目:运行一切正常,但如果我尝试通过mvn包将所有内容打包到jar中,则除了spring boot中的依赖项之外,所有外部依赖项都将丢失。但是,相应的jar文件被复制到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>

生成jar的结构(它的一部分):
jar
你好
lib
META-INF
org
----springframework
----这里应该是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;
    }

}

以及应用程序。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教程。又是坦克

共有1个答案

王棋
2023-03-14

在您的pom中添加下面的插件。xml

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

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

 类似资料:

相关问答

相关文章

相关阅读