Configs

Java Java

Check existing Java installation

On some Linux distros, Java comes installed by default; however it is just the JRE and not the JDK. You can verify this as java command is found but the javac command is NOT found.

java  # would show manual of arguments
javac # would give not found error if JDK not installed
java --version  # OR "java -version" for older java
javac --version

Refer these articles to know the differences between JDK, JRE and JVM:

In Fedora, the JREs/JDKs are stored within /usr/lib/jvm. So you can look into it’s contents or query them as:

find /usr/lib/jvm -name java
find /usr/lib/jvm -name javac

Installing an OpenJDK

I’m on Fedora 40 where the package manager is dnf. You would be installing your desired OpenJDK via your respective Linux distro’s package manager (like apt for Ubuntu/Debian):

Installing an Oracle JDK

Installing JDKs via IntelliJ

You can also install JDKs from within IntelliJ itself:

IntelliJ auto-detects your available JDK locations on your system. You can also add your existing JDK folder locations like /usr/lib/jvm/jdk-11-oracle-x64 under configured JDKs in Project Structure


Working with multiple Java installations

The update-alternatives command in Linux (also called just alternatives in Fedora) creates, removes, maintains and displays information about the symbolic links comprising the alternatives system.

It is possible for several programs fulfilling the same or similar functions to be installed on a single system at the same time. A generic name in the filesystem is shared by all files providing interchangeable functionality. The alternatives system helps determine which actual file is referenced by this generic name.

Useful references:

When you install OpenJDK via your package manager or Oracle-JDK from the downloaded file, the alternatives should automatically get updated during that process

View available options for a command

update-alternatives --display java

The output would look like:

java - status is manual.
 link currently points to /usr/lib/jvm/java-21-openjdk-21.0.4.0.7-2.fc40.x86_64/bin/java
/usr/lib/jvm/java-21-openjdk-21.0.4.0.7-2.fc40.x86_64/bin/java - family java-21-openjdk.x86_64 priority 21000407
 ... follower links ...
/usr/lib/jvm/jdk-11.0.24-oracle-x64/bin/java - priority 184745984
 ... follower links ...
/usr/lib/jvm/java-22-openjdk-22.0.2.0.9-1.rolling.fc40.x86_64-fastdebug/bin/java - family java-latest-openjdk.x86_64 priority 1
 ... follower links ...
Current `best' version is /usr/lib/jvm/jdk-11.0.24-oracle-x64/bin/java.

Similarly, see alternative options list for javac

Adding an alternatives entry for a command

If any alternative for your commands is NOT registered in the list, you can manually add them as:

sudo update-alternatives --install <link> <name> <path> <priority>

For example, I downloaded the JetBrains Runtime (JCEF) JDK from within IntelliJ, which was downloaded at ~/.jdks/jbrsdk_jcef-17.0.12/ folder; but it was not showing up in the alternatives list. So, I’ll add the alternatives for java and javac as:

sudo update-alternatives --install /usr/bin/java java ~/.jdks/jbrsdk_jcef-17.0.12/bin/java 138
sudo update-alternatives --install /usr/bin/javac javac ~/.jdks/jbrsdk_jcef-17.0.12/bin/javac 138

Similarly, add alternatives for more commands like jar, javadoc etc. as required

You can also add follower links as:

--install link name path priority [--follower link name path]... [--initscript service] [--family name]

Switching between available alternatives of a command

Pick between Java installations present

sudo update-alternatives --config java

The output would look like below. Note that * denotes best available version and + denotes your current selection:

There are 4 programs which provide 'java'.

  Selection    Command
-----------------------------------------------
 + 1           java-latest-openjdk.x86_64 (/usr/lib/jvm/java-23-openjdk-23.0.0.0.37-1.rolling.fc40.x86_64-fastdebug/bin/java)
*  2           /usr/lib/jvm/jdk-11.0.24-oracle-x64/bin/java
   3           java-21-openjdk.x86_64 (/usr/lib/jvm/java-21-openjdk-21.0.5.0.11-1.fc40.x86_64/bin/java)
   4           /home/kumar/.jdks/jbrsdk_jcef-17.0.12/bin/java

Enter to keep the current selection[+], or type selection number:

Similarly, choose among the alternative options for javac

When you uninstall/remove your JDK packages, remember to delete the respective alternatives entries

Removing an alternatives entry

For example, to remove the JCEF JDK’s java command alternative entry:

sudo update-alternatives --remove java ~/.jdks/jbrsdk_jcef-17.0.12/bin/java

Add Java to PATH

This should not be necessary if you are following the update-alternatives method to manage between Java installations and all entries are complete

However, to manually set the configuration in your shell profile, you can do as below: