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

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
}

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


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.