How can you change the default version of Java on a mac?
If you are using fish and you are using mac and you want to be able to switch between JDK's, then below works for me on mac.
@kenglxn's answer didn't work for me and I figured out it bcos didn't set -g which is global !
Put below under ~/.config/fish/config.fish
alias j8="jhome -v 1.8.0_162"
alias j9="jhome -v 9.0.1"
function jhome
set -g -x JAVA_HOME (/usr/libexec/java_home $argv)
echo "JAVA_HOME:" $JAVA_HOME
echo "java -version:"
java -version
end
funcsave jhome
To know which version /minor version you have installed, you can do :
/usr/libexec/java_home -V 579ms Wed 14 Feb 11:44:01 2018
Matching Java Virtual Machines (3):
9.0.1, x86_64: "Java SE 9.0.1" /Library/Java/JavaVirtualMachines/jdk-9.0.1.jdk/Contents/Home
1.8.0_162, x86_64: "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_162.jdk/Contents/Home
1.8.0_121, x86_64: "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home
Here is how I do it on my Linux (Ubuntu / Mint mate), I guess Mac can do it similarly.
Steps:
/mnt/star/program/java/jdk-1.8
ln -s /mnt/star/program/java/jdk-1.8 /mnt/star/program/java/java
/mnt/star/program/java/java
is the soft link.JAVA_HOME
in a start script./etc/profile.d/eric.sh
, or just use ~/.bashrc
.JAVA_HOME=/mnt/star/program/java/java
java -version
should print the java version./mnt/star/program/java/jdk-11
~/.bashrc
, define variable for various Java version._E_JAVA_HOME_11='/mnt/star/program/java/jdk-11'
_E_JAVA_HOME_8='/mnt/star/program/java/jdk-8'
# dir of default version,
_E_JAVA_HOME_D=$_E_JAVA_HOME_8
~/.bashrc
, define command to switch Java version.## switch java version,
alias jv11="rm $JAVA_HOME; ln -s $_E_JAVA_HOME_11 $JAVA_HOME"
alias jv8="rm $JAVA_HOME; ln -s $_E_JAVA_HOME_8 $JAVA_HOME"
# default java version,
alias jvd="rm $JAVA_HOME; ln -s $_E_JAVA_HOME_D $JAVA_HOME"
alias jv="java -version"
source ~/.bashrc
to make the changes take effect.Commands:
jv11
jv8
jvd
_E_JAVA_HOME_D
defined above.jv
Example output:
eric@eric-pc:~$ jv
java version "1.8.0_191"
Java(TM) SE Runtime Environment (build 1.8.0_191-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.191-b12, mixed mode)
eric@eric-pc:~$ jv11
eric@eric-pc:~$ jv
java version "11.0.1" 2018-10-16 LTS
Java(TM) SE Runtime Environment 18.9 (build 11.0.1+13-LTS)
Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.1+13-LTS, mixed mode)
eric@eric-pc:~$ jvd
eric@eric-pc:~$ jv
java version "1.8.0_191"
Java(TM) SE Runtime Environment (build 1.8.0_191-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.191-b12, mixed mode)
eric@eric-pc:~$
JAVA_HOME
.On my machine when install jdk by hand, I keep the minor version, then make a soft link with the major version but without the minor version.
e.g
// this is the actual dir,
jdk1.8.0_191
// this is a soft link to jdk1.8.0_191
jdk-8
// this is a soft link to jdk-8
or jdk-11
java
I define command alias in ~/.bashrc
, but define variable in a separate file.
I am using ~/.eric_var
to define the variables, and ~/.bashrc
will source it (e.g source $HOME/.eric_var
).
It is a little bit tricky, but try to follow the steps described in Installing Java on OS X 10.9 (Mavericks). Basically, you gonna have to update your alias to java
.
Step by step:
After installing JDK 1.7, you will need to do the sudo ln -snf
in order to change the link to current java
. To do so, open Terminal and issue the command:
sudo ln -nsf /Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents \
/System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK
Note that the directory jdk1.7.0_51.jdk
may change depending on the SDK version you have installed.
Now, you need to set JAVA_HOME
to point to where jdk_1.7.0_xx.jdk
was installed. Open again the Terminal and type:
export JAVA_HOME="/Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home"
You can add the export JAVA_HOME
line above in your .bashrc
file to have java permanently in your Terminal
An easy way to include a separately installed JDK in the list given by /usr/libexec/java_home -V
is to symlink the directory as follows:
sudo ln -s path/to/jdk /Library/Java/JavaVirtualMachines/jdk-[some-identifier].jdk
For example, to register the JDK included with Android Studio (1.8.0_152 at the time of writing), use:
sudo ln -s /Applications/Android\ Studio.app/Contents/jre/jdk /Library/Java/JavaVirtualMachines/jdk-android-studio.jdk
Now java_home
will list the JDK under Matching Java Virtual Machines
and you can select it as described above.
install JDK, not just JRE
/usr/libexec/java_home -v 1.8
gives
/Library/Java/JavaVirtualMachines/jdk1.8.0_111.jdk/Contents/Home
next
touch .bash_profile
open -a TextEdit.app .bash_profile
TextEdit will show you a blank page which you can fill in.
add to doc:
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_111.jdk/Contents/Home
in terminal:
export JAVA_HOME="$(/usr/libexec/java_home -v 1.8)"
try the command:
javac -version
should output:
javac 1.8.0_111
Use jenv, it is like a Java environment manager. It is super easy to use and clean
For Mac, follow the steps:
brew install jenv
git clone https://github.com/gcuisinier/jenv.git ~/.jenv
Installation: If you are using bash follow these steps:
$ echo 'export PATH="$HOME/.jenv/bin:$PATH"' >> ~/.bash_profile
echo 'eval "$(jenv init -)"' >> ~/.bash_profile
$ exec $SHELL -l
Add desired versions of JVM to jenv:
jenv add /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
jenv add /System/Library/Java/JavaVirtualMachines/1.8.0.jdk/Contents/Home
Check the installed versions:
jenv versions
Set the Java version you want to use by:
jenv global oracle64-1.6.0