Reputation: 14155
How can you change the default version of Java on a mac?
Upvotes: 1403
Views: 1799749
Reputation: 467
A very simple way to change the default Java version on MacOS via HomeBrew:
Start with:
brew install openjdk@17
Once it completes. Run:
sudo ln -sfn /opt/homebrew/opt/openjdk@17/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk-17.jdk
Enter your machine's password and finally run the 2 commands one after another.
export JAVA_HOME=$(/usr/libexec/java_home -v 17) // Specify your own version
export PATH=$JAVA_HOME/bin:$PATH
Check Java Version:
java -version
You're good to go:
openjdk version "17.0.14" 2025-01-21
OpenJDK Runtime Environment Homebrew (build 17.0.14+0)
OpenJDK 64-Bit Server VM Homebrew (build 17.0.14+0, mixed mode, sharing)
Upvotes: 0
Reputation: 26100
First run /usr/libexec/java_home -V
which will output something like the following:
Matching Java Virtual Machines (3):
1.8.0_05, x86_64: "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_05.jdk/Contents/Home
1.6.0_65-b14-462, x86_64: "Java SE 6" /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
1.6.0_65-b14-462, i386: "Java SE 6" /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
/Library/Java/JavaVirtualMachines/jdk1.8.0_05.jdk/Contents/Home
Pick the version you want to be the default (1.6.0_65-b14-462
for arguments sake) then:
export JAVA_HOME=`/usr/libexec/java_home -v 1.6.0_65-b14-462`
or you can specify just the major version, like:
export JAVA_HOME=`/usr/libexec/java_home -v 1.8`
Now when you run java -version
you will see:
java version "1.6.0_65"
Java(TM) SE Runtime Environment (build 1.6.0_65-b14-462-11M4609)
Java HotSpot(TM) 64-Bit Server VM (build 20.65-b04-462, mixed mode)
Add the export JAVA_HOME…
line to your shell’s init file.
For Bash (as stated by antonyh):
export JAVA_HOME=$(/usr/libexec/java_home -v 1.8)
For Fish (as stated by ormurin)
set -x JAVA_HOME (/usr/libexec/java_home -d64 -v1.8)
Updating the .zshrc file should work:
nano ~/.zshrc
export JAVA_HOME=$(/usr/libexec/java_home -v 1.8.0)
Press CTRL+X to exit the editor Press Y to save your changes
source ~/.zshrc
echo $JAVA_HOME
java -version
Upvotes: 2533
Reputation: 1990
Due to multiple Homebrew, cast(Deprecation Notice AdoptOpenJDK/openjdk), and MacOS updates I'm posting this updated answer.
Homebrew tap AdoptOpenJDK/openjdk is officially deprecated in favor of the temurin casks provided directly from the Homebrew project but Homebrew is the best way to manage and work with different Java versions.
In case you already have Homebrew and AdoptOpenJDK/openjdk cast installed, please untap this brew tap first:
$ brew untap AdoptOpenJDK/openjdk
if you do not have Homebrew installed.
1 - Install Homebrew.
$ ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
2 - To Install the latest Java version with temurin:
$ brew install --cask temurin
3 - To install other versions:
$ brew tap homebrew/cask-versions
$ brew install --cask temurin@8
$ brew install --cask temurin@11
$ brew install --cask temurin@17
$ brew install --cask temurin@18
$ brew install --cask temurin@19
$ brew install --cask temurin@21
4 - Switch between different versions of Java
Switching between different versions of Java, you only need to add the following to your .bash_profile
or .zshrc
.
In this case, we want to be able to switch between Java8, Java11, Java17... and Java21:
export JAVA_8_HOME=$(/usr/libexec/java_home -v1.8)
export JAVA_11_HOME=$(/usr/libexec/java_home -v11)
export JAVA_17_HOME=$(/usr/libexec/java_home -v17)
export JAVA_18_HOME=$(/usr/libexec/java_home -v18)
export JAVA_19_HOME=$(/usr/libexec/java_home -v19)
export JAVA_21_HOME=$(/usr/libexec/java_home -v21)
alias java8='export JAVA_HOME=$JAVA_8_HOME'
alias java11='export JAVA_HOME=$JAVA_11_HOME'
alias java17='export JAVA_HOME=$JAVA_17_HOME'
alias java18='export JAVA_HOME=$JAVA_18_HOME'
alias java19='export JAVA_HOME=$JAVA_19_HOME'
alias java21='export JAVA_HOME=$JAVA_21_HOME'
# default to Java 21
java21
5 - Reload .bash_profile
or .zshrc
for the aliases to take effect:
$ source ~/.bash_profile
or
$ source ~/.zshrc
6 - Finally you can use the aliases to switch between different Java versions.
$ java8
$ java -version
java version "1.8.0_261"
Old Guide if you have old Homebrew and MacOS versions...
I will share my experiences with macOS Big Sur v11.4, the best way to deal with these problems is by installing Java using Homebrew:
1 - Install Homebrew.
$ ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
2 - Install Homebrew Cask.
$ brew tap homebrew/cask-versions
$ brew update
$ brew tap homebrew/cask
3 - Install the latest version of Java
$ brew cask install java
4 - Install the other needed versions of Java (Java8, Java11, Java13).
$ brew tap adoptopenjdk/openjdk
$ brew cask install adoptopenjdk8
$ brew cask install adoptopenjdk11
$ brew cask install adoptopenjdk13
$ brew cask install adoptopenjdk14
5 - Switch between different versions of Java
Switching between different versions of Java, you only need to add the following to your .bash_profile
or .zshrc
.
In this case, we want to be able to switch between Java8, Java11, Java13 and Java14:
export JAVA_8_HOME=$(/usr/libexec/java_home -v1.8)
export JAVA_11_HOME=$(/usr/libexec/java_home -v11)
export JAVA_13_HOME=$(/usr/libexec/java_home -v13)
export JAVA_14_HOME=$(/usr/libexec/java_home -v14)
alias java8='export JAVA_HOME=$JAVA_8_HOME'
alias java11='export JAVA_HOME=$JAVA_11_HOME'
alias java13='export JAVA_HOME=$JAVA_13_HOME'
alias java14='export JAVA_HOME=$JAVA_14_HOME'
# default to Java 14
java14
6 - Reload .bash_profile
or .zshrc
for the aliases to take effect:
$ source ~/.bash_profile
or
$ source ~/.zshrc
7 - Finally you can use the aliases to switch between different Java versions.
$ java8
$ java -version
java version "1.8.0_261"
For more info, you can see this post: How to Use Brew to Install Java on Mac
Upvotes: 83
Reputation: 438
I would suggest to go with mise - it can handle multiple co-existing versions of multiple languages. And best of all, whichever package manager that the rest of the team uses, mise can understand those "legacy" (ie language-specific) config files and work seamlessly.
Upvotes: 0
Reputation: 41
You will need to check your java_home, which can be in .bash.rc or .zshrc, both are in your area, example: /Users/your_user/here .
Open the file with open or vi in .bash.rc or .zshrc: Change java home to the folder where your jdk .
If you use asdf, it will probably be in this folder:
export JAVA_HOME=/Users/your_user/.asdf/installs/java/your_jdk
If using SDKMAN:
/Users/your_user/.sdkman/candidates/java/your_jdk
If you are using intellij, download the jdk for it, it is here:
/Users/your_user/Library/Java/JavaVirtualMachines/your_jdk/Contents/Home
Knowing where your jdk is, you save it in this path:
export JAVA_HOME=insert_path_here
After, do the command:
Source ~/.zshrc
To restart the terminal and that's it, close the terminal and do a java-version and you will see the chosen version. Don't forget that once you use intellij, you need to point out the jdk in the idea, in file/project_structure. And ready
Upvotes: 1
Reputation: 445
Run
/usr/libexec/java_home -V
you will get all the java version that you have
Example:-
Matching Java Virtual Machines (3):
17.0.7 (x86_64) "Amazon.com Inc." - "Amazon Corretto 17" /Library/Java/JavaVirtualMachines/amazon-corretto-17.jdk/Contents/Home
11.0.19 (x86_64) "Amazon.com Inc." - "Amazon Corretto 11" /Users/abhishek.khaiwale/Library/Java/JavaVirtualMachines/corretto-11.0.19/Contents/Home
11.0.19 (x86_64) "Amazon.com Inc." - "Amazon Corretto 11" /Library/Java/JavaVirtualMachines/amazon-corretto-11.jdk/Contents/Home
If you need java version 11.0.19
Run
export JAVA_HOME=`/usr/libexec/java_home -v 11.0.19`
Upvotes: 21
Reputation: 4516
I suggest to use sdkman to manage different version of java as suggested in previous answers. However, no one mentioned the easy way to switch. run below command to see what all version in installed. I have installed java 8 and java 17 using sdkman.
sdk list java
output
Oracle | | 20.0.2 | oracle | | 20.0.2-oracle
| | 20.0.1 | oracle | | 20.0.1-oracle
| >>> | 17.0.8 | oracle | installed | 17.0.8-oracle
Right now my default version is set to java 8. If i want to switch to java 17. Use below command for it.
sdk use java 17.0.8-oracle
Now you can see default version is java 17
% java -version
java version "17.0.8" 2023-07-18 LTS
Java(TM) SE Runtime Environment (build 17.0.8+9-LTS-211)
Java HotSpot(TM) 64-Bit Server VM (build 17.0.8+9-LTS-211, mixed mode, sharing)
Upvotes: 0
Reputation: 11
HI you can use this command
sudo update-alternatives --config java
then you can use the one you like with number
for example i have 3 java in my linux
so i pick 0 , 1 or etc
i hope help for others
Upvotes: -1
Reputation: 4903
Adding to the above answers, I put the following lines in my .bash_profile
(or .zshrc
for MacOS 10.15+) which makes it really convenient to switch (including @elektromin's comment for java 9):
alias j20="export JAVA_HOME=`/usr/libexec/java_home -v 20`; java -version"
alias j19="export JAVA_HOME=`/usr/libexec/java_home -v 19`; java -version"
alias j18="export JAVA_HOME=`/usr/libexec/java_home -v 18`; java -version"
alias j17="export JAVA_HOME=`/usr/libexec/java_home -v 17`; java -version"
alias j16="export JAVA_HOME=`/usr/libexec/java_home -v 16`; java -version"
alias j15="export JAVA_HOME=`/usr/libexec/java_home -v 15`; java -version"
alias j14="export JAVA_HOME=`/usr/libexec/java_home -v 14`; java -version"
alias j13="export JAVA_HOME=`/usr/libexec/java_home -v 13`; java -version"
alias j12="export JAVA_HOME=`/usr/libexec/java_home -v 12`; java -version"
alias j11="export JAVA_HOME=`/usr/libexec/java_home -v 11`; java -version"
alias j10="export JAVA_HOME=`/usr/libexec/java_home -v 10`; java -version"
alias j9="export JAVA_HOME=`/usr/libexec/java_home -v 9`; java -version"
alias j8="export JAVA_HOME=`/usr/libexec/java_home -v 1.8`; java -version"
alias j7="export JAVA_HOME=`/usr/libexec/java_home -v 1.7`; java -version"
After inserting, execute $ source .bash_profile
I can switch to Java 8 by typing the following:
$ j8
java version "1.8.0_102"
Java(TM) SE Runtime Environment (build 1.8.0_102-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.102-b14, mixed mode)
Upvotes: 437
Reputation: 37167
With no additional tools to install, an easy way to have a separately installed JDK recognized as a first class member by /usr/libexec/java_home -V
is to symlink it as follows:
sudo ln -s <path> /Library/Java/JavaVirtualMachines/jdk-[some-identifier].jdk
<path>
is expected to be a directory containing subdirectories Contents/Home/bin
etc.
A common use case is to register the JDK/JRE included with Android Studio:
The default location for the Java tools in recent versions of Android Studio on macOS is /Applications/Android\ Studio.app/Contents/jre
. We will use jdk-android-studio.jdk
as the identifier:
sudo ln -s /Applications/Android\ Studio.app/Contents/jre /Library/Java/JavaVirtualMachines/jdk-android-studio.jdk
Now, /usr/libexec/java_home -V
will list it under Matching Java Virtual Machines
:
$ /usr/libexec/java_home -V
Matching Java Virtual Machines (1):
11.0.13 (arm64) "JetBrains s.r.o." - "OpenJDK 11.0.13" /Applications/Android Studio.app/Contents/jre/Contents/Home
/Applications/Android Studio.app/Contents/jre/Contents/Home
Upvotes: 7
Reputation: 2892
Use jenv is an easy way. (Update 2022)
Install jenv: see Getting started
Install java with brew
brew install openjdk@11
ln -s /usr/local/opt/openjdk@11/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk-11.jdk
#other java
brew install openjdk@8
brew install openjdk@17
Add java to jenv
jenv add /Library/Java/JavaVirtualMachines/openjdk-11.jdk/Contents/Home
Use: refer to jenv
Upvotes: 55
Reputation: 673
Very simple answer:
/usr/libexec/java_home -V
git clone https://github.com/jenv/jenv.git ~/.jenv #Linux/macOS
OR
brew install jenv #macOS
echo 'export PATH="$HOME/.jenv/bin:$PATH"' >> ~/.zshrc
$ echo 'eval "$(jenv init -)"' >> ~/.zshrc
jenv add PATH_FROM_STEP_1
Upvotes: 1
Reputation: 1088
Check Java version: java -version
Switch between versions: https://devqa.io/brew-install-java/
open ~/.bash_profile
export JAVA_8_HOME=$(/usr/libexec/java_home -v1.8)
export JAVA_11_HOME=$(/usr/libexec/java_home -v11)
alias java8='export JAVA_HOME=$JAVA_8_HOME' alias java11='export JAVA_HOME=$JAVA_11_HOME'
source ~/.bash_profile
When we are switching to java11 or java8, java -version command is not showing the correct version.
In that case use mvn -version to see the correct java version is used for building the solution
Upvotes: 8
Reputation: 16299
Consider the following approach only to change the JDK for each and specific tab of your terminal (i.e: iTerm
).
Having in the /Library/Java/JavaVirtualMachines
path the two following jdks
openjdk8u275-b01
openjdk-11.0.9.1+1
And in the .bash_profile
file the following:
export JAVA_HOME=/Library/Java/JavaVirtualMachines/openjdk-11.0.9.1+1/Contents/Home
export PATH=$JAVA_HOME/bin:$PATH
If you open Iterm
- with the first Tab A - and the following commands are executed:
javac -version
javac 11.0.9.1
java -version
openjdk version "11.0.9.1" 2020-11-04
OpenJDK Runtime Environment AdoptOpenJDK (build 11.0.9.1+1)
OpenJDK 64-Bit Server VM AdoptOpenJDK (build 11.0.9.1+1, mixed mode)
The output is correct and expected
But if you open a second Tab B and you need override the default JDK then write in the terminal the following:
export JAVA_HOME=/Library/Java/JavaVirtualMachines/openjdk8u275-b01/Contents/Home/
export PATH=$JAVA_HOME/bin:$PATH
Then
javac -version
javac 1.8.0_275
java -version
openjdk version "1.8.0_275"
OpenJDK Runtime Environment (AdoptOpenJDK)(build 1.8.0_275-b01)
OpenJDK 64-Bit Server VM (AdoptOpenJDK)(build 25.275-b01, mixed mode)
It works fine. Of course if the Tab B is closed or you open a new Tab C all work according the .bash_profile
settings (therefore the default settings)
Upvotes: 19
Reputation: 16688
Add following in your ~/.bash_profile
and set the default java version accordingly.
export JAVA_8_HOME=$(/usr/libexec/java_home -v1.8)
export JAVA_11_HOME=$(/usr/libexec/java_home -v11)
alias java8='export JAVA_HOME=$JAVA_8_HOME'
alias java11='export JAVA_HOME=$JAVA_11_HOME'
# default to Java 8
java8
I am using macOS and have installed java using brew
.
Edit:
If you are not using bash
please update the correct shell file, example, if you are using zsh
then it will be ~/.zshrc
Upvotes: 12
Reputation: 2080
add this function to bashrc or zshrc, java-change [version]
to choose the JDK
# set and change java versions
function java-change() {
echo "----- old java version -----"
java -version
if [ $# -ne 0 ]; then
export JAVA_HOME=`/usr/libexec/java_home -v $@`
export PATH=$JAVA_HOME/bin:$PATH
fi
echo "----- new java version -----"
java -version
}
Upvotes: 2
Reputation: 5397
I'm using this fish function I wrote:
function javav
set min_version 8
set max_version 99
set java_home_cmd '/usr/libexec/java_home 2>/dev/null --failfast --version'
if ! test (eval $java_home_cmd $argv)
echo "Version not found"
return 1
end
for current_version in (seq $min_version $max_version)
set path_to_remove (eval $java_home_cmd $current_version)
if ! test -z $path_to_remove
echo 'Removing' $path_to_remove 'from PATH'
set PATH (string match --invert $path_to_remove/bin $PATH)
end
end
echo 'Setting up env for Java' $argv
set -x JAVA_HOME (eval $java_home_cmd $argv)
set PATH $JAVA_HOME/bin $PATH
end
It basically automates the step in this answer, similarly to this one, but also taking care of setting the PATH.
Just put it in .config/fish/functions/
and then use it like this:
javav 11 # Sets to Java 11
javav 16 # Sets to Java 16
Upvotes: -1
Reputation: 111
alias j='f(){ export JAVA_HOME=
/usr/libexec/java_home -v $1
};f'
$ source .zshrc
$ j 1.8
Explanation This is parameterised so you do not need to update the script like other solutions posted. If you do not have the JVM installed you are told. Sample cases below:
/Users/user/IDE/project $ j 1.8
/Users/user/IDE/project $ java -version
openjdk version "1.8.0_265"
OpenJDK Runtime Environment (AdoptOpenJDK)(build 1.8.0_265-b01)
OpenJDK 64-Bit Server VM (AdoptOpenJDK)(build 25.265-b01, mixed mode)
/Users/user/IDE/project $ j 13
/Users/user/IDE/project $ java -version
openjdk version "13.0.2" 2020-01-14
OpenJDK Runtime Environment (build 13.0.2+8)
OpenJDK 64-Bit Server VM (build 13.0.2+8, mixed mode, sharing)
/Users/user/IDE/project $ j 1.7
Unable to find any JVMs matching version "1.7".
Upvotes: 9
Reputation: 2037
Previously I used alias'es in .zshrc for easy switching between versions but today I use SDKMAN. SDKMAN can also handle setting default java for the system, and downloading and installing new java versions.
Once sdkman is installed you can then do commands similar to what is possible with the nvm tool for handling node versions.
sdk list java
will list the java versions available on your system as well as available online for installation including their identifier that you can use in the sdk use
, sdk default
and sdk install
commands.
e.g. to install Amazon Corretto 11.0.8 and ask if it should be the new default do this:
sdk install java 11.0.8-amzn
A feature I also use regularly is the .sdkmanrc
file.
If you place that in a directory on your machine and run the sdk env
command in the directory then you can configure tool versions used only in that directory. It is also possible to make sdkman switch tool versions automatically using the sdkman_auto_env=true
configuration.
sdkman also supports handling other tools for the JVM such as gradle, kotlin, maven and more.
For more information check out https://sdkman.io/usage#env
Upvotes: 6
Reputation: 341
You can add it to your .bash_profile to have the version set by default.
//Open bash profile
open ~/.bash_profile
export JAVA_HOME=`/usr/libexec/java_home -v 1.8`
// run bash profile
source ~/.bash_profile
Upvotes: 12
Reputation: 2794
Four easy steps using terminal for people who uses the default process.. :)
echo $JAVA_HOME
gives you current java home. For eg: /Library/Java/JavaVirtualMachines/jdk1.8.0_191.jdk/Contents/Home/
cd /Library/Java/JavaVirtualMachines/
will take you to the folder where u normally install jdks (It might be different for your machines)
ls
shows you available folders (normally it will have the version numbers, for eg: jdk1.8.0_191.jdk openjdk-11.0.2.jdk
)export JAVA_HOME='/Library/Java/JavaVirtualMachines/openjdk-11.0.2.jdk/Contents/Home'
will change the java home..Upvotes: 17
Reputation: 6504
macOS El Capitan or newer will choose the higher version of the JDK available in /Library/Java/JavaVirtualMachines
, so in order to downgrade you may rename the file Info.plist
to something else like Info.plist.disabled
so that the OS will choose a previous version.
Upvotes: 17
Reputation: 977
tl;dr
Add the line:
export JAVA_HOME='/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home'
to the file
~/.bash_profile
(replace jdk1.8.0_144.jdk with your downloaded version)
then source ~/.bash_profile
Upvotes: 34
Reputation: 2867
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
Upvotes: 31
Reputation: 1753
add following command to the ~/.zshenv
file
export JAVA_HOME=`/usr/libexec/java_home -v 1.8`
Upvotes: 10
Reputation: 24974
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
).
Upvotes: 2
Reputation: 20477
This answer is an attempt to address: how to control java version system-wide (not just in currently running shell) when several versions of JDK are installed for development purposes on macOS El Capitan or newer (Sierra, High Sierra, Mojave). As far as I can tell, none of the current answers do that (*).
As a developer, I use several JDKs, and I want to switch from one to the other easily. Usually I have the latest stable one for general use, and others for tests. But I don't want the system (e.g. when I start my IDE) to use the latest "early access" version I have for now. I want to control system's default, and that should be latest stable.
The following approach works with Java 7 to 12 at least (early access at the time of this writing), with Oracle JDK or OpenJDK (including builds by AdoptOpenJDK produced after mid-October 2018).
/Library/Java/JavaVirtualMachines
. The system will pick the highest version by default. Contents/Info.plist
to Info.plist.disabled
. That JDK can still be used when $JAVA_HOME
points to it, or explicitly referenced in a script or configuration. It will simply be ignored by system's java
command.System launcher will use the JDK with highest version among those that have an Info.plist
file.
When working in a shell with alternate JDK, pick your method among existing answers (jenv
, or custom aliases/scripts around /usr/libexec/java_home
, etc).
Details of investigation in this gist.
(*) Current answers are either obsolete (no longer valid for macOS El Capitan or Sierra), or only address a single JDK, or do not address the system-wide aspect. Many explain how to change $JAVA_HOME
, but this only affects the current shell and what is launched from there. It won't affect an application started from OS launcher (unless you change the right file and logout/login, which is tedious). Same for jenv, it's cool and all, but as far as I can tell it merely changes environment variables, so it has the same limitation.
Upvotes: 601
Reputation: 1188
First find out where do you store the environment variables-
Steps to Set up the environment variable :-
Download the jdk from JAVA
install it by double click
Now set-up environment variables in your file
a. For emacs.profile you can use this link OR see the screenshot below
b. For ZSH profile setup -
1. export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_112.jdk/Contents/Home
2. source ~/.zshrc - Restart zshrc to reflect the changes.
3. echo $JAVA_HOME - make sure path is set up properly
----> /Library/Java/JavaVirtualMachines/jdk1.8.0_112.jdk/Contents/Home
4. java -version
--> java version "1.8.0_112" Java(TM) SE Runtime Environment (build 1.8.0_112-b16)Java HotSpot(TM) 64-Bit Server VM (build 25.112-b16, mixed mode)
All set Now you can easily upgrade or degrade the JAVA version..
Upvotes: 6
Reputation: 1341
If still u are not able to set it. using this command.
export JAVA_HOME=/usr/libexec/java_home -v 1.8
then you have to use this one.
export JAVA_HOME=$(/usr/libexec/java_home -v 1.8)
it will surely work.
Upvotes: 6
Reputation: 5947
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
Upvotes: 4