Find the Java SDK location on Linux


Overview

Finding software’s installation directory is a very common operation. One common reason is for updating the PATH environment variable. For example, Java developers are often interested in finding the installation directory of Java. This article shows how to find the location of the JDK on Linux. The method described here works on both 32-bit and 64-bit versions of these operating systems.

The JDK includes many tools that can be used by programmers. In this article we will look at some of them. We will start with the most basic tool: the java command.

The Java Command

The java command is one of the most important commands available to Java programmers. It allows you to run programs written in the Java programming language. The java command has two main uses. First, it lets you execute Java code directly from the command line. Second, it provides an easy way to build your own Java applications.

To use the java command, type java followed by the name of the class or package containing the program you want to run. For example, if you have a file called HelloWorld.java in the current working directory, then you could run the following command to compile and run the program −

$ javac HelloWorld.java
$ java HelloWorld

If you don’t specify any arguments after the java command, then the default JRE version is used. You can also set the JAVA_HOME environment variable to point to a different JRE.

Multiple Java in a System

You may need to install additional versions of the Java runtime environment (JRE) when using multiple versions of Java on your system. To do so, you must use the update−java−alternatives command. This command updates the symbolic links that link to the various versions of the JRE.

For example, suppose you have installed three versions of the JRE on your system: 1.5.0_10, 1.6.0_20, and 1.7.0_25. If you wanted to use the latest version of the JRE, you would first remove all existing versions of the JRE −

$ sudo apt-get purge openjdk*

Then you would create a new symbolic link pointing to the newest version of the JRE −

sudo ln -sf /usr/lib/jvm/java-1.7.0-openjdk-amd64/bin/java /usr/bin/java

Finally, you would update the symbolic links that point to other versions of the JRE to point to the newly created symbolic link− sudo update−alternatives −config java

This process ensures that the correct version of the JRE is always selected when you invoke the java command.

Java Development Kit Installation Directory

The JDK contains several components, including the compiler, development libraries, and tools needed to develop Java applications. These components are typically stored in subdirectories under the JDK installation directory. On Linux, the JDK installation directory is usually located at /usr/lib/java/.

Note that the JDK installation directory may vary depending on which distribution you are using. For example, on Ubuntu 10.04 LTS, the JDK is located at /usr/share/java/.

Finding the JDK Installation Directory

There are several ways to locate the JDK installation directory on Linux. The easiest way is to check the contents of the /etc/profile file. The /etc/profile file contains information about where the user's home directories are located as well as the location of the JDK installation directory for each user account.

Using the update-java-alternatives Command

Update-Java-Alternatives updates all alternatives for the Java runtimes and development kits. We can use it together with the -l argument when searching for the JDK or JVM installation directory −

$ update-java-alternatives -l
java-1.14.0-openjdk-amd64 1411 /usr/lib/jvm/java-1.14.0-openjdk-amd64

Using the update-alternatives Command

The update−alternative tool maintains symbolic links to determine which programs are used by the system when no specific program is specified. You can combine the −jar command line option with the −list option to list the locations of the Java SDK or JRE.

$ update-alternatives --list java
/usr/lib/jvm/java-14-openjdk-amd64/bin/java

Using the which and readlink Commands

The which command displays the full path of a file and the readlink command solves the symbolic link. We can use a combination of these commands to find the location of the JDK and JRE respectively −

$ readlink -f $(which javac)
/usr/lib/jvm/java-14-openjdk-amd64/bin/javac
$ readlink -f $(which java)
/usr/lib/jvm/java-14-openjdk-amd64/bin/java

If you use the −f option, then it will follow every symbolic link in recursive fashion.

Conclusion

We've covered several different methods for finding the location of Java. We can use these command line tools in our daily lives while using the Linux operating systems.

Updated on: 23-Dec-2022

18K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements