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_profile
$vi ~/.bash_profile
~/.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
}
python
$source ~/.bash_profile
Do it with $ chjava [java version]
#Specify java10
$chjava 10.0
#Specify java8
$chjava 1.8
#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.
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