'Unable to create tempDir, java.io.tmpdir is set to C:\Windows\

I'm using Spring Boot with embedded tomcat, everything worked fine and suddenly I got the error :

Caused by: org.springframework.boot.context.embedded.EmbeddedServletContainerException: Unable to create tempDir. java.io.tmpdir is set to C:\Windows\
    at org.springframework.boot.context.embedded.AbstractEmbeddedServletContainerFactory.createTempDir(AbstractEmbeddedServletContainerFactory.java:183)
    at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory.getEmbeddedServletContainer(TomcatEmbeddedServletContainerFactory.java:165)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.createEmbeddedServletContainer(EmbeddedWebApplicationContext.java:164)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:134)
    ... 11 common frames omitted
Caused by: java.io.IOException: Access is denied
    at java.io.WinNTFileSystem.createFileExclusively(Native Method)
    at java.io.File.createTempFile(File.java:2024)
    at java.io.File.createTempFile(File.java:2070)
    at org.springframework.boot.context.embedded.AbstractEmbeddedServletContainerFactory.createTempDir(AbstractEmbeddedServletContainerFactory.java:174)
    ... 14 common frames omitted

I didn't do any manipulations with user or system variables.

My TEMP user variable is looking on C:/Users/me/AppData/Local/Temp , and I guess tomcat has to use this value insted of system one, which is actually C:/Windows/Temp



Solution 1:[1]

If you use IDEA check "Include parent environment variables" in Environment Variables window in the Run/Debug Configuration.

Solution 2:[2]

On Windows GetTempPathA is used to locate temp directory. Algorithm:

1. The path specified by the TMP environment variable.
2. The path specified by the TEMP environment variable.
3. The path specified by the USERPROFILE environment variable.
4. The Windows directory.

So if your app is started without TMP & TEMP & USERPROFILE defined you'll get java.io.tmpdir == c:\Windows (https://docs.microsoft.com/en-us/windows/desktop/api/sysinfoapi/nf-sysinfoapi-getwindowsdirectorya).

Typically applications set java.io.tmpdir inside app-run.bat (via -D...=...) or app.properties.

I hit this problem because Gradle Test task won't pass environment variables if environment properties aren't passed but replaced:

test {
    environment = ["A": "1", "B": "2"] // won't work, because it replaces envs
}

test {
    environment( ["A": "1", "B": "2"] ) // will work, because it appends to existing envs
}

Solution 3:[3]

I observed the following behaviour

  • changed all out of a sudden
  • works if run from commandline as self-contained jar
  • fails when run from IntelliJ (2018.1)

As a quick workaround i explicitly added -Djava.io.tmpdir=$EXISING_DIR_WITH_WRITE_ACCESS as JVM parameter in run configurations.

Solution 4:[4]

If you use eclipse check "Append to environment variables" in Environment Variables window in the Run/Debug Configuration.

Thanks to @max answer above

I was using JAVA EE eclipse - photon

Solution 5:[5]

In my case the problem occurred when I changed my default workspace library from [jre.1.8.0_121] to [jdk.1.8.0_121]. Setting it back to jre seems to have fixed the problem.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2
Solution 3 HannesB
Solution 4 Abdeali Chandanwala
Solution 5 rodicc