Java installation

Different ways to install java in Linux distributions and MacOS

Using sdkman to install and manage jdk versions

How to set JAVA_HOME in Linux for all users

JDK Installation

Download Java from openjdk website

https://openjdk.org/projects/jdk/

https://jdk.java.net/archive/

A more straight-forward way is to download it from openjdk install page and extracting it using the command:

$ tar xvf openjdk-13*_bin.tar.gz

Confirm the version of the new JVM using the -version option:

$ cd jdk-18.0.12+7/bin
$ ./java -version

The version output of the JVM looks similar to this:

openjdk version "18" 2022-03-22
OpenJDK Runtime Environment (build 18+36-2087)
OpenJDK 64-Bit Server VM (build 18+36-2087, mixed mode, sharing)

Set the path.

export JAVA_HOME=~/Downloads/jdk-18
export PATH=$JAVA_HOME/bin:$PATH

How to find out where Java is installed?

If Java is installed using package managers or by using any other ways other than downloading the JDK from https://openjdk.java.net/install/ , it can get tricky to find out where it is installed.

  1. The following command will tell you a lot of information about your java version, including the vendor. It works on Windows, Mac, and Linux.

    java -XshowSettings:properties -version
    
  2. Here is another way to find it.

    find /usr/lib/jvm/java-1.x.x-openjdk
    

    In Linux machines, if jdk is installed through the tools provided by the OS, the jdk folder is usually in /usr/lib/jvm folder.

  3. Here is another way to find it. Use the whereis command and follow the symbolic links to find the Java path.

    [explorer436@explorer436-p50-20eqs27p03 bin]$ whereis java
    java: /usr/bin/java /usr/share/java /home/explorer436/Documents/jdk-18/bin/java /usr/lib/jvm/java-17-openjdk/bin/java
    

    The output tells you that Java is located in /usr/bin/java. List the content of the /usr/bin/java directory.

    [explorer436@explorer436-p50-20eqs27p03 ~]$ ls -l /usr/bin/java
    lrwxrwxrwx 1 root root 37 Jan 19  2023 /usr/bin/java -> /usr/lib/jvm/default-runtime/bin/java
    

    Inspecting the directory shows that /usr/bin/java is only a symbolic link for /usr/lib/jvm/default-runtime/bin/java

    Just like in the previous step, list the content of the provided path by running

    [explorer436@explorer436-p50-20eqs27p03 ~]$ whereis /usr/lib/jvm/default-runtime/bin/java
    java: /usr/bin/java /usr/share/java /home/explorer436/Documents/jdk-18/bin/java /usr/lib/jvm/java-17-openjdk/bin/java
    

How to set JAVA_HOME and PATH environment variables after figuring out where the jdk is?

Look at Linux/Path.org for details about how the path can be set in .bashrc

You can also set it in /etc/profile. Try the .bashrc option before trying this. #+begin_src vim /etc/profile + +#+end_src

Prepend sudo if logged in as not-privileged user, ie. sudo vim

Press ‘i’ to get in insert mode and add this to the file:

export JAVA_HOME="path to the jdk directory that you extracted using the tar xvf command"
export PATH=$JAVA_HOME/bin:$PATH

It should look like this:

export JAVA_HOME=~/Downloads/jdk-18
export PATH=$JAVA_HOME/bin:$PATH

logout and login again, reboot, or use source /etc/profile to apply changes immediately in your current shell

Test if they are set correctly:

echo $JAVA_HOME
echo $path

Macbook

echo "export JAVA_HOME=/opt/homebrew/opt/openjdk" >> ~/.zshrc
echo "export PATH=/opt/homebrew/opt/openjdk/bin:$PATH" >> ~/.zshrc
source ~/.zshrc
java -version

Windows

  1. Download the zip file for jdk and unzip it into a location.
  2. After that, set JAVA_HOME in environment variables. Don’t include the \bin folder, just the JDK path. For example – C:\Program Files\Java\jdk1.8.0_60
  3. After that, update PATH in environment variables. Add this: %JAVA_HOME%\bin (If you are looking at the old view, the entries will be separated by semi-colon)

If Java is already installed, how to check where it is installed?

On Linux

Open a terminal and type:

> $JAVA_HOME/bin/javac -version

If JAVA_HOME points to a JDK, the output should look like:

> javac 1.X.0_XX

If JAVA_HOME doesn’t point to a JDK, the OS will throw an error message:

> bash: /bin/javac: No such file or directory

On Mac OS

Use /usr/libexec/java_home -v 1.8 command on a terminal shell to figure out where is your Java 1.8 home directory

If you just want to find out the home directory of your most recent version of Java, omit the version. e.g. /usr/libexec/java_home

Usually, if you have multiple JDKs installed in your machine, you can navigate from the value of the above command to tell what other versions are installed in your machine.

Troubleshooting installation

Even if you install openjdk using HomeBrew in Macbook, make sure to run the commands to set JAVA_HOME and PATH using the commands from terminal. If not, installation of other components like maven will have issues.

This will also help with the scenario where you have multiple jdk versions installed in the computer and you have to switch between them from time to time.

Removing jdk and jre in Arch Linux

Sometimes we may have to remove jdk or jre from Arch Linux.

How to find out which jdk is installed?

pacman -Q | grep jdk

Removing

sudo pacman -Rs jre14-openjdk
sudo pacman -Rs jre17-openjdk

This command will remove the OpenJDK package and its dependencies. -R removes the package, -n removes all unneeded dependencies, and -s removes all configuration files.

sudo pacman -Rns jdk-openjdk