java - 无法创建 tempDir。 java.io.tmpdir 设置为/tmp

标签 java spring spring-boot jar

在 ubuntu 服务器上运行我的 springboot web 服务 java jar 应用程序会导致如下所示的错误,我已经对该错误进行了一些研究,但是我无法得到解决方案。我用来运行 jar 文件的命令是

java -jar mySpringBootApp.jar

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:157) ~[spring-boot-2.1.1.RELEASE.jar!/:2.1.1.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:540) ~[spring-context-5.1.3.RELEASE.jar!/:5.1.3.RELEASE]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:142) ~[spring-boot-2.1.1.RELEASE.jar!/:2.1.1.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:775) [spring-boot-2.1.1.RELEASE.jar!/:2.1.1.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.1.1.RELEASE.jar!/:2.1.1.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:316) [spring-boot-2.1.1.RELEASE.jar!/:2.1.1.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260) [spring-boot-2.1.1.RELEASE.jar!/:2.1.1.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248) [spring-boot-2.1.1.RELEASE.jar!/:2.1.1.RELEASE]
    at aacctt.payments.org.Application.main(Application.java:18) [classes!/:0.0.1-SNAPSHOT]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_191]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_191]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_191]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_191]
    at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48) [ mySpringBootApp-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
    at org.springframework.boot.loader.Launcher.launch(Launcher.java:87) [ mySpringBootApp-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
    at org.springframework.boot.loader.Launcher.launch(Launcher.java:50) [ mySpringBootApp-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
    at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51) [ mySpringBootApp-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
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:182) ~[spring-boot-2.1.1.RELEASE.jar!/:2.1.1.RELEASE]
    at org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory.getWebServer(TomcatServletWebServerFactory.java:162) ~[spring-boot-2.1.1.RELEASE.jar!/:2.1.1.RELEASE]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.createWebServer(ServletWebServerApplicationContext.java:181) ~[spring-boot-2.1.1.RELEASE.jar!/:2.1.1.RELEASE]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:154) ~[spring-boot-2.1.1.RELEASE.jar!/:2.1.1.RELEASE]
    ... 16 common frames omitted
Caused by: java.io.IOException: Read-only file system
    at java.io.UnixFileSystem.createFileExclusively(Native Method) ~[na:1.8.0_191]
    at java.io.File.createTempFile(File.java:2024) ~[na:1.8.0_191]
    at java.io.File.createTempFile(File.java:2070) ~[na:1.8.0_191]
    at org.springframework.boot.web.server.AbstractConfigurableWebServerFactory.createTempDir(AbstractConfigurableWebServerFactory.java:173) ~[spring-boot-2.1.1.RELEASE.jar!/:2.1.1.RELEASE]
    ... 19 common frames omitted

最佳答案

如果您在 Kubernetes 中部署 Spring boot 应用程序时遇到此问题,值得检查的配置之一是 readOnlyRootFilesystem。在您尝试有意运行只读容器之前,必须将其设置为 false。

它可以在部署配置中使用,如下所示:

template:
    metadata:
      labels:
        app: hello
    spec:
      containers:
      - name: hello
        image: hello-world
        securityContext:
          runAsUser: 1000
          readOnlyRootFilesystem: false ## this should not be true

关于java - 无法创建 tempDir。 java.io.tmpdir 设置为/tmp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58981148/

相关文章:

linux - 尝试使用 Spring Tools Suite 导入时端口超出范围 -1

java - 尝试将对象添加到列表并将其保存在 Spring Boot 上的 MySQL 中时出现 UnsupportedOperationException

spring - @PreAuthorize 与表达式中的 Bean (Spring Boot)

gradle - "Address already in use: bind"运行 Gradle JUnit 测试时出现异常

java - com.google.maps.api.android.lib6.gmm6.vector.ct 处的致命异常 : java. lang.ArrayIndexOutOfBoundsException

java - 类加载器 loadClass 抛出 ClassNotFoundException

java - 时间戳 hibernate

java - 直接调用@Bean注解的方法——调用@Service类中bean的函数

java - Spring Data JPA - "could not initialize proxy - no Session"- 方法标记为事务性

c# - Fluent APIs - 返回这个还是新的?