Goals:
- never upgrade Java with the GUI pkg installer again.
- never try to remember that
/Library/Java
is where JDKs go. - Just keep JDK8 and JDK9 up-to-date. Java is so stable, maybe these two major versions is all we need?
Note the assumptions in these goals. Making this a bit more generic would be nice.
I'm on a mac and I use the fish shell which I know is squarely in the edge case camp but I hope this is inspiring or useful to someone else because there's not much shell specific stuff here. I found jenv
which is going to help.
First, upgrading java with homebrew is pretty easy. The assumption is that java8 is stable and java (9) is still emerging. This assumption would likely change so it'd be nice to have this scripted out of this assumption. For now, I'm just trying to avoid graphical Oracle installers.
brew upgrade java # jdk9
brew upgrade java8 # jdk8
Ok. That's easy. But then we have to switch around JAVA_HOMEs and all that. Bleh.
That's what jenv does. jenv versions
gives us a list like this
system
* 1.8 (set by ~/.java-version)
1.8.0.162
9.0
9.0.4
oracle64-1.8.0.162
oracle64-9.0.4
I tried looping through this text output in the shell but it's sort of problematic (maybe newlines?). I don't know. Brute force hack incoming!
~/.jenv/versions
is just a directory containing the definitions. And jenv has a refresh-versions command which I think is for situations like this where we've changed the filesystem underneath it.
rm -rf ~/.jenv/versions/*
jenv refresh-versions
# fish shell loop, very similar to bash/zsh
for i in (ls /Library/Java/JavaVirtualMachines/); jenv add "/Library/Java/JavaVirtualMachines/$i/Contents/Home"; end
oracle64-9.0.4 added
9.0.4 added
9.0 added
oracle64-1.8.0.162 added
1.8.0.162 added
1.8 added
Seems to work.
jenv global 1.8; java -version
java version "1.8.0_162"
Java(TM) SE Runtime Environment (build 1.8.0_162-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.162-b12, mixed mode)
jenv global 9.0; java -version
java version "9.0.4"
Java(TM) SE Runtime Environment (build 9.0.4+11)
Java HotSpot(TM) 64-Bit Server VM (build 9.0.4+11, mixed mode)
You can set a global version with jenv global
and project specific versions with jenv local
. But be aware, it creates a dot file named .java_version
in your current directory. I'm just going to throw all this into a script and then done.