如何查看Mac OS X系统上已安装的全部Java/JDK版本?
Hey there! Let's break down how to check all installed Java/JDK versions on your Mac and fix the javac -version limitation you're seeing.
The easiest way to list every Java runtime (including both JREs and JDKs) installed on your system is using Apple's built-in java_home tool. Just run this command in Terminal:
/usr/libexec/java_home -V
This will output a list of all available Java versions, along with their installation paths. The uppercase -V flag triggers verbose mode, forcing the tool to show every installed entry instead of just the default one.
javac -version仅显示当前版本的问题 First, let's clarify why javac -version only shows one version: it pulls the javac executable from whatever directory is first in your PATH environment variable (which you've configured in .bashrc). To see all installed JDKs, you can check the standard Mac JDK installation directory directly:
ls /Library/Java/JavaVirtualMachines/
Each folder in this directory corresponds to a separate JDK installation (e.g., jdk1.8.0_301.jdk, openjdk-11.jdk).
If you want to check the javac version for a specific JDK without switching your entire environment, you can run the javac executable directly from its installation path. For example:
/Library/Java/JavaVirtualMachines/jdk1.8.0_301.jdk/Contents/Home/bin/javac -version
To switch which version javac uses by default (so javac -version shows the one you want), you need to update your JAVA_HOME and PATH variables:
- First, get the path of your target JDK using
java_homewith a version flag. For Java 11, that would be:/usr/libexec/java_home -v 11 - Update your environment variables in
.bashrc(or.zshrcif you use Zsh):export JAVA_HOME=$(/usr/libexec/java_home -v 11) export PATH=$JAVA_HOME/bin:$PATH - Reload your shell configuration to apply the changes immediately:
source ~/.bashrc
Now running javac -version should show the version you specified.
For even quicker switching between versions, you can add aliases to your shell config. For example:
alias jdk8='export JAVA_HOME=$(/usr/libexec/java_home -v 1.8); export PATH=$JAVA_HOME/bin:$PATH' alias jdk11='export JAVA_HOME=$(/usr/libexec/java_home -v 11); export PATH=$JAVA_HOME/bin:$PATH'
Just type jdk8 or jdk11 in Terminal, and your javac (and java) commands will switch to that version instantly.
内容的提问来源于stack exchange,提问作者user14190216




