The system cannot find the path while createTempFile

I get an exception saying: "the system cannot find the specified path" when calling the createTempFile("test", "test") java function createTempFile("test", "test") . I tried to walk, but no luck. Does anyone know where java gets its default temporary path from and how not to find it? Windows variables seem correct, and changing them does not affect java.

+6
source share
3 answers

Does anyone know where java gets its default temporary path from

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 install it explicitly, the JVM initializes it to the default value for the platform at startup - see also the environment variable for managing java.io.tmpdir?

and how not to find him?

If the java.io.tmpdir property points to an invalid directory, a temporary file cannot be created.

+14
source

Regardless of how the default is obtained, you can set the temporary files directory by setting the java.io.tmpdir system property when starting the JVM:

 java -Djava.io.tmpdir=/path/to/where/ever/you/like YourClass 

If you want to know where the default came from, you will need to read the source code for your JVM. For example, OpenJDK on Windows calls the GetTempPathW API GetTempPathW (search for the java_props_md.c file in the JDK source code), which looks up the path in the environment and registry variables as follows:

The GetTempPath function checks for the presence of environment variables in the following order and uses the found first path:

  • The path specified by the TMP environment variable.
  • The path specified by the TEMP environment variable.
  • The path specified by the USERPROFILE environment variable.
  • Windows directory.

Note that the function does not check if the path exists, and does not check if the current process has any access rights to the path.

+7
source

Try:

 String path = System.getProperty("java.io.tmpdir"); 

See: get property method

And to add it here for completeness, there are also the createTempFile (String prefix, string suffix) and createTempFile (String prefix, string suffix, file directory) methods from the Java file methods.

Here is my code to go to the temp file and find the temporary 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(); } } } 

The output of this code is:

 Temp file : /tmp/temp-file-name3697762749201044262.tmp Temp file path : /tmp 
+3
source

Source: https://habr.com/ru/post/951286/


All Articles