How to Check if Java is Installed – 如何检查Java是否已安装

最后修改: 2018年 12月 9日

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

1. Overview

1.概述

In this short tutorial, we’re going to take a look at a few ways to determine if Java is installed on a machine.

在这个简短的教程中,我们将看一下确定机器上是否安装了Java的几种方法。

2. Command Line

2.命令行

First, let’s open a command window or terminal and enter:

首先,让我们打开一个命令窗口或终端,并输入:

> java -version

If Java is installed and the PATH is configured correctly, our output will be similar to:

如果Java已经安装并且PATH配置正确,我们的输出将类似于。

java version "1.8.0_31"
Java(TM) SE Runtime Environment (build 1.8.0_31-b13)
Java HotSpot(TM) Client VM (build 25.31-b07, mixed mode, sharing)

Otherwise, we’ll see an error message like the one below and we need to check elsewhere:

否则,我们会看到像下面这样的错误信息,我们需要在其他地方检查。

'java' is not recognized as an internal or external command,
operable program or batch file.

The exact messages we see will vary depending on the operating system used and the Java version installed.

我们看到的确切信息会因使用的操作系统和安装的Java版本而不同。

3. When Path Is Not Set

3.未设置路径时

Going to a command line and typing java -version can tell us for sure if Java is installed. However, if we see an error message, Java might still be installed – we’ll just need to investigate further.

在命令行中输入java -version 可以肯定地告诉我们Java是否已经安装。然而,如果我们看到一个错误信息,Java可能仍然被安装 – 我们只需要进一步调查。

Many discussions about using java -version mention the JAVA_HOME environment variable. This is misleading because JAVA_HOME won’t affect our java -version results.

许多关于使用java -version的讨论都提到了JAVA_HOME环境变量。这是个误导,因为JAVA_HOME不会影响我们的java -version结果。

Additionally, JAVA_HOME should point to a JDK and other applications that use JDK features, such as Maven, use it.

此外,JAVA_HOME 应该指向一个 JDK,其他使用 JDK 功能的应用程序,如 Maven,也会使用它。

For more information, please check our articles JAVA_HOME should point to a JDK and how to set JAVA_HOME.

更多信息,请查看我们的文章JAVA_HOME应指向JDK如何设置JAVA_HOME

So, let’s look at alternative ways to find Java, in case the command-line failed us.

因此,让我们看看寻找Java的其他方法,以防命令行让我们失望。

3.1. Windows 10

3.1 Windows 10

On Windows, we can find it in the Application list:

在Windows上,我们可以在应用程序列表中找到它。

  1. Press the Start Button
  2. Scroll down the application list to J
  3. Open the Java folder
  4. Click About Java

how to check if java installed windows apps about java

We can also look at installed Programs and Features:

我们还可以查看已安装的程序和功能。

  1. In the Search bar, type Control Panel
  2. Click Programs
  3. If the Java icon present, then Java is installed
  4. If not, click Programs and Features, and look for installed versions of Java in the J’s

how to check if java installed windows programs features

3.2. Mac OS X

3.2. Mac OS X

To see if Java 7 or greater is installed on a Mac we can:

要查看Mac上是否安装了Java 7或更高版本,我们可以。

  1. Go to System Preferences
  2. Look for the Java icon

how to check if java installed mac sys prefs

For earlier versions of Java, we’ll need to:

对于早期版本的Java,我们需要。

  1. Open Finder
  2. Go to the Applications folder
  3. Go to the Utilities folder
  4. Look for the Java Preferences app

3.3. *Nix

3.3.*尼克斯

There are few different package managers in the *nix world.

在*nix世界中,有几个不同的软件包管理器

In a Debian-based distribution, we can use the aptitude search command:

在基于Debian的发行版中,我们可以使用aptitude search命令。

$ sudo aptitude search jdk jre

If there is an i before the result, then that means the package is installed:

如果结果前面有一个i,那么就意味着该软件包已经安装。

...
i   oracle-java8-jdk                - Java™ Platform, Standard Edition 8 Develop
...

4. Other Command Line Tools

4.其他命令行工具

In addition to java -version, there are some other command line tools we can use to learn about our Java installation.

除了java -version,还有一些其他的命令行工具,我们可以用来了解我们的Java安装情况。

4.1. Windows where Command

4.1.Windows where 命令

In Windows, we can use the where command to find where our java.exe is located:

在Windows中,我们可以使用where命令来查找我们的java.exe的位置。

> where java

And our output will look something like:

而我们的输出将看起来像这样。

C:\Apps\Java\jdk1.8.0_31\bin\java.exe

However, as with java -version, this command is only useful if our PATH environment variable points to the bin directory.

然而,与java -version一样,这个命令只有在我们的PATH环境变量指向bin目录时才有用。

4.2. Mac OS X and *nix which and whereis

4.2.Mac OS X和*nix whichwhereis

In a *nix system or on a Mac in the Terminal app, we can use the which command:

在*nix系统或Mac的终端应用中,我们可以使用which命令。

$ which java

The output tells us where the Java command is:

输出告诉我们Java命令的位置。

/usr/bin/java

Now let’s use the whereis command:

现在让我们使用whereis命令。

$ whereis java -b

The whereis command also gives us the path to our Java installation:

whereis命令还提供了我们的Java安装路径。

/usr/bin/java

As with java -version, these commands will only find Java if it’s on the path. We can use these when we have Java installed but want to know exactly what’s going to run when we use the java command.

java -version一样,这些命令只有在路径上有Java时才会找到它。当我们已经安装了 Java,但想确切地知道使用 java 命令时将会运行什么时,我们可以使用这些命令。

5. Conclusion

5.总结

In this short article, we discussed how to find out if Java is installed on a Windows 10, Mac OS X or Linux/Unix machine even if it’s not on the PATH.

在这篇短文中,我们讨论了如何在Windows 10、Mac OS X或Linux/Unix机器上查找是否安装了Java,即使它不在PATH上。

We also looked at a couple useful commands for locating our Java installation.

我们还看了几个有用的命令来定位我们的Java安装。