To answer my own question, based on help from #centos on irc.freenode.net, to select/set on the command line:

# alternatives --set java java-1.8.0-openjdk.x86_64

For Ansible, looking at the source of the alternatives module it requires a path to the java executable, such as /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.222.b10-0.el7_6.x86_64/jre/bin/java so this doesn't seem to be as easy/straight forward.

Answer from gratz on Stack Exchange
🌐
Red Hat
access.redhat.com › solutions › 6232511
How to set default Java version with alternatives tool in RHEL on a per user basis - Red Hat Customer Portal
1 week ago - Selection Command ----------------------------------------------- *+ 1 java-1.8.0-openjdk.x86_64 (/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.292.b10-1.el8_4.x86_64/jre/bin/java) 2 java-11-openjdk.x86_64 (/usr/lib/jvm/java-11-openjdk-11.0.11.0.9-2.el8_4.x86_64/bin/java) Enter to keep the current selection[+], or type selection number: 2 failed to create /var/lib/alternatives/java.new: Permission denied · Using the alternatives tool allows you to set the system default version as root but does not currently provide options to set different defaults for individual users.
🌐
MTU Service Desk
servicedesk.mtu.edu › TDClient › 1801 › Portal › KB › ArticleDet
Article - Changing Java versions (Linux)
These scripts are located at the following location: `/opt/java-selection/` In order to activate a Java version, run the script corresponding to the version you would like to work with, then reload or resource your terminal: For example the ...
🌐
MTU Service Desk
servicedesk.mtu.edu › TDClient › KB › ArticleDet
Changing Java versions (Linux) - Service Desk
These scripts are located at the following location: `/opt/java-selection/` In order to activate a Java version, run the script corresponding to the version you would like to work with, then reload or resource your terminal: For example the ...
🌐
Red Hat
developers.redhat.com › articles › 2023 › 01 › 09 › how-run-correct-java-version-after-update
How to run the correct Java version after an update | Red Hat Developer
January 9, 2023 - Figure 3 highlights the JDK path in the JAVA_HOME setting in my current .bashrc file. Figure 3: The .bashrc file expects an old JDK, openjdk-17.0.4.1.1–1.fc35.x86_64. The paths are different, so I need to update the environment variable in the .bashrc file to match the path offered by the system. Specifically, I need to change the JDK version in the path from openjdk-17.0.4.1.1–1.fc35.x86_64 to openjdk-17.0.5.0.8–2.fc35.x86_64.
🌐
Baeldung
baeldung.com › home › installation › switch between multiple java versions
Switch Between Multiple Java Versions | Baeldung on Linux
March 18, 2024 - Afterwards, we’ll enlist the versions again to verify that the JDK has changed successfully: $ update-alternatives --config java There are 3 choices for the alternative java (providing /usr/bin/java).
Find elsewhere
🌐
LinuxQuestions.org
linuxquestions.org › questions › red-hat-31 › how-to-change-the-jdk-version-in-the-redhat-4175630780
How to change the JDK version in the redhat
May 29, 2018 - I have installed redhat 6.6 server version.It default comes with openjdk. I have install java 1.8. So how to update the JAVA_HOME to 1.8 ? I tried
🌐
Red Hat
docs.redhat.com › en › documentation › red_hat_build_of_openjdk › 8 › html › installing_and_using_red_hat_build_of_openjdk_8_for_rhel › updating-openjdk11-on-rhel8_openjdk
Chapter 4. Updating Red Hat build of OpenJDK 8 on RHEL | Installing and using Red Hat build of OpenJDK 8 for RHEL | Red Hat build of OpenJDK | 8 | Red Hat Documentation
May 10, 2024 - If the output from the previous command shows that you have a different major version of Red Hat build of OpenJDK checked out on your system, you can enter the following command in your CLI to switch your system to use Red Hat build of OpenJDK 8: ... You can update Red Hat build of OpenJDK using the archive. This is useful if the Red Hat build of OpenJDK administrator does not have root privileges. ... Know the generic path pointing to your JDK or JRE installation. For example, ~/jdks/java-8
🌐
Linux Hint
linuxhint.com › change-java-version-linux
How to Change Java Version Linux – Linux Hint
To change the Java version in Linux, use the –config java option with the update-alternatives command:
🌐
The Geek Diary
thegeekdiary.com › how-to-work-with-multiple-java-versions-under-linux
How to work with multiple java versions under Linux
GitLab CI/CD is a tool used for automating the process of testing, building, and deploying code. It helps developers integrate and deliver software quickly and reliably. Here’s a breakdown of the p · © 2025 Sandeep. Some rights reserved
🌐
DEV Community
dev.to › thegroo › install-and-manage-multiple-java-versions-on-linux-using-alternatives-5e93
Install and manage multiple Java versions on Linux using alternatives - DEV Community
February 10, 2022 - Type in the Reference number for the one you want to be used from the displayed list and check with java -version the same can be done for, javac, jshell, jar or any other tools you want to manage multiple versions using alternatives on linux.
🌐
Xmodulo
xmodulo.com › change-default-java-version-linux.html
How to change default Java version on Linux
First, check available Java versions on your Linux system by using update-alternatives command: ... In this example, there are four different Java versions that are installed: OpenJDK JRE 1.6, Oracle Java JRE 1.6, OpenJDK JRE 1.7 and Oracle Java JRE 1.7. The default Java version is currently set to OpenJDK JRE 1.6. If the necessary Java JRE is not installed, you can always install it using these instructions. Now that there are suitable candidates to change to, you can switch the default Java version among available Java JREs by running the following command:
🌐
Red Hat
docs.redhat.com › en › documentation › red_hat_build_of_openjdk › 8 › html-single › configuring_red_hat_build_of_openjdk_8_for_rhel › index
Configuring Red Hat build of OpenJDK 8 for RHEL | Red Hat build of OpenJDK | 8 | Red Hat Documentation
To specify the Red Hat build of OpenJDK version to use permanently for all users, add these commands into /etc/bashrc: export JAVA_HOME=/opt/jdk/jdk-1.8.0 export PATH="$JAVA_HOME/bin:$PATH" ... If you do not want to redefine JAVA_HOME, add only the PATH command to bashrc, specifying the path ...
🌐
Red Hat
developers.redhat.com › blog › 2018 › 11 › 05 › migrating-from-oracle-jdk-to-openjdk-on-red-hat-enterprise-linux-what-you-need-to-know
Migrating from Oracle JDK to OpenJDK on Red Hat Enterprise Linux: What you need to know | Red Hat Developer
July 31, 2023 - E.g: $ export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.151-5.b12.el7_4.x86_64 $ export PATH=$JAVA_HOME/bin:$PATH # Validate via: $ java -version openjdk version "1.8.0_151" $ javac -version javac 1.8.0_151 · For details, please see How to install multiple minor versions of OpenJDK on a Red Hat Enterprise Linux system?
🌐
Sticsrpacks
sticsrpacks.github.io › SticsOnR › articles › Changing_java_version_linux.html
Changing Java version procedure under linux • SticsOnR
After the Java 17 installation, and if it is not the default version known by the system, switching to this version can be done with a script like the following one. It can be ran either from R or a shell terminal. #!/bin/bash # Changing latest installed Java version to Java version 11 echo "The current Java version is : " java -version version=$(java -version 2>&1 | awk -F '"' '/version/ {print $2}') if !
🌐
Coderanch
coderanch.com › t › 694738 › os › update-JAVA-HOME-RedHat
How to update the JAVA_HOME in RedHat? (GNU/Linux forum at Coderanch)
And note that unless the environment is shared, that setting will NOT change the JAVA_HOME values for any other shell process. That, in fact is why the "export" statement exists. So that you can set environment variables in one shell process and have them be inherited by child shells that it spawns (for example, to run a shell script). When you install Java on a Red Hat/CentOS/Fedora or similar system, Oracle provides a RedHat Package Manager (RPM) file.