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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With