首页 文章

NoClassDefFoundError spring boot maven

提问于
浏览
0

我使用spring boot编程REST API,它使用一些外部库,这些库通过我的pom.xml中的依赖项包含在内 . 如果我通过 mvn spring-boot:run 在IntelliJ中启动项目一切正常,但如果我尝试通过 mvn package 将所有东西打包到jar中,除了spring-boot之外,所有外部依赖性calsses都会丢失 . 但是,相应的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
组织
---- 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;
    }

}

和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 季启动教程 . 再次坦克

1 回答

  • 0

    pom.xml <build><plugins> ... </plugins></build> 部分添加以下插件 . 构建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

相关问题