Skip to content

Instantly share code, notes, and snippets.

@joakime
Created August 4, 2021 15:19
Show Gist options
  • Save joakime/fd8454fd437405e92445adb1f81a438c to your computer and use it in GitHub Desktop.
Save joakime/fd8454fd437405e92445adb1f81a438c to your computer and use it in GitHub Desktop.
FindShortTempDir
package fs;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
public class FindShortTempDir
{
public static void main(String[] args)
{
Path tmpDir = findTempDir("jetty.unixsocket.path");
System.out.printf("tmpDir[jetty.unixsocket.path] = %s%n", tmpDir);
}
private static Path findTempDir(String systemPropertyName)
{
String systemPropPath = System.getProperty(systemPropertyName);
if (systemPropPath != null && !systemPropPath.isBlank())
{
Path tmp = Paths.get(systemPropPath);
if (Files.exists(tmp) && Files.isDirectory(tmp))
return tmp;
}
for (Path rootDir : FileSystems.getDefault().getRootDirectories())
{
for (String dirName : List.of("tmp", "var/tmp", "Temp"))
{
Path tmp = rootDir.resolve(dirName);
if (Files.exists(tmp) && Files.isDirectory(tmp))
return tmp;
}
}
return Paths.get(System.getProperty("java.io.tmpdir"));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment