NoClassDefFoundError春季启动Maven [英] NoClassDefFoundError spring boot maven
问题描述
我用spring boot编程REST API,它使用一些外部库,这些库通过依赖关系包含在我的pom.xml中. 如果我通过 mvn spring-boot:run 在IntelliJ中启动项目,那么一切都很好,但是如果我尝试通过 mvn package 将所有内容打包到jar中,则所有外部依赖关系都将得到解决.除了spring-boot之外的其他东西都没有.但是,相应的jarfiles被复制到jar的lib文件夹中. 因此,如果我启动jar,一切正常(回答getRequests等),但是一旦我想初始化 FFmpegFrameGrabber 类型的变量(来自bytedeco)我收到 NoClassDefFoundError
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
我的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>
带盖罐子的结构(一部分):
罐子
+你好
+ lib
+ META-INF
+ org
---- + springframework
---- +这里应该是bytedeco(?)
The structure of the genrerated jar (part of it):
jar
+ hello
+ lib
+ META-INF
+ org
----+ springframework
----+ HERE shoould be 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
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);
}
}
}
这直接来自spring boot教程. 再来一次
This is straight from the spring boot tutorial. tanks again
推荐答案
在pom.xml
<build><plugins> ... </plugins></build>
部分中添加以下插件.
生成Maven项目并执行jar命令.
该插件会将您所有的依赖jar打包到最终的可执行jar中.
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>
此页面具有有关maven程序集插件的更多信息- https://maven.apache.org/plugins/maven-assembly-plugin/usage.html
This page has more information on maven assembly plugin - https://maven.apache.org/plugins/maven-assembly-plugin/usage.html
这篇关于NoClassDefFoundError春季启动Maven的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!