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

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

有没有一个命令行呢?

在terminal中的javac -version会做

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

要列出已安装的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 

要请求特定JDK版本的JAVA_HOMEpath,您可以执行:

 $ /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 

你可以像这样在脚本中利用上面的命令:

 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 

你也许可以做if-else并检查多个不同版本的java。

如果您更喜欢XML输出,那么java_home也有一个用XML输出的-X选项。

 $ /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. 

inputterminal:

 which javac 

它应该告诉你类似的东西

 /usr/bin/javac 

只需键入javac 。 如果安装了,你会得到使用信息,否则它会问你是否想安装Java。

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

所以你可以通过下面的方法来检查java是否被安装:

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