I'm sure there are a few other people who are developing Java on a Mac, running commands like mvn from a terminal (rather than an IDE), and who love direnv.
For such a person, it would be convenient to be able to specify the version as follows with .envrc
(which can be edited with direnv edit .
), isn't it?
use java 8
Define the following function in ~ / .direnvrc
.
use_java() {
if [ "$#" -ne 1 ]; then
echo "usage: use java VERSION" >&2
return 1
fi
local v
v="$1"
if [ "$v" -le "8" ]; then
v="1.$v"
fi
export JAVA_HOME="$(/usr/libexec/java_home -v "$v")"
}
--direnv can call the ʻuse_xxx function in the form of ʻuse xxx
--Define the environment variable $ JAVA_HOME
to switch Java versions
--On Mac, refer to the Home directory of each Java installed with / usr / libexec / java_home
It just implements that.
Recommended Posts