如何检查Java JDK是否安装在Mac上?

时间:2021-11-22 19:52:16

How do you check if Java SDK is installed on a Mac?

如何检查Java SDK是否安装在Mac上?

Is there a command line for this?

这里有命令行吗?

6 个解决方案

#1


125  

javac -version in a terminal will do

终端中的javac版本就可以了

#2


39  

You can leverage the java_home helper binary on OS X for what you're looking for.

您可以利用OS X上的java_home helper二进制文件来查找所需的内容。

To list all versions of installed JDK:

列出已安装JDK的所有版本:

$ /usr/libexec/java_home -V
Matching Java Virtual Machines (2):
    1.8.0_51, x86_64:   "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_51.jdk/Contents/Home
    1.7.0_79, x86_64:   "Java SE 7" /Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home

To request the JAVA_HOME path of a specific JDK version, you can do:

要请求特定JDK版本的JAVA_HOME路径,您可以这样做:

$ /usr/libexec/java_home -v 1.7
/Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home

$ /usr/libexec/java_home -v 1.8
/Library/Java/JavaVirtualMachines/jdk1.8.0_51.jdk/Contents/Home

You could take advantage of the above commands in your script like this:

您可以利用上述命令在您的脚本如下:

REQUESTED_JAVA_VERSION="1.7"
if POSSIBLE_JAVA_HOME="$(/usr/libexec/java_home -v $REQUESTED_JAVA_VERSION 2>/dev/null)"; then
    # Do this if you want to export JAVA_HOME
    export JAVA_HOME="$POSSIBLE_JAVA_HOME"
    echo "Java SDK is installed"
else
    echo "Did not find any installed JDK for version $REQUESTED_JAVA_VERSION"
fi

You might be able to do if-else and check for multiple different versions of java as well.

您可能还可以执行if-else,并检查多个不同版本的java。

If you prefer XML output, java_home also has a -X option to output in XML.

如果您喜欢XML输出,java_home也有一个-X选项以XML输出。

$ /usr/libexec/java_home --help
Usage: java_home [options...]
    Returns the path to a Java home directory from the current user's settings.

Options:
    [-v/--version   <version>]       Filter Java versions in the "JVMVersion" form 1.X(+ or *).
    [-a/--arch      <architecture>]  Filter JVMs matching architecture (i386, x86_64, etc).
    [-d/--datamodel <datamodel>]     Filter JVMs capable of -d32 or -d64
    [-t/--task      <task>]          Use the JVM list for a specific task (Applets, WebStart, BundledApp, JNI, or CommandLine)
    [-F/--failfast]                  Fail when filters return no JVMs, do not continue with default.
    [   --exec      <command> ...]   Execute the $JAVA_HOME/bin/<command> with the remaining arguments.
    [-R/--request]                   Request installation of a Java Runtime if not installed.
    [-X/--xml]                       Print full JVM list and additional data as XML plist.
    [-V/--verbose]                   Print full JVM list with architectures.
    [-h/--help]                      This usage information.

#3


17  

Type in a terminal:

在终端类型:

which javac

It should show you something like

它应该向你展示一些类似的东西

/usr/bin/javac

#4


1  

Just type javac. If it is installed you get usage information, otherwise it would just ask if you would like to install Java.

只是javac类型。如果安装了它,您将获得使用信息,否则它只会询问您是否希望安装Java。

#5


1  

/usr/bin/java_home tool returns 1 if java not installed.

如果java未安装,则/usr/bin/java_home工具返回1。

So you can check if java is installed by the next way:

所以你可以检查下一个方法是否安装了java:

/usr/libexec/java_home &> /dev/null && echo "installed" || echo  "not installed"

#6


0  

Make sure you correctly define the project's JDK and restart IntelliJ (full restart).

确保正确地定义了项目的JDK并重新启动IntelliJ(完全重启)。

#1


125  

javac -version in a terminal will do

终端中的javac版本就可以了

#2


39  

You can leverage the java_home helper binary on OS X for what you're looking for.

您可以利用OS X上的java_home helper二进制文件来查找所需的内容。

To list all versions of installed JDK:

列出已安装JDK的所有版本:

$ /usr/libexec/java_home -V
Matching Java Virtual Machines (2):
    1.8.0_51, x86_64:   "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_51.jdk/Contents/Home
    1.7.0_79, x86_64:   "Java SE 7" /Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home

To request the JAVA_HOME path of a specific JDK version, you can do:

要请求特定JDK版本的JAVA_HOME路径,您可以这样做:

$ /usr/libexec/java_home -v 1.7
/Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home

$ /usr/libexec/java_home -v 1.8
/Library/Java/JavaVirtualMachines/jdk1.8.0_51.jdk/Contents/Home

You could take advantage of the above commands in your script like this:

您可以利用上述命令在您的脚本如下:

REQUESTED_JAVA_VERSION="1.7"
if POSSIBLE_JAVA_HOME="$(/usr/libexec/java_home -v $REQUESTED_JAVA_VERSION 2>/dev/null)"; then
    # Do this if you want to export JAVA_HOME
    export JAVA_HOME="$POSSIBLE_JAVA_HOME"
    echo "Java SDK is installed"
else
    echo "Did not find any installed JDK for version $REQUESTED_JAVA_VERSION"
fi

You might be able to do if-else and check for multiple different versions of java as well.

您可能还可以执行if-else,并检查多个不同版本的java。

If you prefer XML output, java_home also has a -X option to output in XML.

如果您喜欢XML输出,java_home也有一个-X选项以XML输出。

$ /usr/libexec/java_home --help
Usage: java_home [options...]
    Returns the path to a Java home directory from the current user's settings.

Options:
    [-v/--version   <version>]       Filter Java versions in the "JVMVersion" form 1.X(+ or *).
    [-a/--arch      <architecture>]  Filter JVMs matching architecture (i386, x86_64, etc).
    [-d/--datamodel <datamodel>]     Filter JVMs capable of -d32 or -d64
    [-t/--task      <task>]          Use the JVM list for a specific task (Applets, WebStart, BundledApp, JNI, or CommandLine)
    [-F/--failfast]                  Fail when filters return no JVMs, do not continue with default.
    [   --exec      <command> ...]   Execute the $JAVA_HOME/bin/<command> with the remaining arguments.
    [-R/--request]                   Request installation of a Java Runtime if not installed.
    [-X/--xml]                       Print full JVM list and additional data as XML plist.
    [-V/--verbose]                   Print full JVM list with architectures.
    [-h/--help]                      This usage information.

#3


17  

Type in a terminal:

在终端类型:

which javac

It should show you something like

它应该向你展示一些类似的东西

/usr/bin/javac

#4


1  

Just type javac. If it is installed you get usage information, otherwise it would just ask if you would like to install Java.

只是javac类型。如果安装了它,您将获得使用信息,否则它只会询问您是否希望安装Java。

#5


1  

/usr/bin/java_home tool returns 1 if java not installed.

如果java未安装,则/usr/bin/java_home工具返回1。

So you can check if java is installed by the next way:

所以你可以检查下一个方法是否安装了java:

/usr/libexec/java_home &> /dev/null && echo "installed" || echo  "not installed"

#6


0  

Make sure you correctly define the project's JDK and restart IntelliJ (full restart).

确保正确地定义了项目的JDK并重新启动IntelliJ(完全重启)。