Change the default Java Version on macOS

Here, We are trying to change the version to Java SE 8

First run /usr/libexec/java_home -V which will output something like the following:

Matching Java Virtual Machines (3):
    16.0.2 (x86_64) "Oracle Corporation" - "Java SE 16.0.2" /Library/Java/JavaVirtualMachines/jdk-16.0.2.jdk/Contents/Home
    1.8.202.08 (x86_64) "Oracle Corporation" - "Java" /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home
    1.8.0_202 (x86_64) "Oracle Corporation" - "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_202.jdk/Contents/Home

/Library/Java/JavaVirtualMachines/jdk-16.0.2.jdk/Contents/Home

Enter fullscreen mode Exit fullscreen mode

Pick the version you want to be the default (1.8.0_202) then:

export JAVA_HOME=`/usr/libexec/java_home -v 1.8.0_202`

Enter fullscreen mode Exit fullscreen mode

Now when you run java -version you will see:

java version "1.8.0_202"
Java(TM) SE Runtime Environment (build 1.8.0_202-b08)
Java HotSpot(TM) 64-Bit Server VM (build 25.202-b08, mixed mode)

Enter fullscreen mode Exit fullscreen mode

Add the

export JAVA_HOME=`/usr/libexec/java_home -v 1.8.0_202`

Enter fullscreen mode Exit fullscreen mode

line to your shell’s init file.

Updating the .zshrc file should work:

nano ~/.zshrc

Enter fullscreen mode Exit fullscreen mode

paste the line

export JAVA_HOME=$(/usr/libexec/java_home -v 1.8.0_202)

Enter fullscreen mode Exit fullscreen mode

at bottom of the file.

Press CTRL+X to exit the editor, Press Y to save your changes.

source ~/.zshrc
echo $JAVA_HOME
java -version

Enter fullscreen mode Exit fullscreen mode

Output should be:

java version "1.8.0_202"
Java(TM) SE Runtime Environment (build 1.8.0_202-b08)
Java HotSpot(TM) 64-Bit Server VM (build 25.202-b08, mixed mode)

Enter fullscreen mode Exit fullscreen mode

Resources

Stackoverflow

原文链接:Change the default Java Version on macOS

© 版权声明
THE END
喜欢就支持一下吧
点赞13 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容