This command leverages the update-alternatives --config functionality to present an interactive selection menu for each Java binary (java, javac, and jar).
The system displays all available java alternatives:
Showing the optionsThere are 3 choices for the alternative java (providing /usr/bin/java). Selection Path Priority Status------------------------------------------------------------* 0 /opt/java/jdk-21/bin/java 2100 auto mode 1 /opt/java/jdk-11/bin/java 1100 manual mode 2 /opt/java/jdk-17/bin/java 1700 manual mode 3 /opt/java/jdk-21/bin/java 2100 manual modePress <enter> to keep the current choice[*], or type selection number:
Enter the number corresponding to your desired Java version (e.g., 2 for Java 17).
3
Configure javac compiler
After selecting the runtime, you’ll see the javac alternatives:
There are 3 choices for the alternative javac (providing /usr/bin/javac). Selection Path Priority Status------------------------------------------------------------* 0 /opt/java/jdk-21/bin/javac 2100 auto mode 1 /opt/java/jdk-11/bin/javac 1100 manual mode 2 /opt/java/jdk-17/bin/javac 1700 manual mode 3 /opt/java/jdk-21/bin/javac 2100 manual modePress <enter> to keep the current choice[*], or type selection number:
Select the matching javac version.
4
Configure jar tool
Finally, configure the jar binary:
There are 3 choices for the alternative jar (providing /usr/bin/jar). Selection Path Priority Status------------------------------------------------------------* 0 /opt/java/jdk-21/bin/jar 2100 auto mode 1 /opt/java/jdk-11/bin/jar 1100 manual mode 2 /opt/java/jdk-17/bin/jar 1700 manual mode 3 /opt/java/jdk-21/bin/jar 2100 manual modePress <enter> to keep the current choice[*], or type selection number:
Select the matching jar version.
5
Verification
After all three selections, verify your Java version:
$ java -versionjava version "17.0.9" 2023-10-17 LTSJava(TM) SE Runtime Environment (build 17.0.9+11-LTS-201)Java HotSpot(TM) 64-Bit Server VM (build 17.0.9+11-LTS-201, mixed mode, sharing)
$ javaoptionscli# Select "Changes Version" from menuShowing the optionsThere are 2 choices for the alternative java (providing /usr/bin/java). Selection Path Priority Status------------------------------------------------------------* 0 /opt/java/jdk-21/bin/java 2100 auto mode 1 /opt/java/jdk-17/bin/java 1700 manual modePress <enter> to keep the current choice[*], or type selection number: 1There are 2 choices for the alternative javac (providing /usr/bin/javac). Selection Path Priority Status------------------------------------------------------------* 0 /opt/java/jdk-21/bin/javac 2100 auto mode 1 /opt/java/jdk-17/bin/javac 1700 manual modePress <enter> to keep the current choice[*], or type selection number: 1There are 2 choices for the alternative jar (providing /usr/bin/jar). Selection Path Priority Status------------------------------------------------------------* 0 /opt/java/jdk-21/bin/jar 2100 auto mode 1 /opt/java/jdk-17/bin/jar 1700 manual modePress <enter> to keep the current choice[*], or type selection number: 1# Returns to main menu
When working on a project that requires Java 11, switch to that version before building:
# Change to Java 11$ javaoptionscli# Select "Changes Version" and choose Java 11# Verify$ java -version# Build your project$ mvn clean install
Testing across multiple Java versions
Quickly test your application against different Java versions:
# Test with Java 17$ javaoptionscli # Select Changes Version -> Java 17$ ./run-tests.sh# Test with Java 21$ javaoptionscli # Select Changes Version -> Java 21$ ./run-tests.sh
Matching production environment
Before deploying, ensure you’re testing with the same Java version as production:
# Production uses Java 17$ javaoptionscli # Select Changes Version -> Java 17$ mvn clean package$ java -jar target/myapp.jar
Always select the same version for all three binaries (java, javac, jar). Mixing versions can lead to unexpected behavior and compilation issues.
Changing Java version only affects the system-wide default. Applications using JAVA_HOME environment variable may still use a different version. Update JAVA_HOME if needed: