[英]ClassNotFoundException when I run from a Spring Boot Jar, but not through Eclipse
I have a multiple level Maven project with Spring Boot.我有一个带有 Spring Boot 的多级 Maven 项目。
ParentProject |_ Project1 |_pom.xml |_ LibraryProject |_pom.xml |_ Project2 |_pom.xml
For ParentProject the parent is defined thus:对于 ParentProject,父项的定义如下:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath></relativePath>
</parent>
For Project1, LibraryProject and Project2:对于 Project1、LibraryProject 和 Project2:
<parent>
<groupId>ParentProject</groupId>
<artifactId>ParentProject</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>..</relativePath>
</parent>
Project1 and Project2 depend on classes from LibraryProject. Project1 和 Project2 依赖于 LibraryProject 中的类。 When I execute from Eclipse, Project1 and Project2 both find all of their classes and all of the classes in LibraryProject.
当我从 Eclipse 执行时,Project1 和 Project2 都找到了它们的所有类以及 LibraryProject 中的所有类。 When I create a Project1.jar with
当我创建一个 Project1.jar 时
mvn clean install package
And then run it, I get ClassNotFoundExceptions on the classes that are in LibraryProject.然后运行它,我在 LibraryProject 中的类上得到 ClassNotFoundExceptions。 LibraryProject is a dependency of Project1 and is built before Project1.
LibraryProject 是 Project1 的依赖项,在 Project1 之前构建。
ParentProject uses spring-boot-parent as its parent. ParentProject 使用 spring-boot-parent 作为其父项。 It builds in the order:
它按以下顺序构建:
1) LibraryProject 2) Project1 3) Project2
I've brought in the maven-compiler-plugin, maven-jar-plugin, maven-remote-resources-plugin.我引入了 maven-compiler-plugin、maven-jar-plugin、maven-remote-resources-plugin。
I'm just out of ideas.我只是没有想法。 It compiles in eclipse and maven can build it.
它在 eclipse 中编译,maven 可以构建它。 But running it creates exceptions.
但是运行它会产生异常。
Thank you all in advance.谢谢大家。
It's cause by ClassLoader, SpringBoot run with java -jar
has it's own ClassLoader.It will load Class from BOOT-INF/classes and BOOT-INF/lib,but Eclipse will add all dependency to classpath.这是ClassLoader造成的,SpringBoot用
java -jar
运行有它自己的ClassLoader。它会从BOOT-INF/classes和BOOT-INF/lib加载Class,但是Eclipse会将所有依赖添加到classpath中。 So Eclipse can find the Class.这样 Eclipse 就可以找到 Class。 you can checkout you pom.xml.
你可以检出你的 pom.xml。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.