How to configure root for temp dirs in Java
Asked Answered
C

1

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

Carbrey answered 25/5, 2018 at 15:6 Comment(6)
Wait - you can't change the code which places files into the temp directory?Cusick
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.Straighten
@Cusick 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.Carbrey
@Alfe: When you change your system property and execute java -XshowSettings, what value do you see for java.io.tmpdir?Cusick
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.Energid
I fear my question bogus, sorry guys :-( I tested this with a minimal reproducible example and there setting the java.io.tmpdir works just fine. It rather seems to be a Jenkins issue with the Maven plugin or something.Carbrey
P
2

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

Pippa answered 25/5, 2018 at 15:17 Comment(4)
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.Carbrey
@Carbrey 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.Pippa
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.Pippa
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.Carbrey

© 2022 - 2024 — McMap. All rights reserved.