Update & Cleanup Java JDK Version on macOS

From Bonus Bits
Jump to: navigation, search

Purpose

This article gives the steps to update version of the Java Development Kit (JDK) for CLI and some example JetBrains applications. By default when the little auto update window pops up and you say sure to upgrade my JDK version... it does not switch a lot of applications to use the newer version, nor does it clean up the older versions. As of macOS High Sierra and JDK 1.8.0 152.

List Current Version

java -version

Install Latest Version

  1. Download x86 and/or x64 JDK
    1. Java 7 SDK
      1. http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html
    2. Java 8 SDK
      1. http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
  2. Run installer

List Current Home Setting

/usr/libexec/java_home


Set Java Home Version

This should be completed by the installer, but it's a good idea to double check it was set. Also, this is how you can switch between versions if needed.

/System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/java_home -v 1.8.0_152 --exec javac -version


Set Environment Variables

To force the version used by some CLI apps and

General Java Vars

java_version=1.8.0_152
export JDK_HOME=$(/usr/libexec/java_home -v $java_version)
export JAVA_HOME=$(/usr/libexec/java_home -v $java_version)

Jetbrain IDE Examples

Jetbrains comes with and embedded JDK these days, but if you want to force it to use the version you've installed this is one part of the setup.

export IDEA_JDK=$(/usr/libexec/java_home -v $java_version)
export RUBYMINE_JDK=$(/usr/libexec/java_home -v $java_version)


Remove Older JDK Versions

  1. View all installed versions
    ls /Library/Java/JavaVirtualMachines
    
  2. Remove all but latest and macOS installed version
    sudo rm -rf /Library/Java/JavaVirtualMachines/1.6.0.jdk
    
    sudo rm -rf /Library/Java/JavaVirtualMachines/jdk1.8.0_74.jdk
    
    sudo rm -rf /Library/Java/JavaVirtualMachines/jdk1.8.0_101.jdk
    
    sudo rm -rf /Library/Java/JavaVirtualMachines/jdk1.8.0_112.jdk
    
    sudo rm -rf /Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk
    


Set Jetbrains JDK (Optional)

Rubymine

vim ~/Library/Preferences/RubyMine2017.2/rubymine.jdk
/Library/Java/JavaVirtualMachines/jdk1.8.0_152.jdk

IntelliJ IDEA

vim ~/Library/Preferences/IntelliJIdea2017.2/idea.jdk
/Library/Java/JavaVirtualMachines/jdk1.8.0_152.jdk