Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Find the Java SDK location on Linux
Finding the Java SDK location on Linux is a common task for developers who need to configure environment variables, set up IDEs, or manage multiple Java versions. This article demonstrates several reliable methods to locate your Java Development Kit (JDK) installation directory using built-in Linux commands.
Understanding Java Installations on Linux
On Linux systems, Java can be installed in various locations depending on the installation method and distribution. Common locations include /usr/lib/jvm/, /usr/java/, or /opt/java/. Additionally, many systems support multiple Java versions simultaneously, making it important to identify which version is currently active.
Method 1: Using update-java-alternatives
The update-java-alternatives command provides a comprehensive view of all installed Java versions. Use the -l (list) option to display all available alternatives:
update-java-alternatives -l
This command outputs information in the following format:
java-11-openjdk-amd64 1111 /usr/lib/jvm/java-11-openjdk-amd64 java-17-openjdk-amd64 1711 /usr/lib/jvm/java-17-openjdk-amd64
The output shows the alternative name, priority, and the full installation path for each Java version.
Method 2: Using update-alternatives
The update-alternatives command manages symbolic links for system commands. To find Java executable locations:
update-alternatives --list java update-alternatives --list javac
Example output:
/usr/lib/jvm/java-11-openjdk-amd64/bin/java /usr/lib/jvm/java-17-openjdk-amd64/bin/java
Method 3: Using which and readlink
Combine which and readlink commands to resolve symbolic links and find the actual Java installation:
readlink -f $(which java) readlink -f $(which javac)
The -f option follows all symbolic links recursively, revealing the final destination:
/usr/lib/jvm/java-11-openjdk-amd64/bin/java /usr/lib/jvm/java-11-openjdk-amd64/bin/javac
Method 4: Checking Environment Variables
If JAVA_HOME is already configured, you can check its value:
echo $JAVA_HOME
You can also use the java command itself to display system properties:
java -XshowSettings:properties -version 2>&1 | grep "java.home"
Managing Multiple Java Versions
When multiple Java versions are installed, you can switch between them using:
sudo update-alternatives --config java sudo update-alternatives --config javac
This presents an interactive menu to select the default Java version.
Verification Commands
After locating your Java installation, verify the version and configuration:
java -version javac -version
Conclusion
These methods provide reliable ways to locate Java SDK installations on Linux systems. The update-java-alternatives and readlink approaches are particularly useful for scripting and automation, while environment variable checking works well for quick verification of existing configurations.
