idea spring boot启动 Application startup failed 和 Unable to start embedded container错误

今天遇见一个蛋疼的bug,开发中的项目公司电脑运行没问题,然后今天我换了台新电脑,导完项目之后自觉一切配置都没问题,可是无论如何都启动不了项目

该项目是spring boot 的启动方式,使用eclipes启动毫无问题,但是idea总是启动到这里报这种错误,错误完整日志如下 

[2018-08-08 14:01:17,323][restartedMain][ERROR][org.springframework.boot.SpringApplication.reportFailure(SpringApplication.java:771)] Application startup failed
org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is org.springframework.boot.context.embedded.EmbeddedServletContainerException: Unable to start embedded Tomcat
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:137) ~[spring-boot-1.5.7.RELEASE.jar:1.5.7.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:537) ~[spring-context-4.3.11.RELEASE.jar:4.3.11.RELEASE]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.5.7.RELEASE.jar:1.5.7.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693) [spring-boot-1.5.7.RELEASE.jar:1.5.7.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360) [spring-boot-1.5.7.RELEASE.jar:1.5.7.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:303) [spring-boot-1.5.7.RELEASE.jar:1.5.7.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118) [spring-boot-1.5.7.RELEASE.jar:1.5.7.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107) [spring-boot-1.5.7.RELEASE.jar:1.5.7.RELEASE]
    at com.itouch.tzfda.Application.main(Application.java:39) [classes/:?]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_144]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_144]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_144]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_144]
    at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) [spring-boot-devtools-1.5.7.RELEASE.jar:1.5.7.RELEASE]
 

截图

 看日志 百度了下错误,都说是jar包的问题,但是当我尝试了好几种重新导jar包的办法后还是没解决

后来梳理了项目结构,项目有两个spring boot应用,采用maven聚合工程

第一个spring boot 启动没任何问题,唯独第二个项目启动有问题

在网上搜罗了很多很多资料后,才发现原来与idea创建工程的方式有关, 假设你的code目录如下:

springboot-project
    |
    --- module1 (springboot项目1)
    |
    --- module2 
    |
    --- module3 (springboot项目2)

那么恭喜你,跟我遇到问题一致可以参考以下解决方法:

如果你的idea工程创建在springboot-project 这一级,即跟目录为 springboot-project,子目录为module1,2,3

在IDEA中运行module3时,在C:\Users\用户名\\AppData\Local\Temp这个目录下,会生成两个tomcat目录, 如下:

而如果 module3做为跟目录 是只会产生一个tomcat文件夹的

其中, tomcat.xxxx.8080这个目录下没有自动生成class文件

在eclipse里运行没有异常,eclipse运行时只会生成tomcat.xxx.8080目录,并且会自动生成class文件。

知道这点就能很好解决我们问题了,spring boot启动时候找不到编译后的class文件 因此出现无法启动问题

而且你会发现在多module下,运行module3项目时,System.getProperty("user.dir")获取的是springboot-project这个目录的路径。

解决方法如图配置如下

这里Working directory配置需要告诉IDE哪里是相对路径的入口,即module3根路径在哪,这样你要启动的项目最后输出的class文件才能输出到正确位置,$MODULE_DIR$表示根路径为你配置的web应用模块的根路径,即module3的跟路径

评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值