How To Find Jdk Path In Linux – Using Update Alternatives Command Line

Java development on Linux requires knowing where your JDK installation resides. This guide will show you exactly how to find jdk path in linux using multiple methods, from simple commands to advanced techniques. Whether you’re setting up environment variables or debugging build issues, knowing your JDK location is essential for smooth development.

Why You Need To Know The JDK Path

Your JDK path tells your system where Java tools like javac and java live. Without it, many development tools won’t work properly. Build tools like Maven or Gradle often need this path configured. Environment variables like JAVA_HOME rely on it. So let’s get you sorted quickly.

How To Find Jdk Path In Linux

The quickest way to find your JDK location is using the terminal. Open your favorite terminal emulator and try these commands. Each method works slightly differently, so pick the one that suits your situation best.

Using The Which Command

The which command shows the path of executables in your system’s PATH. It’s the simplest approach for most users.

  1. Open a terminal window.
  2. Type which java and press Enter.
  3. The output shows something like /usr/bin/java.
  4. This is a symbolic link, not the actual JDK home.

To resolve the symlink and find the real path, use readlink -f $(which java). This gives you the true location of the java binary. For example, it might show /usr/lib/jvm/java-11-openjdk-amd64/bin/java.

Using The Readlink Command

The readlink command follows symbolic links to reveal the actual file. Combine it with which for a complete solution.

  1. Run readlink -f $(which java) in your terminal.
  2. The output shows the full path to the java executable.
  3. Remove the /bin/java part to get the JDK home directory.
  4. For example, /usr/lib/jvm/java-11-openjdk-amd64.

This method works on most Linux distributions including Ubuntu, Fedora, and CentOS. It’s reliable and fast.

Using The Update-Alternatives Command

Linux systems often use the alternatives system to manage multiple Java versions. This command shows all configured Java installations.

  1. Type update-alternatives --list java in the terminal.
  2. If you have multiple versions, each appears on a separate line.
  3. Note the path shown, like /usr/lib/jvm/java-11-openjdk-amd64/bin/java.
  4. Again, remove /bin/java for the JDK home.

This method is especially useful when you have multiple JDK versions installed. You can see all of them at once.

Checking Environment Variables

Sometimes the JDK path is already set in your environment variables. Check JAVA_HOME first, as many applications use it.

  1. Run echo $JAVA_HOME in your terminal.
  2. If it returns a path like /usr/lib/jvm/java-11-openjdk-amd64, you’re done.
  3. If it’s empty, check echo $JDK_HOME as an alternative.
  4. Some systems also set JRE_HOME for the JRE path.

If these variables are not set, you can set them manually after finding your JDK path. Add the export command to your .bashrc or .zshrc file.

Using The Locate Command

The locate command searches a database of files on your system. It’s faster than find but requires an updated database.

  1. First, update the database with sudo updatedb (if needed).
  2. Run locate java | grep -i "/bin/java$".
  3. This shows all java binaries on your system.
  4. Pick the one that looks like a JDK installation directory.

This method finds files even if they’re not in your PATH. It’s good for discovering hidden or alternative installations.

Using The Find Command

The find command searches your filesystem in real time. It’s thorough but can be slow on large systems.

  1. Run sudo find / -name "javac" -type f 2>/dev/null.
  2. This searches for the javac compiler binary.
  3. Look for results in directories like /usr/lib/jvm or /opt.
  4. Note the parent directory as your JDK home.

You can narrow the search to common directories like /usr/lib/jvm or /usr/local to speed things up. Use sudo find /usr/lib/jvm -name "javac" -type f.

Checking Common Installation Locations

Linux distributions often install JDK in standard locations. Check these directories manually if commands fail.

  • /usr/lib/jvm/ – Common for OpenJDK on Debian/Ubuntu
  • /usr/java/ – Used by Oracle JDK on some systems
  • /opt/java/ – Manual installations often go here
  • /usr/local/java/ – User-installed versions
  • /snap/ – Snap package installations

List the contents of these directories with ls -la to see what’s installed. You’ll often find versioned subdirectories like java-11-openjdk-amd64.

Using The Java -XshowSettings Command

Java itself can show you its installation details. Use the -XshowSettings flag for comprehensive information.

  1. Run java -XshowSettings:all 2>&1 | grep "java.home".
  2. The output shows the java.home property.
  3. This is the JDK home directory directly from the JVM.
  4. Example output: java.home = /usr/lib/jvm/java-11-openjdk-amd64

This method is reliable because it comes from the Java runtime itself. No guessing or symlink following needed.

Checking Package Manager Information

If you installed JDK via a package manager, you can query it for installation details. Different managers have different commands.

For APT (Debian/Ubuntu)

  • Run dpkg -L openjdk-11-jdk | grep "/bin/javac"
  • This shows files installed by the package.
  • Look for the base directory in the output.

For YUM/DNF (Fedora/CentOS)

  • Run rpm -ql java-11-openjdk-devel | grep "/bin/javac"
  • This lists files from the RPM package.
  • Identify the JDK home from the path.

For Pacman (Arch Linux)

  • Run pacman -Ql jdk11-openjdk | grep "/bin/javac"
  • This shows package files on Arch-based systems.
  • Extract the JDK home from the file path.

Setting JAVA_HOME Permanently

Once you’ve found your JDK path, set it as an environment variable. This makes it available to all applications.

  1. Edit your shell profile file: nano ~/.bashrc or nano ~/.zshrc.
  2. Add the line: export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64
  3. Also add: export PATH=$JAVA_HOME/bin:$PATH
  4. Save the file and run source ~/.bashrc to apply changes.

Now your system knows where Java lives. Test it with echo $JAVA_HOME and javac -version.

Handling Multiple JDK Versions

Many developers work with multiple JDK versions. Here’s how to manage them effectively.

  • Use update-alternatives --config java to switch default version.
  • Set JAVA_HOME dynamically in project scripts.
  • Use tools like SDKMAN or jEnv for version management.
  • Check /usr/lib/jvm for all installed versions.

SDKMAN is particularly useful. Install it with curl -s "https://get.sdkman.io" | bash, then use sdk list java to see available versions.

Troubleshooting Common Issues

Sometimes things don’t work as expected. Here are solutions to frequent problems.

Java Command Not Found

  • Install JDK if not present: sudo apt install default-jdk
  • Check if Java is in PATH: echo $PATH
  • Reinstall if corrupted: sudo apt install --reinstall openjdk-11-jdk

Symlink Points To Wrong Location

  • Use readlink -f to find the real path.
  • Update alternatives: sudo update-alternatives --config java
  • Manually fix symlinks: sudo ln -sf /new/path/java /usr/bin/java

JAVA_HOME Not Recognized

  • Ensure the variable is exported: export JAVA_HOME
  • Check for typos in your shell profile.
  • Verify the path exists: ls -la $JAVA_HOME
  • Restart your terminal or source the profile again.

Using Graphical Tools

If you prefer GUI over command line, some desktop environments offer tools. GNOME’s “About” panel shows system info but not JDK details directly. File managers like Nautilus can browse to /usr/lib/jvm visually. System monitoring tools like “Hardinfo” might show installed packages. However, command line methods are generally faster and more reliable.

Automating The Search With Scripts

Create a simple shell script to find your JDK path automatically. This saves time if you check frequently.

#!/bin/bash
# find_jdk.sh - Locate JDK home directory

JAVA_PATH=$(readlink -f $(which java) 2>/dev/null)
if [ -n "$JAVA_PATH" ]; then
    JDK_HOME=$(dirname $(dirname "$JAVA_PATH"))
    echo "JDK Home: $JDK_HOME"
else
    echo "Java not found in PATH"
    # Fallback to common locations
    for dir in /usr/lib/jvm/* /usr/java/* /opt/java/*; do
        if [ -f "$dir/bin/javac" ]; then
            echo "Found JDK at: $dir"
        fi
    done
fi

Save this as find_jdk.sh, make it executable with chmod +x find_jdk.sh, and run it whenever needed.

Understanding The Output

When you run these commands, you’ll see paths like /usr/lib/jvm/java-11-openjdk-amd64. This is the JDK home directory. Inside it, you’ll find:

  • bin/ – Contains java, javac, javadoc, etc.
  • lib/ – Library files and JARs
  • include/ – C header files for JNI
  • jre/ – JRE (sometimes separate)
  • man/ – Manual pages

Knowing this structure helps you verify you’ve found the right directory. If you see these subdirectories, you’re in the right place.

Security Considerations

Be careful when running commands with sudo. Only use elevated privileges when necessary. The find command without sudo might miss some directories, but it’s safer. Avoid running scripts from untrusted sources that claim to find your JDK path. Stick to the methods described here.

Cross-Distribution Differences

Different Linux distributions organize JDK installations differently. Here’s what to expect:

  • Debian/Ubuntu: JDK in /usr/lib/jvm/, managed by alternatives system.
  • Fedora/RHEL: JDK in /usr/lib/jvm/, uses alternatives too.
  • Arch Linux: JDK in /usr/lib/jvm/, but structure may vary.
  • OpenSUSE: JDK in /usr/lib64/jvm/ on 64-bit systems.
  • Slackware: Manual installations often in /usr/local/.

Always check your distribution’s documentation for specifics. The commands in this article work on most distributions with minor adjustments.

Using Container Environments

If you’re working inside Docker or other containers, finding the JDK path is similar. The same commands work inside the container. However, containers often have minimal installations. You might need to install which or readlink first. Use apt-get update && apt-get install -y coreutils if needed.

Verifying Your JDK Installation

After finding the path, confirm it’s a valid JDK. Run these checks:

  1. $JAVA_HOME/bin/java -version – Shows Java version.
  2. $JAVA_HOME/bin/javac -version – Shows compiler version.
  3. ls $JAVA_HOME/bin/ – Lists all Java tools.
  4. test -f $JAVA_HOME/lib/tools.jar – Checks for tools JAR.

If all these pass, you have a working JDK installation. If not, you may have found a JRE instead of a full JDK.

FAQ

What is the default JDK path in Linux?

There’s no single default path. Common locations include /usr/lib/jvm/java-11-openjdk-amd64 on Debian systems and /usr/java/latest on Oracle installations. It varies by distribution and installation method.

How can I find JDK path if Java is not in PATH?

Use sudo find / -name "javac" -type f 2>/dev/null to search the entire filesystem. Alternatively, check common directories like /usr/lib/jvm or /opt manually.

What’s the difference between JDK and JRE paths?

The JDK includes development tools like javac and javadoc. The JRE only contains the runtime. JDK paths have a bin/javac file, while JRE paths don’t. Always use the JDK path for JAVA_HOME.

Can I set JAVA_HOME to a JRE path?

You can, but it’s not recommended. Many build tools and IDEs require the full JDK. Setting it to a JRE path will cause errors when trying to compile code. Always point to the JDK home.

How do I find the JDK path for a specific version?

Use update-alternatives --list