I get an exception saying 'the system cannot find the path specified' while calling java function createTempFile("test", "test")
.
Tried googling but no luck.
Does anyone know from where java gets its default temp path and how can it be not found?
Windows variables seem to be correct and changing them does not affect java.

- 36,091
- 7
- 95
- 123

- 1,817
- 15
- 50
- 85
-
2check whats in String dirName = System.getProperty("java.io.tmpdir"); And also maybe try define tempDir direclty with: File.createTempFile(String prefix, String suffix, File directory) – Horuss Aug 08 '13 at 09:13
-
File.CreateTempFile("test", "test") is prettymuch the while code :) – user1985273 Aug 08 '13 at 09:14
-
If uncertain, you can also attach the runtime sources (src.zip) to your IDE, start debugging and step into the `createTempFile()` method to see where it fails – Andreas Fester Aug 08 '13 at 09:16
3 Answers
Does anyone know from where java gets its default temp path
It is read from the java.io.tmpdir
property.
Files.createTempFile("test", "test");
essentially calls java.nio.file.TempFileHelper.createTempFile(null, prefix, suffix, attrs);
which again calls java.nio.file.TempFileHelper.create(dir, prefix, suffix, false, attrs);
. There, if dir
is null, it is set to tmpdir
which is declared as follows:
private static final Path tmpdir =
Paths.get(doPrivileged(new GetPropertyAction("java.io.tmpdir")));
You can set the property explicitly as shown in the answer from @Joni. If you do not set it explicitly, the JVM initializes it to a platform specific default value at startup - see also Environment variable to control java.io.tmpdir?
and how can it be not found?
If the property java.io.tmpdir
points to an invalid directory, the temporary file can not be created.

- 1
- 1

- 36,091
- 7
- 95
- 123
Independently of how the default value is obtained, you can set the temporary files directory by setting the system property java.io.tmpdir
when starting the JVM:
java -Djava.io.tmpdir=/path/to/where/ever/you/like YourClass
If you want to know where the default value comes from, you'll have to read the source code for your JVM. For example, OpenJDK on Windows calls the API function GetTempPathW
(search for the file java_props_md.c
in the JDK source code), which looks up the path in environment variables and registry in the following way:
The
GetTempPath
function checks for the existence of environment variables in the following order and uses the first path found:
- The path specified by the TMP environment variable.
- The path specified by the TEMP environment variable.
- The path specified by the USERPROFILE environment variable.
- The Windows directory.
Note that the function does not verify that the path exists, nor does it test to see if the current process has any kind of access rights to the path.

- 108,737
- 14
- 143
- 193
-
But if i change the TMP env variables then java.io.tmpdir still prints to pervious oodatuim? – user1985273 Aug 08 '13 at 11:25
-
Assuming "oodatuim" means directory: maybe your program is launched with a script that resets TMP, maybe you need to restart the system for the change to take effect, maybe your JVM doesn't use the GetTempPath function, it's impossible to know with the data provided. – Joni Aug 08 '13 at 11:40
Try:
String path = System.getProperty("java.io.tmpdir");
See: get property method
And to add it here for completeness sake, there's also the methods createTempFile(String prefix,String suffix) and createTempFile(String prefix, String suffix, File directory) methods from Java's file class.
Here is my code to fin path of temp file and find temp path:
public class GetTempFilePathExample
{
public static void main(String[] args)
{
try{
//create a temp file
File temp = File.createTempFile("temp-file-name", ".tmp");
System.out.println("Temp file : " + temp.getAbsolutePath());
//Get tempropary file path
String absolutePath = temp.getAbsolutePath();
String tempFilePath = absolutePath.
substring(0,absolutePath.lastIndexOf(File.separator));
System.out.println("Temp file path : " + tempFilePath);
}catch(IOException e){
e.printStackTrace();
}
}
}
Output of this code is :
Temp file : /tmp/temp-file-name3697762749201044262.tmp
Temp file path : /tmp

- 1,115
- 1
- 15
- 44