Of course! Configuring the Java path on Linux is a fundamental task. The "Java path" can mean two different things, and it's crucial to understand the difference:

JAVA_HOME: An environment variable that points to the root directory of your JDK (Java Development Kit) installation. This is primarily used by applications, servers (like Tomcat or Jetty), and build tools (like Maven or Gradle) to find Java.PATHEnvironment Variable: A system-wide list of directories where the operating system looks for executable files. To run Java commands (java,javac, etc.) from any directory, you need to add the JDK'sbindirectory to yourPATH.
This guide will cover both, with instructions for the most common Linux package managers (apt for Debian/Ubuntu, dnf/yum for RHEL/CentOS/Fedora) and manual installations.
Step 1: Check if Java is Already Installed
Before you begin, check if Java is already installed and which version it is.
Open your terminal and run:
# Check for the java command java -version # Check for the javac (compiler) command javac -version # Find the location of the java executable which java
- If
java -versionreturns a version number, Java is installed. You might just need to configure the paths. - If it says "command not found", you need to install Java first.
- If
which javareturns nothing, thejavaexecutable is not in yourPATH.
Step 2: Install Java (If Needed)
Choose the installation method that matches your distribution.

Method A: Using apt (Debian, Ubuntu, Mint)
This is the recommended method for most Debian-based systems. It will install OpenJDK, the open-source reference implementation of Java.
# Update your package list sudo apt update # Install the default JDK (usually the latest LTS version) sudo apt install default-jdk # If you need a specific version, e.g., JDK 11 or 17 sudo apt install openjdk-11-jdk sudo apt install openjdk-17-jdk
Method B: Using dnf or yum (RHEL, CentOS, Fedora, Rocky Linux, AlmaLinux)
This is the standard for Red Hat-based systems.
# For systems using dnf (Fedora, newer RHEL/CentOS) sudo dnf install java-latest-openjdk # Or for a specific version sudo dnf install java-17-openjdk # For systems using yum (older RHEL/CentOS) sudo yum install java-latest-openjdk sudo yum install java-17-openjdk
Method C: Manual Installation (Using SDKMAN!)
This is a fantastic, tool-agnostic way to manage multiple Java versions on any Linux system.
-
Install SDKMAN! (if not already installed):
(图片来源网络,侵删)curl -s "https://get.sdkman.io" | bash source "$HOME/.sdkman/bin/sdkman-init.sh"
-
Install a Java version:
# List available versions sdk list java # Install a specific version (e.g., 17.0.9-tem) sdk install java 17.0.9-tem # Set it as the default sdk default java 17.0.9-tem
SDKMAN! automatically handles setting
JAVA_HOMEandPATHfor you.
Step 3: Find the Installation Path
After installation, you need to find where Java was installed so you can point your environment variables to it.
For apt and dnf/yum installations:
The package manager usually creates a symbolic link in /usr/lib/jvm/. Find the actual path by listing the contents of that directory.
ls -l /usr/lib/jvm/
You will see output similar to this. The directory with the version number is the one you need.
total 0
drwxr-xr-x 3 root root 60 May 10 12:00 default-java
drwxr-xr-x 7 root root 220 May 10 12:00 java-11-openjdk-amd64
drwxr-xr-x 7 root root 240 May 10 12:00 java-17-openjdk-17.0.9
lrwxrwxrwx 1 root root 29 May 10 12:00 java-17-openjdk -> /etc/alternatives/java_sdk_openjdk
In this example, the JDK 17 path is /usr/lib/jvm/java-17-openjdk-17.0.9/. This is the path you will use for JAVA_HOME.
For SDKMAN! installations:
SDKMAN! installs Java into your home directory by default.
echo $JAVA_HOME
This will output something like /home/your_user/.sdkman/candidates/java/current/. You can use this path directly.
Step 4: Set JAVA_HOME and PATH
Now you will set the environment variables. There are two scopes: system-wide (for all users) or user-specific (just for you).
Option 1: System-Wide Configuration (Recommended for Servers)
This affects all users on the system. You need sudo privileges.
-
Open the profile file for editing.
/etc/profileis the main one, but/etc/environmentis also common.sudo nano /etc/profile
-
Add the following lines to the end of the file. Remember to replace
/path/to/your/jdkwith the actual path you found in Step 3.# Set JAVA_HOME export JAVA_HOME=/path/to/your/jdk # e.g., /usr/lib/jvm/java-17-openjdk-17.0.9/ # Add JDK bin to PATH export PATH="$JAVA_HOME/bin:$PATH"
-
Save and close the file (
Ctrl+X, thenY, thenEnterin nano). -
Apply the changes to the current session by running:
source /etc/profile
Option 2: User-Specific Configuration (Recommended for Developers)
This only affects your current user. No sudo is needed.
-
Open your shell's configuration file. The most common are
~/.bashrc,~/.zshrc, or~/.profile. If you're not sure,~/.bashrcis a safe bet for Bash.nano ~/.bashrc
-
Add the same lines to the end of the file. Remember to replace the path.
# Set JAVA_HOME export JAVA_HOME=/path/to/your/jdk # e.g., /home/your_user/.sdkman/candidates/java/current/ # Add JDK bin to PATH export PATH="$JAVA_HOME/bin:$PATH"
-
Save and close the file.
-
Apply the changes to your current terminal session:
source ~/.bashrc
Or, simply open a new terminal window.
Step 5: Verify the Configuration
Finally, verify that everything is set up correctly.
-
Check
JAVA_HOME:echo $JAVA_HOME
This should print the path you set.
-
Check
PATH:echo $PATH
You should see your
$JAVA_HOME/bindirectory listed at the beginning. -
Check the Java version from the new location:
$JAVA_HOME/bin/java -version
This should output the Java version information.
-
Check that the commands work from anywhere:
java -version javac -version
These should now work without any "command not found" errors.
Common Pitfalls
- Confusing JRE and JDK: If you only install the JRE (Java Runtime Environment), you won't have the
javaccompiler and other development tools. Usedefault-jdkorjava-latest-openjdkto get the full JDK. - Using the wrong path: Double-check the path you found in
/usr/lib/jvm/. A typo here will cause everything to fail. - Forgetting to
sourcethe file: If you edit a profile file, you must either open a new terminal or runsource <file_path>for the changes to take effect in your current session. - Mixing 32-bit and 64-bit: Ensure you have the correct architecture (usually
amd64orx86_64). Mixing them can cause errors.
