杰瑞科技汇

如何正确设置Java Home环境变量?

Of course! Setting the JAVA_HOME environment variable is a crucial step for many Java-based applications and build tools (like Maven, Gradle, and Tomcat) to locate your Java Development Kit (JDK).

如何正确设置Java Home环境变量?-图1
(图片来源网络,侵删)

The process is different depending on your operating system. Follow the instructions for your OS below.


Part 1: Find Your Java Installation Path

Before you can set JAVA_HOME, you need to know where the JDK is installed. Open a terminal or command prompt and run the following command:

java -XshowSettings:properties -version 2>&1 | grep "java.home"

This command will output the path to your Java installation. The JAVA_HOME variable should point to the parent directory of the bin folder.

For example, if the output is: java.home = /Library/Java/JavaVirtualMachines/jdk-17.jdk/Contents/Home

如何正确设置Java Home环境变量?-图2
(图片来源网络,侵删)

Then your JAVA_HOME should be: /Library/Java/JavaVirtualMachines/jdk-17.jdk/Contents/Home

If the output is: java.home = C:\Program Files\Java\jdk-17.0.2

Then your JAVA_HOME should be: C:\Program Files\Java\jdk-17.0.2


Part 2: Set the Environment Variable

Now, use the path you found to set the variable.

如何正确设置Java Home环境变量?-图3
(图片来源网络,侵删)

On Windows

There are two common methods: using the GUI or using the Command Prompt.

Method 1: Using the System Properties GUI (Recommended for most users)

  1. Press the Windows Key, type env, and select "Edit the system environment variables".
  2. In the System Properties window, click the "Environment Variables..." button.
  3. In the "System variables" section (at the bottom), click "New...".
  4. Enter the following information:
    • Variable name: JAVA_HOME
    • Variable value: (Paste the path you found in Part 1, e.g., C:\Program Files\Java\jdk-17.0.2)
  5. Click OK on all windows to save the changes.

Method 2: Using Command Prompt (Admin)

This method is useful for scripting or if you don't have GUI access.

  1. Open Command Prompt as Administrator. Right-click on the Start Menu and select "Windows PowerShell (Admin)" or "Command Prompt (Admin)".

  2. Find your JDK path (if you don't have it already) using the command from Part 1.

  3. Set the variable. Replace "C:\Path\To\Your\JDK" with your actual path.

    setx JAVA_HOME "C:\Program Files\Java\jdk-17.0.2"

    Note: setx makes the variable permanent for future command prompt sessions, but it won't affect the current one. You may need to open a new Command Prompt for the change to take effect.

(Optional) Update the PATH Variable

Some older applications might still rely on the PATH variable. It's generally better to use JAVA_HOME, but if you need to update PATH:

  1. In the "Environment Variables" window (from Method 1), find the Path variable in the "System variables" list and click "Edit...".
  2. Click "New" and add the path to your JDK's bin directory. For our example, this would be: C:\Program Files\Java\jdk-17.0.2\bin.
  3. Move this new entry to the top of the list to ensure it's found first.
  4. Click OK on all windows.

On macOS

Method 1: Using ~/.zshrc (for macOS Catalina and later)

The default shell on modern macOS is Zsh.

  1. Open Terminal.

  2. Find your JDK path using the command from Part 1.

  3. Edit the configuration file. Open the ~/.zshrc file in a text editor like Nano:

    nano ~/.zshrc
  4. Add the following lines to the end of the file. Replace the path with your own.

    # Set JAVA_HOME
    export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-17.jdk/Contents/Home
  5. Save the file and exit the editor (in Nano, press Ctrl+X, then Y, then Enter).

  6. Apply the changes to your current terminal session by running:

    source ~/.zshrc

Method 2: Using ~/.bash_profile (for older macOS versions or Bash shell)

If you are using the older Bash shell, you would edit ~/.bash_profile instead.

  1. Open Terminal.
  2. Edit the configuration file:
    nano ~/.bash_profile
  3. Add the following lines, replacing the path:
    # Set JAVA_HOME
    export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-17.jdk/Contents/Home
  4. Save and exit.
  5. Apply the changes:
    source ~/.bash_profile

On Linux (Ubuntu, Debian, CentOS, etc.)

The process is similar to macOS, but the shell configuration file is usually ~/.bashrc.

  1. Open Terminal.

  2. Find your JDK path using the command from Part 1. On Linux, it's often something like /usr/lib/jvm/java-17-openjdk-amd64.

  3. Edit the configuration file:

    nano ~/.bashrc
  4. Add the following lines to the end of the file. Replace the path with your own.

    # Set JAVA_HOME
    export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64
  5. Save and exit the editor.

  6. Apply the changes to your current terminal session:

    source ~/.bashrc

How to Verify Your Changes

After setting the variable, it's important to verify that it's working correctly.

  1. Open a new terminal window to ensure you are in a clean session.
  2. Check the value of JAVA_HOME:
    • Windows: echo %JAVA_HOME%
    • macOS / Linux: echo $JAVA_HOME
  3. Check the Java version using the JAVA_HOME path:
    • Windows: %JAVA_HOME%\bin\java -version
    • macOS / Linux: $JAVA_HOME/bin/java -version

You should see output similar to this, confirming that Java is being found from the correct location:

openjdk version "17.0.2" 2025-01-18
OpenJDK Runtime Environment Temurin-17.0.2+8 (build 17.0.2+8)
OpenJDK 64-Bit Server VM Temurin-17.0.2+8 (build 17.0.2+8, mixed mode, sharing)
分享:
扫描分享到社交APP
上一篇
下一篇