Switching Between Java Versions Without the Hassle
Why I ditched SDKMAN for a simple bash script when juggling Java 8 and Java 17.
Working with different projects means dealing with different Java versions. Some of my Hadoop stuff needs Java 8. Metabase plugins want Java 17. I’d start a Spark job and get errors because I forgot to switch JDKs. It happened enough times that I needed a better way.
The Hadoop stack I work with still runs on Java 8. A lot of the libraries expect it. Metabase, on the other hand, uses newer Java features and works better with Java 17. So I keep both installed and switch between them constantly.
At first, I tried the usual route: update-alternatives to flip between versions. It worked, but I’d forget which version was active and waste time debugging compatibility issues that turned out to be just the wrong $JAVA_HOME.
Looking for Something Better
I gave SDKMAN a shot. It’s popular, installs multiple JDKs, and lets you switch with sdk use java x.y.z. Seemed perfect.
But I ran into problems:
- My shell took longer to start because of the initialization.
- It occasionally messed with my environment variables.
- Setting it up on remote servers was more steps than I wanted to remember.
I looked at jEnv and other version managers. They all felt like extra layers. I wanted something that just worked without adding complexity.
The Fix
I wrote a small bash script:
#!/usr/bin/env bash
# switch-java.sh: swap $JAVA_HOME and PATH quickly
case "$1" in
8)
export JAVA_HOME="/usr/lib/jvm/java-8-openjdk"
;;
17)
export JAVA_HOME="/usr/lib/jvm/java-17-openjdk"
;;
*)
echo "Usage: switch-java.sh [8|17]"
exit 1
;;
esac
export PATH="$JAVA_HOME/bin:$(echo $PATH | sed -e 's#/usr/lib/jvm/java-[0-9]*-openjdk/bin:##g')"
java -version
I made it executable with chmod +x switch-java.sh and put it in my PATH. Now I just run:
switch-java.sh 17
That’s it. I’m on Java 17 for Metabase. Run switch-java.sh 8 and I’m back to Hadoop. The script prints the version so I always know what’s active.
Enjoyed this article?
Share it with your network or let me know your thoughts!
Ahmed MAKROUM
Data Engineer & Full-Stack Developer
Made it to the end? Thanks for reading! If you enjoyed it, share it or say hi. I read every message.