简体   繁体   English

当我从 Spring Boot Jar 运行但不是通过 Eclipse 时出现 ClassNotFoundException

[英]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.

相关问题 从 java -jar 运行 Spring Boot 应用程序时未提交事务 - Transaction is not committed when spring boot application is run from java -jar 提取 jar 时,spring boot 运行 JarLauncher - spring boot run JarLauncher when extracting jar 我可以从另一个 spring boot 应用程序中编译并运行 spring boot jar 吗? - Can I compile and run a spring boot jar from inside another spring boot application? 构建 jar 并运行 spring 从 cmd 启动 - Build jar and run spring boot from cmd Spring 引导 JAR 文件不适用于 ClassNotFoundException - Spring Boot JAR file not working with ClassNotFoundException 无法使用 ClassNotFoundException 从 PCF CLI 运行 spring 引导批处理作业 - Unable to run spring boot batch job from PCF CLI with ClassNotFoundException 从jar运行类时出现ClassNotFoundException,但是如果我直接在应用程序中运行该类,它将起作用 - ClassNotFoundException when running a class from a jar but if I run the class directly in my application it works 尝试从 jar 文件运行时,Spring Boot 应用程序不会运行 - Spring boot application won't run when trying to run from the jar file 运行 spring-boot-maven-plugin 配置的胖 JAR 时出现 ClassNotFoundException - ClassNotFoundException when running fat JAR configured by spring-boot-maven-plugin spring-boot:run抛出ClassNotFoundException - spring-boot:run throws ClassNotFoundException
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM