Simplify java version switching (Mac)

Switching java verison was a bit annoying, so I made it a bash function so that it can be used like a command.

reference: http://shinsuke789.hatenablog.jp/entry/2017/08/16/103000 https://qiita.com/ponsuke0531/items/795cd00d93b71c14d07b

bash additions

  1. Edit ~ / .bash_profile
$vi ~/.bash_profile
  1. Add the following

~/.bash_profile


#!/bin/bash

#PATH initialization
export PATH=/sbin:/bin:/usr/bin:/usr/local/bin:/usr/sbin

#~~~usually~/.bash_Processing done in profile~~~
# ~/.use-java-version
if [ -f ~/.use-java-version ]; then
        export JAVA_HOME=$(cat ~/.use-java-version)
else
        #I default to java8
        /usr/libexec/java_home -v 1.8 | tee ~/.use-java-version
        export JAVA_HOME=$(cat ~/.use-java-version)
fi
export PATH=$JAVA_HOME/bin:$PATH

function lsjava () {
        #Output installed java
        echo -e "\033[0;33mInstalled java version:\033[0;39m"
        /usr/libexec/java_home -V
        #Output java currently in use
        echo -e "\n\033[0;33mCurrent specificed java version:\033[0;39m"
        java -version
}
function chjava () {
        #If no parameters are specified
        if [ -z "$1" ]; then
                echo -e "Please specific java version. \nex)\n\$ chjava 1.8"
                return 1
        fi
        /usr/libexec/java_home -v $1
        local checkJavaVersion=$?
        #If the specified java version is not installed
        if [ $checkJavaVersion -ne 0 ]; then
                echo "Please specific installed java version."
                echo Java $1 is not installed.
                return 2
        fi
        echo Use java $1.
        /usr/libexec/java_home -v $1 | tee ~/.use-java-version
        # ~/.use-java-Since the java path used for version is output, the upper if[ -f ~/.use-java-version ]By the way, it will be added to the PATH
        source ~/.bash_profile
        #Show PATH
        echo PATH=$PATH
                #Show java version in use
        java -version
        return 0
}
  1. Load the modified bash_profile.

python


$source ~/.bash_profile

function

java version switching

Do it with $ chjava [java version]

#Specify java10
$chjava 10.0

#Specify java8
$chjava 1.8

Error handling

#If you forget the parameters
$chjava
Please specific java version.
ex)
$ chjava 1.8

#If a java version that is not installed is specified
$chjava 1.9
Unable to find any JVMs matching version "1.9".
/Library/Java/JavaVirtualMachines/jdk-10.0.1.jdk/Contents/Home
Please specific installed java version.
Java 1.9 is not installed.

Currently installed java version & version in use

Do it with $ lsjava

$lsjava
Installed java version:
Matching Java Virtual Machines (3):
    10.0.1, x86_64:	"Java SE 10.0.1"	/Library/Java/JavaVirtualMachines/jdk-10.0.1.jdk/Contents/Home
    1.8.0_121, x86_64:	"Java SE 8"	/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home
    1.8.0_101, x86_64:	"Java SE 8"	/Library/Java/JavaVirtualMachines/jdk1.8.0_101.jdk/Contents/Home

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

Current specificed java version:
java version "1.8.0_121"
Java(TM) SE Runtime Environment (build 1.8.0_121-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.121-b13, mixed mode)

Recommended Posts

Simplify java version switching (Mac)
Switching java version (memorial)
Learning Java framework # 1 (Mac version)
Java version check
Download and install Eclipse (Java) (Mac version)
Java installation (Mac, Homebrew, multiple version control)
[Java] Convert PDF version
Java version notation comparison
Mac Java Home Directory
Java version control on macOS
Java version 8 and later features
How to lower java version
Ruby version switching with rbenv
How to uninstall Java 8 (Mac)
Version control Java with SDKMAN
Java version control with jenv
Java development environment (Mac, Eclipse)
Java version change on CentOS
Java installation location for mac
How to switch Java version with direnv in terminal on Mac
Java version notation that changes in Java 10
[Java version] The story of serialization
Ruby environment construction summary ~ mac version ~
[OpenCV3.2.0] Eclipse (Java) settings (for Mac)
Build Java development environment (for Mac)
Initial introduction to Mac (Java engineer)
[Java & SpringBoot] Environment Construction for Mac
Java development environment (Mac, VS Code)
Install Java development environment on Mac