项目场景:
程序启动时报错:Application run failed org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.boot.web.server.WebServerException: Unable to create tempDir. java.io.tmpdir is set to ./tmp
原文:
2023-08-18 11:09:51.416 ERROR 20894 --- [ main] o.s.boot.SpringApplication : Application run failed
org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.boot.web.server.WebServerException: Unable to create tempDir. java.io.tmpdir is set to ./tmp
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:161) ~[spring-boot-2.3.12.RELEASE.jar!/:2.3.12.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:545) ~[spring-context-5.2.15.RELEASE.jar!/:5.2.15.RELEASE]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:143) ~[spring-boot-2.3.12.RELEASE.jar!/:2.3.12.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:755) [spring-boot-2.3.12.RELEASE.jar!/:2.3.12.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:747) [spring-boot-2.3.12.RELEASE.jar!/:2.3.12.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:402) [spring-boot-2.3.12.RELEASE.jar!/:2.3.12.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:312) [spring-boot-2.3.12.RELEASE.jar!/:2.3.12.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1247) [spring-boot-2.3.12.RELEASE.jar!/:2.3.12.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1236) [spring-boot-2.3.12.RELEASE.jar!/:2.3.12.RELEASE]
at cn.tongdun.atreus.loan.AppMain.main(AppMain.java:23) [classes!/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_333]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_333]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_333]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_333]
at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48) [atreus-loan.jar:na]
at org.springframework.boot.loader.Launcher.launch(Launcher.java:87) [atreus-loan.jar:na]
at org.springframework.boot.loader.Launcher.launch(Launcher.java:51) [atreus-loan.jar:na]
at org.springframework.boot.loader.PropertiesLauncher.main(PropertiesLauncher.java:597) [atreus-loan.jar:na]
Caused by: org.springframework.boot.web.server.WebServerException: Unable to create tempDir. java.io.tmpdir is set to ./tmp
at org.springframework.boot.web.server.AbstractConfigurableWebServerFactory.createTempDir(AbstractConfigurableWebServerFactory.java:195) ~[spring-boot-2.3.12.RELEASE.jar!/:2.3.12.RELEASE]
at org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory.getWebServer(TomcatServletWebServerFactory.java:180) ~[spring-boot-2.3.12.RELEASE.jar!/:2.3.12.RELEASE]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.createWebServer(ServletWebServerApplicationContext.java:178) ~[spring-boot-2.3.12.RELEASE.jar!/:2.3.12.RELEASE]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:158) ~[spring-boot-2.3.12.RELEASE.jar!/:2.3.12.RELEASE]
... 17 common frames omitted
Caused by: java.nio.file.NoSuchFileException: ./tmp/tomcat.8094.5929425290742081680
at sun.nio.fs.UnixException.translateToIOException(UnixException.java:86) ~[na:1.8.0_333]
at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:102) ~[na:1.8.0_333]
at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:107) ~[na:1.8.0_333]
at sun.nio.fs.UnixFileSystemProvider.createDirectory(UnixFileSystemProvider.java:384) ~[na:1.8.0_333]
at java.nio.file.Files.createDirectory(Files.java:674) ~[na:1.8.0_333]
at java.nio.file.TempFileHelper.create(TempFileHelper.java:136) ~[na:1.8.0_333]
at java.nio.file.TempFileHelper.createTempDirectory(TempFileHelper.java:173) ~[na:1.8.0_333]
at java.nio.file.Files.createTempDirectory(Files.java:991) ~[na:1.8.0_333]
at org.springframework.boot.web.server.AbstractConfigurableWebServerFactory.createTempDir(AbstractConfigurableWebServerFactory.java:189) ~[spring-boot-2.3.12.RELEASE.jar!/:2.3.12.RELEASE]
... 20 common frames omitted
问题描述:
应用程序在启动时遇到了问题。
错误信息表明应用程序无法启动Web服务器,具体的错误是 org.springframework.boot.web.server.WebServerException: Unable to create tempDir. java.io.tmpdir is set to ./tmp
。
原因分析:
这个错误意味着应用程序无法创建临时目录(tempDir),因为系统属性 java.io.tmpdir
被设置为 ./tmp
。然后出现了 java.nio.file.NoSuchFileException
异常,报告找不到 ./tmp/tomcat.8094.5929425290742081680
这个目录。
可能存在以下两种原因:
-
临时目录权限问题:应用程序没有足够的权限在指定的目录下创建临时文件。你可以确保应用程序所运行的用户或进程具有在该目录下创建文件的权限。
-
指定的临时目录不存在:应用程序尝试使用的临时目录
./tmp
在当前工作目录下不存在。你可以手动创建这个目录或者将临时目录更改为已存在的有效路径。
解决方案:
解决这个问题,可以采取以下步骤:
-
确保应用程序运行的用户或进程具有在指定目录下创建文件的权限。
-
在当前应用目录下创建指定的临时目录
./tmp
,确保应用程序可以在其中创建临时文件。
mkdir tmp
-
修改应用程序的配置,将临时目录更改为一个已存在且具有正确权限的目录。