HenrikBengtsson / CBI-software

A Scientific Software Stack for HPC (CentOS oriented)
https://wynton.ucsf.edu/hpc/software/software-repositories.html
5 stars 2 forks source link

Java: Make Java respect TMPDIR #39

Open HenrikBengtsson opened 2 years ago

HenrikBengtsson commented 2 years ago

Issue

By default, Java does not respect TMPDIR, it's hardcoded to use /tmp, cf. https://github.com/openjdk/jdk/blob/1a7b70a8be0a236b98925a8320d25d88a405d595/src/java.base/share/classes/java/io/File.java#L2106-L2107

Reproducible example:

$ export TMPDIR=/scratch
$ java tmpdir
java.io.tmpdir='/tmp'

were ./tmpdir.class was compiled as:

$ javac tmpdir.java
$ cat tmpdir.java
class tmpdir {
  public static void main(String args[]){
    System.out.println("java.io.tmpdir='" + System.getProperty("java.io.tmpdir") + "'");
  }
}

Solution

Set:

_JAVA_OPTIONS="-Djava.io.tmpdir='${TMPDIR:-/tmp}' ${_JAVA_OPTIONS}"
export _JAVA_OPTIONS

Then, we get:

$ java tmpdir
Picked up _JAVA_OPTIONS: -Djava.io.tmpdir='/wynton/home/cbi/hb' 
java.io.tmpdir='/wynton/home/cbi/hb'

Suggestion

Set _JAVA_OPTIONS globally, e.g. in the built-in openjdk Lmod modules as:

-- Make Java respect TMPDIR, if set
local tmpdir=os.getenv("TMPDIR") or ""
if tmpdir ~= "" then
  if not isDir(tmpdir) then
    LmodError("Environment variable TMPDIR specifies a non-existing folder: " .. tmpdir)
  end

  -- Set -Djava.io.tmpdir=..., if not already set
  local opts=os.getenv("JAVA_OPTIONS")
  if opts ~= nil and string.find(opts, "-Djava[.]io[.]tmpdir=") then
    opts = nil
  end
  if opts ~= nil then
    prepend_path("JAVA_OPTIONS", "-Djava.io.tmpdir=" .. tmpdir)
  end
end
HenrikBengtsson commented 2 years ago

Hmm... pushing and popping of _JAVA_OPTIONS in Lmod was more complicated than I though, or I'm doing it all wrong. For some reason, it's not sufficient with pushenv() - maybe because of all the conditional checks. To simplify this, I've added a standalone hidden java-tweaks module to deal with this that other modules can depend on;

https://github.com/HenrikBengtsson/CBI-software/blob/master/CBI/java-tweaks/module.lua.tmpl