NoClassDefFoundError spring boot maven

不羁的心 提交于 2019-12-12 04:17:33

问题


Im programming a REST API with spring boot which uses some external libraries which are included via dependency in my pom.xml. If i start the project in IntelliJ via mvn spring-boot:run everything works just fine but if i try to package everything into a jar via mvn package all external dependecy calsses are missing except the ones of spring-boot. However, the corresponding jarfiles are copied into the lib folder of the jar. So if i Start the jar everything works just fine (answering for getRequests etc.) But as soon as i want to initialize a variable of type FFmpegFrameGrabber (which is from bytedeco) i get a NoClassDefFoundError

My POM looks as follows:

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

The structure of the genrerated jar (part of it):
jar
+ hello
+ lib
+ META-INF
+ org
----+ springframework
----+ HERE shoould be bytedeco (?)

Thanks in advance

EDIT: Minimal (Not) working example

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

}

And the 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);
        }
    }

}

This is straight from the spring boot tutorial. tanks again


回答1:


Add the plugin below in your pom.xml <build><plugins> ... </plugins></build> section. Build the maven project and execute the jar command. This plugin will package all your dependent jars into the final executable jar.

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

This page has more information on maven assembly plugin - https://maven.apache.org/plugins/maven-assembly-plugin/usage.html



来源:https://stackoverflow.com/questions/34055029/noclassdeffounderror-spring-boot-maven

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