How to Switch Between Java Versions on Linux Without Breaking Things
Sometimes, when working with Java payloads, it might be necessary to switch to a different Java version or use specific version in order to run a program. Recently I needed to switch from OpenJDK version 17 to 11 in order to get a ysoserial payload to generate. One of the suggestions available when researching this was to use alternatives to switch versions, but this seems like a relatively permanent solution to a temporary problem.
Java versions installed on Debian operating systems are stored in /usr/lib/jvm
.
In there are nested bin
directories containing java.
By setting the PATH
environment variable and including java’s directory first, the correct java command will be used.
Java also requires a home directory set in the JAVA_HOME
directory for java JDK reasons.
- switching to Java 11 on Kali
export PATH=/usr/lib/jvm/java-11-openjdk-arm64/bin:$PATH
export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-arm64
Developing a Version Selector Script
Since I like automating everything, I decided to come up with a Java version selector script in order to do this on the spot when I need to.
Since the script is setting environment variables, I need to actually source it.
The easiest way to do this is to define a bash alias in my bashrc
, which is available on github in my environment scripts.
alias java-selector='source $ENVIRON_BASEPATH/zet/20240107171624/java-selector'
I am using the fzf program to create an interactive menu to and return the path to the selected java binary. A simple awk script to cut the directory path on slashes and remove the last one helps find the relevant paths.
#!/bin/bash
javapath=$(find /usr/lib/jvm -name java | fzf | awk 'BEGIN {OFS=FS="/"} {NF--; print}')
javadir=$(echo "$javapath" | awk 'BEGIN {OFS=FS="/"} {NF--; print}')
export PATH=$javapath:$PATH
export JAVA_HOME=$javadir
- example of switching java versions
┌──(parallels㉿kali-gnu-linux-2023)-[~/environment/zet/20240107171624]
└─$ java-selector
┌──(parallels㉿kali-gnu-linux-2023)-[~/environment/zet/20240107171624]
└─$ java --version
Picked up _JAVA_OPTIONS: -Dawt.useSystemAAFontSettings=on -Dswing.aatext=true
openjdk 11.0.20-ea 2023-07-18
OpenJDK Runtime Environment (build 11.0.20-ea+7-post-Debian-1)
OpenJDK 64-Bit Server VM (build 11.0.20-ea+7-post-Debian-1, mixed mode)
┌──(parallels㉿kali-gnu-linux-2023)-[~/environment/zet/20240107171624]
└─$ java-selector
┌──(parallels㉿kali-gnu-linux-2023)-[~/environment/zet/20240107171624]
└─$ java --version
Picked up _JAVA_OPTIONS: -Dawt.useSystemAAFontSettings=on -Dswing.aatext=true
openjdk 17.0.9 2023-10-17
OpenJDK Runtime Environment (build 17.0.9+9-Debian-2)
OpenJDK 64-Bit Server VM (build 17.0.9+9-Debian-2, mixed mode, sharing)