Set JAVA_HOME on Windows 7, 8, 10, Mac OS X, Linux – 在Windows 7, 8, 10, Mac OS X, Linux上设置JAVA_HOME

最后修改: 2017年 1月 11日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

In this quick tutorial, we’ll take a look at how to set the JAVA_HOME variable on Windows, Mac OS X and Linux.

在这个快速教程中,我们将看一下如何在Windows、Mac OS X和Linux上设置JAVA_HOME变量。

2. Windows

2.窗户

2.1. Windows 10 and 8

2.1.Windows 10和8

  1. Open Search and type advanced system settings.
  2. In the shown options, select the View advanced system settings link.
  3. Under the Advanced tab, click Environment Variables.
  4. In the System variables section, click New (or User variables for single user setting).
  5. Set JAVA_HOME as the Variable name and the path to the JDK installation as the Variable value and click OK.
  6. Click OK and click Apply to apply the changes.

2.2. Windows 7

2.2 Windows 7

  1. On the Desktop, right-click My Computer and select Properties.
  2. Under the Advanced tab, click Environment Variables.
  3. In the System variables section, click New (or User variables for single user setting).
  4. Set JAVA_HOME as the Variable name and the path to the JDK installation as the Variable value and click OK.
  5. Click OK and click Apply to apply the changes.

Open Command Prompt and check the value of the JAVA_HOME variable:

打开命令提示符,检查JAVA_HOME变量的值。

echo %JAVA_HOME%

The result should be the path to the JDK installation:

结果应该是JDK的安装路径。

C:\Program Files\Java\jdk1.8.0_111

3. Mac OS X

3.Mac OS X

3.1. Single User – Mac OS X 10.5 or Newer

3.1.单用户 – Mac OS X 10.5或更新版本

From OS X 10.5, Apple introduced a command line tool (/usr/libexec/java_home) that dynamically finds the top Java version specified in Java Preferences for the current user.

从 OS X 10.5 开始,Apple 引入了一个命令行工具/usr/libexec/java_home),可以动态地找到当前用户在 Java 首选项中指定的顶级 Java 版本。

Open ~/.bash_profile in any text editor and add the following:

在任何文本编辑器中打开~/.bash_profile并添加以下内容。

export JAVA_HOME=$(/usr/libexec/java_home)

Save and close the file.

保存并关闭该文件。

Open a Terminal and run the source command to apply the changes:

打开一个终端,运行源命令来应用这些变化。

source ~/.bash_profile

Now we can check the value of the JAVA_HOME variable:

现在我们可以检查JAVA_HOME变量的值。

echo $JAVA_HOME

The result should be the path to the JDK installation:

结果应该是JDK的安装路径。

/Library/Java/JavaVirtualMachines/jdk1.8.0_111.jdk/Contents/Home

3.2. Single User – Mac OS X Older Versions

3.2.单用户–Mac OS X旧版

For older versions of OS X, we have to set the exact path to the JDK installation.

对于旧版本的OS X,我们必须设置JDK安装的确切路径。

Open ~/.bash_profile in any editor and add the following:

在任何编辑器中打开~/.bash_profile并添加以下内容。

export JAVA_HOME=/path/to/java_installation

Save and close the file.

保存并关闭该文件。

Open a Terminal and run the source command to apply the changes:

打开一个终端,运行源命令来应用这些变化。

source ~/.bash_profile

Now we can check the value of the JAVA_HOME variable:

现在我们可以检查JAVA_HOME变量的值。

echo $JAVA_HOME

The result should be the path to the JDK installation:

结果应该是JDK的安装路径。

/Library/Java/JavaVirtualMachines/jdk1.8.0_111.jdk/Contents/Home

3.3. Global Setting

3.3.全局设置

To set JAVA_HOME globally for all users, the steps are the same as for a single user, but we use the file /etc/profile.

要为所有用户设置JAVA_HOME,步骤与单个用户相同,但我们使用/etc/profile文件。

4. Linux

4.Linux

We’re going to manipulate the PATH here, of course, so here are the detailed instructions on how to do it.

当然,我们要在这里操作PATH,所以这里有关于如何操作的详细说明

4.1. Single User

4.1.单一用户

To set JAVA_HOME in Linux for a single user, we can use /etc/profile or /etc/environment (preferred for system-wide setting) or ~/.bashrc (user-specific setting).

要在Linux中为单个用户设置JAVA_HOME,我们可以使用/etc/profile/etc/environment(首选用于全系统设置)或~/.bashrc(用户特定设置)。

Open ~/.bashrc in any text editor and add the following:

在任何文本编辑器中打开~/.bashrc并添加以下内容。

export JAVA_HOME=/path/to/java_installation

Save and close the file.

保存并关闭该文件。

Run the source command to load the variable:

运行源命令来加载变量。

source ~/.bashrc

Now we can check the value of the JAVA_HOME variable:

现在我们可以检查JAVA_HOME变量的值。

echo $JAVA_HOME

The result should be the path to the JDK installation:

结果应该是JDK的安装路径。

/usr/lib/jvm/java-8-oracle

4.2. Global Setting

4.2.全局设置[/span>

To set JAVA_HOME in Linux for all users, we can use /etc/profile or /etc/environment (preferred).

要在Linux中为所有用户设置JAVA_HOME,我们可以使用/etc/profile/etc/environment(首选)。

Open /etc/environment in any text editor and add the following:

在任何文本编辑器中打开/etc/environment并添加以下内容。

JAVA_HOME=/path/to/java_installation

Please note that /etc/environment is not a script but a list of assignment expressions (that is why export is not used). This file is read at the time of login.

请注意,/etc/environment不是一个脚本,而是一个赋值表达式的列表(这就是为什么不使用export)。这个文件在登录时被读取。

To set JAVA_HOME using /etc/profile, here’s what we’ll add to the file:

要使用/etc/profile来设置JAVA_HOME,下面是我们要添加到该文件的内容。

export JAVA_HOME=/path/to/java_installation

Run the source command to load the variable:

运行源命令来加载变量。

source /etc/profile

Now we can check the value of the JAVA_HOME variable:

现在我们可以检查JAVA_HOME变量的值。

echo $JAVA_HOME

The result should be the path to the JDK installation:

结果应该是JDK的安装路径。

/usr/lib/jvm/java-8-oracle

5. Conclusion

5.结论

In this article, we covered ways to set the JAVA_HOME environment variable on Windows, Mac OS X and Linux.

在这篇文章中,我们介绍了在Windows、Mac OS X和Linux上设置JAVA_HOME环境变量的方法。