5
votes

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 :-/

1
Wait - you can't change the code which places files into the temp directory?Makoto
From what I can tell, it takes the box's default temp directory. You'd probably have to configure the OS' temp directory location. Alternatively, use the other createTempDirectory method which takes in the root directory location, but that requires a source code change.Compass
@Makoto Right I can't because I'm just in control of the build server (Jenkins). The source code is under the control of another team. They are happy with using /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.Alfe
@Alfe: When you change your system property and execute java -XshowSettings, what value do you see for java.io.tmpdir?Makoto
Did you set java.io.tmpdir using the -D command-line option? Because that is how you're supposed to do it, and createTempDirectory() will use that. Please show Minimal, Complete, and Verifiable example.Andreas

1 Answers

1
votes

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