We run code which does the standard for creating a temp directory:
import java.nio.file.Files;
And then:
tmp = Files.createTempDirectory("ourprefix-");
This, effectively, creates the directories under /tmp/
so that we get things like /tmp/ourprefix-1234
or similar.
Unfortunately, this base directory /tmp/
seems to be fixed and since on our build server lots of things tend to put their temp stuff there and because the partition the /tmp/ is on is rather small, this is a problem.
Is there a way to configure this facility from the outside (i. e. without changing the code)? I would have guessed that /tmp/
is a default and can be overridden by setting a special environment variable or (more Javaish) passing a special property to the compiler (e. g. -Djava.tmp.root=/path/to/my/larger/partition/tmp
).
I tried using java.io.tmpdir
but setting this did not have any effect; it seems to be the default in case nothing is given to createTempDirectory()
but in our case the code passes a prefix.
Any idea how to achieve what I want without changing the source code?
EDIT
After some investigation I found that this works just fine:
import java.nio.file.Path;
import java.nio.file.Files;
import java.io.IOException;
public class TestTempDir {
public static void main(String[] args) throws IOException {
System.out.println(System.getProperty("java.io.tmpdir"));
Path path = Files.createTempDirectory("myprefix-");
System.out.println(path.toFile().getAbsolutePath());
}
}
Compile with javac TestTempDir.java
, prepare with mkdir tmp
and run with java -Djava.io.tmpdir=
pwd/tmp TestTempDir
this just works as expected:
/my/work/path/tmp
/my/work/path/tmp/myprefix-1525078348397347983
My issue rather seems to be one with Jenkins and its Maven plugin which does not pass the set properties along to the test cases :-/
if you pass the java.io.tmpdir
property as a custom JVM property as you run the JVM, it should work.
Something like that :
java -Djava.io.tmpdir=myPath myClass
I tested and it works :
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
public class TestTempDir {
public static void main(String[] args) throws IOException {
System.out.println(System.getProperty("java.io.tmpdir"));
Path dir = Files.createTempDirectory("helloDir");
System.out.println(dir.toString());
}
}
$ java -Djava.io.tmpdir=D:\temp TestTempDir
D:\temp
D:\temp\helloDir5660384505531934395
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