杰瑞科技汇

Linux下Java环境变量Path如何配置?

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:

Linux下Java环境变量Path如何配置?-图1
(图片来源网络,侵删)
  1. 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.
  2. PATH Environment 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's bin directory to your PATH.

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 -version returns 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 java returns nothing, the java executable is not in your PATH.

Step 2: Install Java (If Needed)

Choose the installation method that matches your distribution.

Linux下Java环境变量Path如何配置?-图2
(图片来源网络,侵删)

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.

  1. Install SDKMAN! (if not already installed):

    Linux下Java环境变量Path如何配置?-图3
    (图片来源网络,侵删)
    curl -s "https://get.sdkman.io" | bash
    source "$HOME/.sdkman/bin/sdkman-init.sh"
  2. 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_HOME and PATH for 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.

  1. Open the profile file for editing. /etc/profile is the main one, but /etc/environment is also common.

    sudo nano /etc/profile
  2. Add the following lines to the end of the file. Remember to replace /path/to/your/jdk with 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"
  3. Save and close the file (Ctrl+X, then Y, then Enter in nano).

  4. 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.

  1. Open your shell's configuration file. The most common are ~/.bashrc, ~/.zshrc, or ~/.profile. If you're not sure, ~/.bashrc is a safe bet for Bash.

    nano ~/.bashrc
  2. 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"
  3. Save and close the file.

  4. 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.

  1. Check JAVA_HOME:

    echo $JAVA_HOME

    This should print the path you set.

  2. Check PATH:

    echo $PATH

    You should see your $JAVA_HOME/bin directory listed at the beginning.

  3. Check the Java version from the new location:

    $JAVA_HOME/bin/java -version

    This should output the Java version information.

  4. 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 javac compiler and other development tools. Use default-jdk or java-latest-openjdk to 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 source the file: If you edit a profile file, you must either open a new terminal or run source <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 amd64 or x86_64). Mixing them can cause errors.
分享:
扫描分享到社交APP
上一篇
下一篇