6

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

Alfe
  • 56,346
  • 20
  • 107
  • 159
  • Wait - you *can't* change the code which places files into the temp directory? – Makoto May 25 '18 at 15:11
  • 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 May 25 '18 at 15:16
  • @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 May 25 '18 at 15:18
  • 1
    @Alfe: When you change your system property and execute `java -XshowSettings`, what value do you see for `java.io.tmpdir`? – Makoto May 25 '18 at 15:20
  • 1
    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](https://stackoverflow.com/help/mcve). – Andreas May 25 '18 at 15:36
  • I fear my question bogus, sorry guys :-( I tested this with a [mcve] and there setting the `java.io.tmpdir` works just fine. It rather seems to be a Jenkins issue with the Maven plugin or something. – Alfe May 25 '18 at 15:49

1 Answers1

1

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

davidxxx
  • 125,838
  • 23
  • 214
  • 215
  • Well, I passed via maven options: `-Djava.io.tmpdir=${WORKSPACE}/tmp` but that had no effect. I think that is because this `java.io.tmpdir` only is relevant in case _nothing_ is passed to `createTempDir()` but our code passes something. A bit weird, though. – Alfe May 25 '18 at 15:21
  • @Alfe Sorry, I bad read your question. I updated with `createTempDirectory()` and it still works. The parameter passed to the method should not have any effect on the temp directory as it is just the prefix of the directory to create. Maybe you should also check that `${WORKSPACE}` doesn't contain any space. You could also output the value of `java.io.tmpdir` before the code that creates the temp directory. – davidxxx May 25 '18 at 15:39
  • In the doubt, what is the maven command you ran ? If it is a compiling/packaging goal that you execute, the result is expected. You have to run that at runtime as the application is started. – davidxxx May 25 '18 at 15:42
  • I don't run the maven command myself, its a bloody Jenkins plugin doing that and it hides what it does, actually :( Sorry to have bothered you but that seems to be the culprit. The given property isn't passed properly to the unit tests. – Alfe May 25 '18 at 15:54