在~/.bash_profile中设置JAVA_HOME
export JAVA_HOME=$(/usr/libexec/java_home -v 1.8.0_91)
可以指定java具体版本。
下面命令可以列出已安装的JDK:
/usr/libexec/java_home -V
username@localhost:~$ /usr/libexec/java_home -h
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.
username@localhost:~$ /usr/libexec/java_home -V
Matching Java Virtual Machines (3):
1.8.0_91, x86_64: "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home
1.8.0_66, x86_64: "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_66.jdk/Contents/Home
1.7.0_79, x86_64: "Java SE 7" /Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home
/Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home
username@localhost:~$ /usr/libexec/java_home -v 1.8.0_91
/Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home
username@localhost:~$ /usr/libexec/java_home -v 1.7
/Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home
username@localhost:~$