Darshan Ub
4 min readNov 15, 2024

If you have multiple JDK versions installed and want to set a specific version as the default, you can use the update-alternatives command to configure it. Here’s how:

1. List All Installed Java Versions

Run the following command to see a list of installed Java versions managed by update-alternatives:

sudo update-alternatives --config java

This will output something like:

There are 3 choices for the alternative java (providing /usr/bin/java).
Selection Path Priority Status
------------------------------------------------------------
* 0 /usr/lib/jvm/java-11-openjdk-amd64/bin/java 1111 auto mode
1 /usr/lib/jvm/java-8-openjdk-amd64/bin/java 1081 manual mode
2 /usr/lib/jvm/java-11-openjdk-amd64/bin/java 1111 manual mode
3 /usr/lib/jvm/java-17-openjdk-amd64/bin/java 1112 manual mode

Press <enter> to keep the current choice[*], or type selection number:

2. Select a Specific Java Version

Type the selection number for the JDK version you want to set as the default, then press Enter. For example, if you want to use JDK 17, you would type 3 in this example.

3. Set JAVA_HOME for the Selected JDK Version

Once you’ve chosen the JDK version with update-alternatives, you can set JAVA_HOME in your ~/.bashrc file to match that version:

  1. Edit the ~/.bashrc file:
nano ~/.bashrc

2. Add or update the JAVA_HOME line with the path for the selected JDK version. For example:

export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64

3. Update the PATH to include the selected JDK’s bin directory:

export PATH=$JAVA_HOME/bin:$PATH

4. Save and close the file, then reload it:

source ~/.bashrc

4. Verify the Changes

To confirm the JDK version and the JAVA_HOME setting:

java -version
echo $JAVA_HOME

This will ensure your terminal sessions use the specified JDK version as the default.

Setting the JAVA_HOME Environment Variable (Optional)

Some applications may require the JAVA_HOME environment variable. You can set it by adding this to your ~/.bashrc file:

echo "export JAVA_HOME=$(dirname $(dirname $(readlink -f $(which javac))))" >> ~/.bashrc
echo "export PATH=$JAVA_HOME/bin:$PATH" >> ~/.bashrc
source ~/.bashrc

Now, you have the JDK installed and ready to use on Ubuntu!

To uninstall the JDK on Ubuntu, you can follow these steps:

1. List Installed JDK Versions

To check which JDK versions are installed, run:

sudo update-java-alternatives --list

2. Uninstall the JDK

If you installed the default JDK or a specific version using apt, you can uninstall it with the following commands:

  • Uninstall the Default JDK:
sudo apt remove default-jdk -y
  • Uninstall a Specific JDK Version (e.g., OpenJDK 11 or OpenJDK 17):

For OpenJDK 11:

sudo apt remove openjdk-11-jdk -y
  • For OpenJDK 17:
sudo apt remove openjdk-17-jdk -y

3. Remove Residual Configuration Files (Optional)

After removing the JDK, you can clean up any leftover configuration files:

sudo apt autoremove --purge -y

4. Verify the Uninstallation

To confirm that the JDK has been removed, check the version:

java -version

If Java is uninstalled, you should see an error like command not found.

2 DEC 24
— — — — — — — — —

To make the `JAVA_HOME` and `PATH` dynamic so they adapt based on the current Java version you have configured, you can use the `update-alternatives` system. Here’s how to set it dynamically:

— -

### **Dynamic JAVA_HOME and PATH Setup**

1. **Modify `.bashrc` or `.zshrc`**:
Open your shell configuration file:
```bash
nano ~/.bashrc
```
Or, if you’re using `zsh`:
```bash
nano ~/.zshrc
```

2. **Add Dynamic Configuration**:
Add the following block to set `JAVA_HOME` dynamically:
```bash
# Dynamically set JAVA_HOME and update PATH
export JAVA_HOME=$(dirname $(dirname $(readlink -f $(which java))))
export PATH=$JAVA_HOME/bin:$PATH
```

3. **Save and Reload**:
Save the file (`Ctrl + O`, `Enter`, `Ctrl + X`), then reload the configuration:
```bash
source ~/.bashrc
```
Or for `zsh`:
```bash
source ~/.zshrc
```

— -

### **How It Works**:
- The `which java` command finds the current active `java` executable.
- `readlink -f` resolves any symbolic links to get the real path.
- `dirname` is used twice to strip `/bin/java` and get the base directory.

— -

### **Verify the Setup**:
1. Check if `JAVA_HOME` is set dynamically:
```bash
echo $JAVA_HOME
```
— It should now reflect the directory of the currently configured Java version.

2. Verify the `java` version:
```bash
java — version
```
— It should match the version provided by `update-alternatives`.

3. Run:
```bash
echo $PATH | grep java
```
— Ensure it includes the updated `JAVA_HOME/bin`.

— -

This setup ensures your `JAVA_HOME` and `PATH` will dynamically adapt whenever you switch Java versions using `update-alternatives`. Let me know if you need further assistance!

— — — — — -

Yes, the code you provided will dynamically adjust the JAVA_HOME and PATH variables whenever the Java version is changed using update-alternatives. Here's why:

How It Works

  1. which java:
  • Finds the location of the active Java executable, which changes when you use update-alternatives to switch Java versions.

2. readlink -f:

  • Resolves any symbolic links to determine the actual path of the Java binary.

3. dirname (used twice):

  • Strips the /bin/java part from the path to set JAVA_HOME to the base directory of the current Java version.

4. Dynamic Behavior:

  • When you switch the Java version using update-alternatives --config java, the which java command will point to the new version, and the script will automatically adjust JAVA_HOME and PATH to reflect the change.

How to Test It

  1. Switch Java Version: Use update-alternatives to change the Java version:
sudo update-alternatives --config java

2. Reload .bashrc: If you switch Java versions, reload your .bashrc:

source ~/.bashrc

3. Verify the Change:

  • Check JAVA_HOME:
echo $JAVA_HOME
  • Verify the java version:
java --version

Both should reflect the newly selected Java version.

Benefits of This Approach

  • No Manual Edits: You don't need to manually edit JAVA_HOME or PATH in your .bashrc each time you change Java versions.
  • Consistent Environment: Your environment will always match the version selected by update-alternatives.

Let me know if you encounter any issues!

Darshan Ub
Darshan Ub

Written by Darshan Ub

0 Followers

Web Developer

No responses yet