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 :-/
/tmp/
in their production environment. I'd like to know if there is a way to influence where the tmp stuff goes in the build server, that's it. – Carbreyjava -XshowSettings
, what value do you see forjava.io.tmpdir
? – Cusickjava.io.tmpdir
using the-D
command-line option? Because that is how you're supposed to do it, andcreateTempDirectory()
will use that. Please show Minimal, Complete, and Verifiable example. – Energidjava.io.tmpdir
works just fine. It rather seems to be a Jenkins issue with the Maven plugin or something. – Carbrey