Related
Installing java 8 and sbt with brew and/or brew cask is clearly possible,
The problem I am hitting is that brew installs java 13 as sbt dependency,
And I do not know a general way to go back to java 8 as default, since there are so many ways potentially to opt between java version some of which i am aware include PATH, JAVA_HOME, ln -s, java_exec selector.
Another problem potentially is that brew install sbt installs brew install java which is 13,
But java 8 I install it via brew cask.
The TL is saying that Java 13 or 14 might be causing some issues e.g. tests fails, should I push for latest versions of OpenJdk, Scala, SBT, etc?
Indeed, SDK Man is an option.
It actually works with brew also but there need to be just a few manual steps: here they are:
Installing sbt, java8, jenv & configure the shell
We used brew to install sbt (which in its turn installs java 13), and brew cask to install openjdk java 8, like this:
brew install sbt
brew tap AdoptOpenJDK/openjdk
brew cask install adoptjava8
We will use jenv to manage the java versions, with the information from https://github.com/Homebrew/homebrew-core/issues/31390
Basically the next steps are:
brew install jenv
jenv init -
echo 'eval "$(jenv init -)"' >> ~/.bash_profile
echo 'eval "$(jenv init -)"' >> ~/.zprofile
jenv add <path-to-java8-Home-Dir>
Then restart your terminal and you should now be able to run sbt
Where at time of writing <path-to-java8-Home-Dir> is /Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home
Have a look at a utility like Sdk Man.
It is multi-platform and allows you to set any desired JDK as a default one.
As well as changing it to another provider/version when it will be required.
I want to install Eclipse in my Mac (Yosemite OS Installed). But there is an error popup :
I Check my java configuration and i have 1.7 instaled.
I made the update in System preference > Java
And i know that i have the installation of JAva 8 in my computer because when i type in my terminal this command:
/Library/Internet\ Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/bin/java -version
I have java 1.8
But in my Os use the version 1.7 and not 1.8 what i have to do?
And why it is so difficult to do that, why it is not enough to update via System > Preferences
Thanks for you Help!
I made this via homebrew and it work for me:
brew update
brew cask install java
It is now installed in java_home like the version 1.7
But until the first version is in use.
Now You just have to export to JAVA_HOME variable with this command:
export JAVA_HOME=`/usr/libexec/java_home -v '1.8*'`
And it work
Thanks!
The JavaAppletPlugin.plugin Java is just the JRE, you need the full JDK to run Eclipse (and other Java applications) on a Mac.
So download and install the Java 8 JDK from the official Oracle site
Try this on a terminal window
echo $JAVA_HOME
you might get something like /Library/Java/JavaVirtualMachines/jdk1.8.0_111.jdk/Contents/Home (<-- that's mine, java 8)
Yours might point to Java 7. You can change JAVA_HOME to point to your Java 8 installation by doing
export JAVA_HOME=<path-to-java8-installation>
And then it should work.
(btw: your installation dir for Java 8 looks rather odd. you might want to re-install so that it is on a path that looks like mine above)
Install homebrew
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
See Homebrew homepage here
Install java using homebrew
a. brew update
b. brew tap caskroom/cask
c. brew install brew-cask
d. brew cask install java
See full instructions at (tomysmile/brew-java-and-jenv.md)[https://gist.github.com/tomysmile/a9a7aee85ff73454bd57e198ad90e614]
Install jenv, a java environment manager
brew install jenv
Add jenv to your path - see jenv page above
Add java path using jenv
jenv add /Library/Java/JavaVirtualMachines/your-jdk-version/Contents/Home
Replace your-jdk-version with the version of jdk you have installed on your system
Restart eclipse and that's it
Java is an optional package on the latest versions of macOS.
Yet once installed it appears like the JAVA_HOME environment variable is not set properly.
With the Java optional package or Oracle JDK installed,
adding one of the following lines to your ~/.bash_profile file will set the environment variable accordingly.
export JAVA_HOME="$(/usr/libexec/java_home -v 1.6)"
or
export JAVA_HOME="$(/usr/libexec/java_home -v 1.7)"
or
export JAVA_HOME="$(/usr/libexec/java_home -v 1.8)"
or simply
export JAVA_HOME="$(/usr/libexec/java_home)"
Note: If you installed openjdk on mac using brew, run sudo ln -sfn /usr/local/opt/openjdk/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk.jdk for the above to work
Update: added -v flag based on Jilles van Gurp response.
/usr/libexec/java_home is not a directory but an executable. It outputs the currently configured JAVA_HOME and doesn't actually change it. That's what the Java Preferences app is for, which in my case seems broken and doesn't actually change the JVM correctly. It does list the 1.7 JVM but I can toggle/untoggle & drag and drop all I want there without actually changing the output of /usr/libexec/java_home.
Even after installing 1.7.0 u6 from Oracle on Lion and setting it as the default in the preferences, it still returned the apple 1.6 java home. The only fix that actually works for me is setting JAVA_HOME manually:
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.7.0_06.jdk/Contents/Home/
At least this way when run from the command line it will use 1.7. /usr/libexec/java_home still insists on 1.6.
Update: Understanding Java From Command Line on OSX has a better explanation on how this works.
export JAVA_HOME=`/usr/libexec/java_home -v 1.7`
is the way to do it. Note, updating this to 1.8 works just fine.
For me, Mountain Lion 10.8.2, the solution most voted does not work.
I installed jdk 1.7 via Oracle and maven from homebrew.
My solution is from the hadoop-env.sh file of hadoop which I installed from homebrew, too.
I add the below sentence in ~/.bash_profile, and it works.
export JAVA_HOME="$(/usr/libexec/java_home)"
This solution also works for OS X Yosemite with Java 1.8 installed from Oracle.
None of the above answers helped me. I suppose all the answers are for older OS X
For OS X Yosemite 10.10, follow these steps
Use your favorite text editor to open: ~/.bash_profile
//This command will open the file using vim
$ vim ~/.bash_profile
Add the following line in the file and save it ( : followed by "x" for vim):
export JAVA_HOME=$(/usr/libexec/java_home)
Then in the terminal type the following two commands to see output:
$ source ~/.bash_profile
$ echo $JAVA_HOME
In the second line, you are updating the contents of .bash_profile file.
Update for Java 9 and some neat aliases.
In .bash_profile:
export JAVA_HOME8=`/usr/libexec/java_home --version 1.8`
export JAVA_HOME9=`/usr/libexec/java_home --version 9`
Note, that for the latest version it is 9 and not 1.9.
Set active Java:
export JAVA_HOME=$JAVA_HOME8
export PATH=$JAVA_HOME/bin:$PATH
Some additional alias to switch between the different versions:
alias j8='export JAVA_HOME=$JAVA_HOME8; export PATH=$JAVA_HOME/bin:$PATH'
alias j9='export JAVA_HOME=$JAVA_HOME9; export PATH=$JAVA_HOME/bin:$PATH'
Test in terminal:
% j8
% 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)
% j9
% java -version
java version "9"
Java(TM) SE Runtime Environment (build 9+181)
Java HotSpot(TM) 64-Bit Server VM (build 9+181, mixed mode)
EDIT: Update for Java 10
export JAVA_HOME10=`/usr/libexec/java_home --version 10`
alias j10='export JAVA_HOME=$JAVA_HOME10; export PATH=$JAVA_HOME/bin:$PATH'
EDIT: Update for Java 11
export JAVA_HOME11=`/usr/libexec/java_home --version 11`
alias j11='export JAVA_HOME=$JAVA_HOME11; export PATH=$JAVA_HOME/bin:$PATH'
The above didn't work for me with Amazon's EC2 tools, because it expects bin/java etc. underneath JAVA_HOME. /System/Library/Frameworks/JavaVM.framework/Home did work.
For OS X you can do:
export JAVA_HOME=`/System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/java_home`
On Mac OS X Lion, to set visualgc to run, I used:
export JAVA_HOME=/System/Library/Frameworks/JavaVM.framework/Versions/1.6.0/Home
The following worked for me. I'm using ZSH on OSX Yosemite with Java 8 installed.
The following command /usr/libexec/java_home emits the path to JDK home:
/Library/Java/JavaVirtualMachines/jdk1.8.0_05.jdk/Contents/Home
In your ~/.zshrc,
export JAVA_HOME = "/Library/Java/JavaVirtualMachines/jdk1.8.0_05.jdk/Contents/Home"
for macOS Mojave 10.14.1 and JAVA 11.0.1
I set the profile as
export JAVA_HOME=$(/usr/libexec/java_home)
key in terminal this to confirm:
$JAVA_HOME/bin/java -version
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)
A better (more upgradable) way is to use the following:
/System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK/Home
This should work with AWS also since it has bin underneath Home
Newer Oracle JVMs such as 1.7.0_21-b12 seem to install here:
/Library/Internet\ Plug-Ins/JavaAppletPlugin.plugin/Contents/Home
My approach is:
.bashrc
export JAVA6_HOME=`/usr/libexec/java_home -v 1.6`
export JAVA7_HOME=`/usr/libexec/java_home -v 1.7`
export JAVA_HOME=$JAVA6_HOME
# -- optional
# export PATH=$JAVA_HOME/bin:$PATH
This makes it very easy to switch between J6 and J7
I Had to explicitly set it to the exact path on my Macbook air .
Steps followed:
try to echo $JAVA_HOME (if it's set it'll show the path), if not, try to search for it using sudo find /usr/ -name *jdk
Edit the Bash p with - sudo nano ~/.bash_profile
Add the exact path to JAVA Home (with the path from step 2 above)
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home
Save and exit
Check JAVA_Home using - echo $JAVA_HOME
I am running MACOS MOJAVE - 10.14.2 (18C54) on a Macbook Air with JAVA 8
OSX Yosemite, ZSH, and Java SE Runtime Environment 8, I had to:
$ sudo ln -s /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands /System/Library/Frameworks/JavaVM.framework/Versions/Current/bin
and in ~/.zshrc change JAVA_HOME to
export JAVA_HOME="/System/Library/Frameworks/JavaVM.framework/Versions/Current"
For Mac OS X 10.9 I installed the latest version of JRE from Oracle and then reset the JAVA_HOME to /Library/Java/JavaVirtualMachines/jdk1.7.0_45.jdk/Contents/Home.
I am sure there is a better way but this got me up and running.
hughsmac:~ hbrien$ echo $JAVA_HOME
/Library/Java/JavaVirtualMachines/jdk1.7.0_45.jdk/Contents/Home
For Java 11 (JDK 11) it can be located with the following command:
/usr/libexec/java_home -v 11
Got the same issue after I upgrade my Mac OS and following worked for me:
cmd>vi ~/.bash_profile
Add/update the line for JAVA_HOME:
export JAVA_HOME="/Library/Java/JavaVirtualMachines/jdk1.7.0_15.jdk/Contents/Home"
cmd>source ~/.bash_profile or open a new terminal
I think the jdk version might differ, so just use the version which you have under /Library/Java/JavaVirtualMachines/
If you are in need to have multiple versions of JDK under Mac OS X (Yosemite), it might be helpful to add some scripting for automated switching between them.
What you do is to edit your ~/.bash_profile and add the following:
function setjdk() {
if [ $# -ne 0 ]; then
removeFromPath '/System/Library/Frameworks/JavaVM.framework/Home/bin'
if [ -n "${JAVA_HOME+x}" ]; then
removeFromPath $JAVA_HOME
fi
export JAVA_HOME=`/usr/libexec/java_home -v $#`
export PATH=$JAVA_HOME/bin:$PATH
fi
}
function removeFromPath() {
export PATH=$(echo $PATH | sed -E -e "s;:$1;;" -e "s;$1:?;;")
}
setjdk 1.7
What the script does is to first remove other JDK versions in the PATH so that they won’t interfere with our new JDK version. Then it makes some clever use of /usr/libexec/java_home which is a command that lists installed JDK versions. The -v argument tells java_home to return the path of the JDK with the supplied version, for example 1.7. We also update the PATH to point to the bin directory of the newly found JAVA_HOME directory. At the end we can simply execute the function using
setjdk 1.7
which selects the latest installed JDK version of the 1.7 branch. To select a specific version you can simply execute
setjdk 1.7.0_51
instead. Run /usr/libexec/java_home -V to get more details on how to choose versions.
P.S. Do not forget to source ~/.bash_profile after you save it.
For Fish terminal users on Mac (I believe it's available on Linux as well), this should work:
set -Ux JAVA_8 (/usr/libexec/java_home --version 1.8)
set -Ux JAVA_12 (/usr/libexec/java_home --version 12)
set -Ux JAVA_HOME $JAVA_8 //or whichever version you want as default
This answer is related to Mountain Lion and not Lion. I needed to do this for the AWS Command Line Tools. According to the AWS docs, running which java returns /usr/bin/java.
So, I set JAVA_HOME=/usr in my .bashrc.
Apparently, /usr/bin/java is a symbolic link to /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/java which makes it all work.
Update
As mentioned in the comment below, this JAVA_HOME value is not an ideal solution when the JAVA_HOME environment variable is to be used by things other than the AWS Command Line Tools. It works fine for the AWS Command Line Tools, though, as given in their docs.
for mac user .
java 8 should add
export JAVA_HOME=`/usr/libexec/java_home -v 1.8`
# JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_05.jdk/Contents/Home
java 6 :
export JAVA_HOME=`/usr/libexec/java_home -v 1.6`
# JAVA_HOME=/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
ref :http://qiita.com/seri_k/items/e978c1339ce51f13e297
For Mac Yosemite,
JDK 1.7.0_xx is using
$ ls -ltar /usr/bin/java
/System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/java
JAVA_HOME
/Library/Java/JavaVirtualMachines/jdk1.7.0_xx.jdk/Contents/Home
Anyone using AUSKEY from the Australian Tax Office (ATO) should uninstall AUSKEY.
This sorted out my JAVA_HOME issues.
It is also no longer required for MAC users. Yah!
I'm using Fish shell on High Sierra 10.13.4 and installed Java via Brew.
It's not automatically set up so to set it correctly on my system I run:
set -U JAVA_HOME (/usr/libexec/java_home)
Just set java_home of 1.8 jdk version in netbeans.conf file:
/Applications/NetBeans/NetBeans 8.2.app/Contents/Resources/NetBeans/etc/netbeans.conf
uncomment line:
netbeans_jdkhome="path/to/jdk"
and set path to your 1.8 jdk, in my case:
netbeans_jdkhome="/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home"
This approach lays you to have several jdk versions on mac os
Handy command for knowing java home and other details.
java -XshowSettings:properties -version
and to know all java homes on your mac:
/usr/libexec/java_home -V
How can I install an additional java on MacOS? I installed jdk8 and that works fine. But now I need a jdk7 installation for development purposes. When trying to install the old version via DMG file, i get a warning, that there is already a newer version of java installed and the installer quits.
/usr/libexec/java_home -verbose
Matching Java Virtual Machines (1):
1.8.0_20, x86_64: "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_20.jdk/Contents/Home
/Library/Java/JavaVirtualMachines/jdk1.8.0_20.jdk/Contents/Home
How to install jdk7 in addition to this one?
The cleanest way to manage multiple java versions on Mac is to use Homebrew.
And within Homebrew, use:
homebrew-cask to install the versions of java
jenv to manage the installed versions of java
As seen on http://hanxue-it.blogspot.ch/2014/05/installing-java-8-managing-multiple.html , these are the steps to follow.
install homebrew
install homebrew jenv
install homebrew-cask
install a specific java version using cask (see "homebrew-cask versions" paragraph below)
add this version for jenv to manage it
check the version is correctly managed by jenv
repeat steps 4 to 6 for each version of java you need
homebrew-cask versions
Add the homebrew/cask-versions tap to homebrew using:
brew tap homebrew/cask-versions
Then you can look at all the versions available:
brew search java
Then you can install the version(s) you like:
brew install --cask java7
brew install --cask java6
And add them to be managed by jenv as usual.
jenv add <javaVersionPathHere>
I think this is the cleanest & simplest way to go about it.
Another important thing to note, as mentioned in Mac OS X 10.6.7 Java Path Current JDK confusing :
For different types of JDKs or installations, you will have different
paths
You can check the paths of the versions installed using /usr/libexec/java_home -V, see How do I check if the Java JDK is installed on Mac?
On Mac OS X Mavericks, I found as following:
Built-in JRE default: /Library/Internet\ Plug-Ins/JavaAppletPlugin.plugin/Contents/Home
JDKs downloaded from Apple: /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/
JDKs downloaded from Oracle: /Library/Java/JavaVirtualMachines/jdk1.8.0_11.jdk/Contents/Home
Resources
Removing Java 8 JDK from Mac
http://hanxue-it.blogspot.ch/2014/05/installing-java-8-managing-multiple.html
http://sourabhbajaj.com/mac-setup/index.html
http://brew.sh
https://github.com/Homebrew/homebrew/tree/master/share/doc/homebrew#readme
http://sourabhbajaj.com/mac-setup/Homebrew/README.html
"brew tap” explained https://github.com/Homebrew/homebrew/blob/master/share/doc/homebrew/brew-tap.md
“brew versions” explained Homebrew install specific version of formula? and also https://github.com/Homebrew/homebrew-versions
https://github.com/caskroom/homebrew-cask
“cask versions”, similar to “brew versions”, see https://github.com/caskroom/homebrew-versions and also https://github.com/caskroom/homebrew-cask/issues/9447
http://www.jenv.be
https://github.com/gcuisinier/jenv
Uninstall jdk8, install jdk7, then reinstall jdk8.
My approach to switching between them (in .profile) :
export JAVA_7_HOME=$(/usr/libexec/java_home -v1.7)
export JAVA_8_HOME=$(/usr/libexec/java_home -v1.8)
export JAVA_9_HOME=$(/usr/libexec/java_home -v9)
alias java7='export JAVA_HOME=$JAVA_7_HOME'
alias java8='export JAVA_HOME=$JAVA_8_HOME'
alias java9='export JAVA_HOME=$JAVA_9_HOME'
#default java8
export JAVA_HOME=$JAVA_8_HOME
Then you can simply type java7 or java8 in a terminal to switch versions.
(edit: updated to add Dylans improvement for Java 9)
For macOS Sierra 420
This guide was cobbled together from various sources (replies above as well as other posts), and works perfect.
0. If you haven't already, install homebrew.
See https://brew.sh/
1. Install jenv
brew install jenv
2. Add jenv to the bash profile
if which jenv > /dev/null; then eval "$(jenv init -)"; fi
3. Add jenv to your path
export PATH="$HOME/.jenv/shims:$PATH"
4. Tap "homebrew/cask-versions"
FYI: "Tap" extends brew's list of available repos it can install, above and beyond brew's default list of available repos.
brew tap homebrew/cask-versions
5. Install the latest version of java
brew install java --cask
6. Install java 6 (or 7 or 8 whatever you need)
brew install java6 --cask
#brew install java7 --cask
#brew install java8 --cask
? Maybe close and restart Terminal so it sees any new ENV vars that got setup.
7. Review Installations
All Java version get installed here: /Library/Java/JavaVirtualMachines lets take a look.
ls -la /Library/Java/JavaVirtualMachines
8. Add each path to jenv one-at-a-time.
We need to add "/Contents/Home" to the version folder.
WARNING: Use the actual paths on your machine... these are just EXAMPLE's
jenv add /Library/Java/JavaVirtualMachines/1.6.0___EXAMPLE___/Contents/Home
jenv add /Library/Java/JavaVirtualMachines/jdk-9.0.1.jdk___EXAMPLE___/Contents/Home
9. Check if jenv registered OK
jenv versions
10. Set java version to use (globably)
Where XX matches one of the items in the versions list above.
jenv global XX
Check java version
java -version
Check jenv versions
Should also indicate the current version being used with an asterisk.
jenv versions
DONE
Quick future reference
To change java versions
... See the list of available java versions
jenv versions
... then, where XX matches an item in the list above
jenv global XX
SDKMAN! is a great tool for using multiple versions of Java, Gradle, Groovy, Kotlin, and other JVM tools on Mac OS. Installation and usage doc are easily found on the main site.
(I have no affiliation, just a happy user).
As an example usage, if I type the following in a Terminal window, there is a list of available Java SDK versions (edited for brevity):
$ sdk list java
Available Java Versions
+ 9ea170
> + 8u131
7u141-zulu
Here + denotes that the version is installed. > denotes which version is currently in use. To install a version:
$ sdk install java 7u141-zulu
To use a version in this Terminal window:
$ sdk use java 9ea170
First, you need to make certain you have multiple JAVA versions installed.
Open a new Terminal window and input:
/usr/libexec/java_home -V
Your output should look like:
Matching Java Virtual Machines (2):
11.0.1, x86_64: "Java SE 11.0.1" /Library/Java/JavaVirtualMachines/jdk-11.0.1.jdk/Contents/Home
1.8.0_201, x86_64: "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_201.jdk/Contents/Home
Note that there are two JDKs available. If you don’t notice the Java version you need to switch to, download and install the appropriate one from here https://www.oracle.com/java/technologies/downloads/
(JDK 8 is represented as 1.8) .
Once you have installed the appropriate JDK, repeat this step.
Take note of the JDK version you want to switch to. For example, “11.0” and “1.8” are the JDK versions available in the example above.
Switch to the desired version. For example, if you wish to switch to JDK 8, input the following line:
export JAVA_HOME=/usr/libexec/java_home -v 1.8
For 11.0, switch “1.8” with “11.0”
4. Check your JDK version by inputting into Terminal:
java -version
If you have followed all the steps correctly, the JDK version should correlate with the one you specified in the last step.
5. (Optional) To make this the default JDK version, input the following in Terminal:
open ~/.bash_profile
Then, add your Terminal input from step 3 to this file:
SWITCH TO JAVA VERSION 8
export JAVA_HOME=`/usr/libexec/java_home -v 1.8`
Save and close the file.
As found on this website
So Let’s begin by installing jEnv
Run this in the terminal
brew install https://raw.github.com/gcuisinier/jenv/homebrew/jenv.rb
Add jEnv to the bash profile
if which jenv > /dev/null; then eval "$(jenv init -)"; fi
When you first install jEnv will not have any JDK associated with it.
For example, I just installed JDK 8 but jEnv does not know about it. To check Java versions on jEnv
At the moment it only found Java version(jre) on the system. The * shows the version currently selected. Unlike rvm and rbenv, jEnv cannot install JDK for you. You need to install JDK manually from Oracle website.
Install JDK 6 from Apple website. This will install Java in /System/Library/Java/JavaVirtualMachines/. The reason we are installing Java 6 from Apple website is that SUN did not come up with JDK 6 for MAC, so Apple created/modified its own deployment version.
Similarly install JDK7 and JDK8.
Add JDKs to jEnv.
JDK 6:
JDK 7:
JDK 8:
Check the java versions installed using jenv
So now we have 3 versions of Java on our system. To set a default version use the command
jenv local <jenv version>
Ex – I wanted Jdk 1.6 to start IntelliJ
jenv local oracle64-1.6.0.65
check the java version
java -version
That’s it. We now have multiple versions of java and we can switch between them easily. jEnv also has some other features, such as wrappers for Gradle, Ant, Maven, etc, and the ability to set JVM options globally or locally. Check out the documentation for more information.
In the same spirit than #Vegard (lightweight):
Install the wanted JDKs with Homebrew
Put this jdk bash function and a default in your .profile
jdk() {
version=$1
export JAVA_HOME=$(/usr/libexec/java_home -v"$version");
java -version
}
export JAVA_HOME=$(/usr/libexec/java_home -v11); # Your default version
and then, to switch your jdk, you can do
jdk 9
jdk 11
jdk 13
Based on https://github.com/AdoptOpenJDK/homebrew-openjdk.
Manage multiple java version in MAC using jenv
Install homebrew using following command
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
install jenv and activate jenv
brew install jenv
echo 'eval "$(jenv init -)"' >> ~/.bash_profile
tap cask-versions
brew tap homebrew/cask-versions
search available java version that can be installed
brew search java
E.g. to install java6 use following command
brew install cask java6
Add multiple versions of java in jenv
jenv add /Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home
jenv add /Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
Note:- if you get error like ln: /Users/<username>/.jenv/versions/oracle64-1.8.0.231: No such file or directory, then run following:-
mkdir -p /Users/<username>/.jenv/versions/oracle64-1.8.0.231
Rehash jenv after adding jdk’s
jenv rehash
List known versions of java to jenv
jenv versions
Set default version
jenv global oracle64-1.8.0.231
Change java version for a project
jenv local oracle64-1.6.0.65
set JAVA_HOME with the same version as jenv
jenv exec bash
echo $JAVA_HOME
I find this Java version manager called Jabba recently and the usage is very similar to version managers of other languages like rvm(ruby), nvm(node), pyenv(python), etc. Also it's cross platform so definitely it can be used on Mac.
After installation, it will create a dir in ~/.jabba to put all the Java versions you install. It "Supports installation of Oracle JDK (default) / Server JRE, Zulu OpenJDK (since 0.3.0), IBM SDK, Java Technology Edition (since 0.6.0) and from custom URLs.".
Basic usage is listed on their Github. A quick summary to start:
curl -sL https://github.com/shyiko/jabba/raw/master/install.sh | bash && . ~/.jabba/jabba.sh
# install Oracle JDK
jabba install 1.8 # "jabba use 1.8" will be called automatically
jabba install 1.7 # "jabba use 1.7" will be called automatically
# list all installed JDK's
jabba ls
# switch to a different version of JDK
jabba use 1.8
I am using Mac OS X 10.9.5. This is how I manage multiple JDK/JRE on my machine when I need one version to run application A and use another version for application B.
I created the following script after getting some help online.
#!bin/sh
function setjdk() {
if [ $# -ne 0 ]; then
removeFromPath '/Library/Java/JavaVirtualMachines/'
if [ -n "${JAVA_HOME+x}" ]; then
removeFromPath $JAVA_HOME
fi
export JAVA_HOME=/Library/Java/JavaVirtualMachines/$1/Contents/Home
export PATH=$JAVA_HOME/bin:$PATH
fi
}
function removeFromPath() {
export PATH=$(echo $PATH | sed -E -e "s;:$1;;" -e "s;$1:?;;")
}
#setjdk jdk1.8.0_60.jdk
setjdk jdk1.7.0_15.jdk
I put the above script in .profile file. Just open terminal, type vi .profile, append the script with the above snippet and save it. Once your out type source .profile, this will run your profile script without you having to restart the terminal. Now type java -version it should show 1.7 as your current version. If you intend to change it to 1.8 then comment the line setjdk jdk1.7.0_15.jdk and uncomment the line setjdk jdk1.8.0_60.jdk. Save the script and run it again with source command. I use this mechanism to manage multiple versions of JDK/JRE when I have to compile 2 different Maven projects which need different java versions.
Jenv on Mac Sierra:
if not working after install, do this bug fix to add java executable to path
export PATH="$HOME/.jenv/shims:$PATH"
even though eval "$(jenv init -)" could do this job. The reason is /bin folder is not there anymore as describe in it's homepage, but shim folder is used as /bin instead.
Make sure ~/.jenv is there
which java may print /Library/...
jenv global 1.8
jenv shell 1.8
Eventually, which java gives you:
/Users/xxxx/.jenv/shims/java
I answer lately and I really recommand you to use SDKMAN instead of Homebrew.
With SDKMAN you can install easily different version of JAVA in your mac and switch from on version to another.
You can also use SDKMAN for ANT, GRADLE, KOTLIN, MAVEN, SCALA, etc...
To install a version in your mac you can run the command sdk install java 15.0.0.j9-adpt
I know that this question already have a lot of answers, but I want to share my solution only using Temurin and bash.
AdoptOpenJdk is deprecated and that is why I'm using Temurin.
1. Download Temurin using the mirrors or a package manager
$ brew install --cask temurin
And for other versions (like Java 8):
$ brew tap homebrew/cask-versions
$ brew install --cask temurin8
2. Add a bash script into your bash profile
Open your ~/.zshrc or ~/.bash_profile (depending on what you're using it)
And add this code:
set-jdk() {
jdkversion=$1
export JAVA_HOME=/Library/Java/JavaVirtualMachines/temurin-"$jdkversion".jdk/Contents/Home;
export PATH=$PATH:$JAVA_HOME/bin;
java -version
}
3. Restart your terminal and change your Java version
$ set-jdk N N being the Java version that you want and it's already installed
e.g.
$ set-jdk 8 To change to Java 8 / 1.8
Few considerations:
It will only keep the Java version on the terminal session that you used the set-jdk command
It won't set the Java version as global
When you want to add the JAVA_HOME into another script/application, you can set /Library/Java/JavaVirtualMachines/temurin-<version>.jdk/Contents/Home (change the for the desired Java version. Like this JD-GUI example)
Enjoy
As of 2023, brew install --cask zulu8 didn't work for me on Apple chip. brew install openjdk#8 also didn't not. The only one solution worked for me was brew install --cask adoptopenjdk8 and then of course vim ~/.zshrc and inside:
export JAVAC_HOME=/Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home/
export JAVA_11_HOME=$(/usr/libexec/java_home -v11)
export JAVA_8_HOME=$(/usr/libexec/java_home -v1.8)
alias javac8='export JAVA_HOME=$JAVAC_HOME'
alias java11='export JAVA_HOME=$JAVA_11_HOME'
alias java8='export JAVA_HOME=$JAVA_8_HOME'
switch from terminal using javac8 to a freshly installed (different) version. Note: you can have different Java versions than me, so it's just an example.
Edit: for some reason it takes some time once it's activate. It seems to me a restart of a laptop is needed.
Here's a more DRY version for bash (Based on Vegard's answer)
Replace 1.7 and 1.8 with whatever versions you are interested with
and you'll get an alias called 'javaX'; where 'X' is the java version (7 / 8 in the snippet below) that will allow you to easily switch versions
for version in 1.7 1.8; do
v="${version: -1}"
h=JAVA_"$v"_HOME
export "$h"=$(/usr/libexec/java_home -v $version)
alias "java$v"="export JAVA_HOME=\$$h"
done
To install more recent versions of OpenJDK, I use this. Example for OpenJDK 14:
brew info adoptopenjdk
brew tap adoptopenjdk/openjdk
brew cask install adoptopenjdk14
See https://github.com/AdoptOpenJDK/homebrew-openjdk for current info.
I followed steps in below link - https://medium.com/#euedofia/fix-default-java-version-on-maven-on-mac-os-x-156cf5930078 and it worked for me.
cd /usr/local/Cellar/maven/3.5.4/bin/
nano mvn
--Update JAVA_HOME -> "${JAVA_HOME:-$(/usr/libexec/java_home)}"
mvn -version
New commands for installing Java via Homebrew:
brew cask install adoptopenjdk/openjdk/adoptopenjdk8
brew cask install adoptopenjdk/openjdk/adoptopenjdk11
See the homebrew-openjdk repo for the latest commands.
Installing Java
You can install Java via Homebrew, Jabba, SDKMAN or manually. See this answer for details on all the commands.
Switching Java versions*
You can switch Java versions with jenv Jabba, SDKMAN or manually. See details on all the switching commands here.
Best solutions
Jabba is designed to work on multiple platforms, so it's a good option if you want a solution that'll also work on Windows
Using Homebrew to download Java versions and jenv to switch versions provides a nice workflow. jenv makes it easy to work with Java versions stored in any directory on your machine, so it's a good alternative if you're interested in storing Java in non-default directories.
Using SDKMAN to download Javas and switch versions is another great alternative
Manually switching should be avoided because it's an unnecessary headache.
Function to manually switch Java versions
Here's the Bash / ZSH function for manually switching Java versions (by OpenJDK):
jdk() {
version=$1
export JAVA_HOME=$(/usr/libexec/java_home -v"$version");
java -version
}
There are great tools for switching Java versions, so I highly recommend against doing it manually.
Here is a great tutorial
https://medium.com/#chamikakasun/how-to-manage-multiple-java-version-in-macos-e5421345f6d0
by using jEnv installed by brew
or you can check out homebrew-openjdk
https://github.com/AdoptOpenJDK/homebrew-openjdk managing version using brew Tap
To find the available Java versions:
brew search java
To install the latest stable version (19 as of today):
brew info java
brew install java
For the system Java wrappers (eg: IDEs) to find the latest JDK, symlink it with:
sudo ln -sfn /opt/homebrew/opt/openjdk/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk.jdk
To install a specific version (11):
brew install java11
For the system, Java wrappers to find JDK 11:
sudo ln -sfn /opt/homebrew/opt/openjdk#11/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk-11.jdk
You can have one of the OpenJDK versions in your PATH. Add one of the following lines to your ~/.bash_profile
export PATH="/opt/homebrew/opt/openjdk/bin:$PATH
export PATH="/opt/homebrew/opt/openjdk#11/bin:$PATH
Then,
source ~/.bash_profile
I solved this error on my mac m1 air just by adding the path of new jdk version in ~/.zshrc file.
I have multiple jdks in my /Library/Java/JavaVirtualMachines path. So I can choose any version I wish to set as my default java path.
Java is an optional package on the latest versions of macOS.
Yet once installed it appears like the JAVA_HOME environment variable is not set properly.
With the Java optional package or Oracle JDK installed,
adding one of the following lines to your ~/.bash_profile file will set the environment variable accordingly.
export JAVA_HOME="$(/usr/libexec/java_home -v 1.6)"
or
export JAVA_HOME="$(/usr/libexec/java_home -v 1.7)"
or
export JAVA_HOME="$(/usr/libexec/java_home -v 1.8)"
or simply
export JAVA_HOME="$(/usr/libexec/java_home)"
Note: If you installed openjdk on mac using brew, run sudo ln -sfn /usr/local/opt/openjdk/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk.jdk for the above to work
Update: added -v flag based on Jilles van Gurp response.
/usr/libexec/java_home is not a directory but an executable. It outputs the currently configured JAVA_HOME and doesn't actually change it. That's what the Java Preferences app is for, which in my case seems broken and doesn't actually change the JVM correctly. It does list the 1.7 JVM but I can toggle/untoggle & drag and drop all I want there without actually changing the output of /usr/libexec/java_home.
Even after installing 1.7.0 u6 from Oracle on Lion and setting it as the default in the preferences, it still returned the apple 1.6 java home. The only fix that actually works for me is setting JAVA_HOME manually:
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.7.0_06.jdk/Contents/Home/
At least this way when run from the command line it will use 1.7. /usr/libexec/java_home still insists on 1.6.
Update: Understanding Java From Command Line on OSX has a better explanation on how this works.
export JAVA_HOME=`/usr/libexec/java_home -v 1.7`
is the way to do it. Note, updating this to 1.8 works just fine.
For me, Mountain Lion 10.8.2, the solution most voted does not work.
I installed jdk 1.7 via Oracle and maven from homebrew.
My solution is from the hadoop-env.sh file of hadoop which I installed from homebrew, too.
I add the below sentence in ~/.bash_profile, and it works.
export JAVA_HOME="$(/usr/libexec/java_home)"
This solution also works for OS X Yosemite with Java 1.8 installed from Oracle.
None of the above answers helped me. I suppose all the answers are for older OS X
For OS X Yosemite 10.10, follow these steps
Use your favorite text editor to open: ~/.bash_profile
//This command will open the file using vim
$ vim ~/.bash_profile
Add the following line in the file and save it ( : followed by "x" for vim):
export JAVA_HOME=$(/usr/libexec/java_home)
Then in the terminal type the following two commands to see output:
$ source ~/.bash_profile
$ echo $JAVA_HOME
In the second line, you are updating the contents of .bash_profile file.
Update for Java 9 and some neat aliases.
In .bash_profile:
export JAVA_HOME8=`/usr/libexec/java_home --version 1.8`
export JAVA_HOME9=`/usr/libexec/java_home --version 9`
Note, that for the latest version it is 9 and not 1.9.
Set active Java:
export JAVA_HOME=$JAVA_HOME8
export PATH=$JAVA_HOME/bin:$PATH
Some additional alias to switch between the different versions:
alias j8='export JAVA_HOME=$JAVA_HOME8; export PATH=$JAVA_HOME/bin:$PATH'
alias j9='export JAVA_HOME=$JAVA_HOME9; export PATH=$JAVA_HOME/bin:$PATH'
Test in terminal:
% j8
% 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)
% j9
% java -version
java version "9"
Java(TM) SE Runtime Environment (build 9+181)
Java HotSpot(TM) 64-Bit Server VM (build 9+181, mixed mode)
EDIT: Update for Java 10
export JAVA_HOME10=`/usr/libexec/java_home --version 10`
alias j10='export JAVA_HOME=$JAVA_HOME10; export PATH=$JAVA_HOME/bin:$PATH'
EDIT: Update for Java 11
export JAVA_HOME11=`/usr/libexec/java_home --version 11`
alias j11='export JAVA_HOME=$JAVA_HOME11; export PATH=$JAVA_HOME/bin:$PATH'
The above didn't work for me with Amazon's EC2 tools, because it expects bin/java etc. underneath JAVA_HOME. /System/Library/Frameworks/JavaVM.framework/Home did work.
For OS X you can do:
export JAVA_HOME=`/System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/java_home`
On Mac OS X Lion, to set visualgc to run, I used:
export JAVA_HOME=/System/Library/Frameworks/JavaVM.framework/Versions/1.6.0/Home
The following worked for me. I'm using ZSH on OSX Yosemite with Java 8 installed.
The following command /usr/libexec/java_home emits the path to JDK home:
/Library/Java/JavaVirtualMachines/jdk1.8.0_05.jdk/Contents/Home
In your ~/.zshrc,
export JAVA_HOME = "/Library/Java/JavaVirtualMachines/jdk1.8.0_05.jdk/Contents/Home"
for macOS Mojave 10.14.1 and JAVA 11.0.1
I set the profile as
export JAVA_HOME=$(/usr/libexec/java_home)
key in terminal this to confirm:
$JAVA_HOME/bin/java -version
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)
A better (more upgradable) way is to use the following:
/System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK/Home
This should work with AWS also since it has bin underneath Home
Newer Oracle JVMs such as 1.7.0_21-b12 seem to install here:
/Library/Internet\ Plug-Ins/JavaAppletPlugin.plugin/Contents/Home
My approach is:
.bashrc
export JAVA6_HOME=`/usr/libexec/java_home -v 1.6`
export JAVA7_HOME=`/usr/libexec/java_home -v 1.7`
export JAVA_HOME=$JAVA6_HOME
# -- optional
# export PATH=$JAVA_HOME/bin:$PATH
This makes it very easy to switch between J6 and J7
I Had to explicitly set it to the exact path on my Macbook air .
Steps followed:
try to echo $JAVA_HOME (if it's set it'll show the path), if not, try to search for it using sudo find /usr/ -name *jdk
Edit the Bash p with - sudo nano ~/.bash_profile
Add the exact path to JAVA Home (with the path from step 2 above)
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home
Save and exit
Check JAVA_Home using - echo $JAVA_HOME
I am running MACOS MOJAVE - 10.14.2 (18C54) on a Macbook Air with JAVA 8
OSX Yosemite, ZSH, and Java SE Runtime Environment 8, I had to:
$ sudo ln -s /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands /System/Library/Frameworks/JavaVM.framework/Versions/Current/bin
and in ~/.zshrc change JAVA_HOME to
export JAVA_HOME="/System/Library/Frameworks/JavaVM.framework/Versions/Current"
For Mac OS X 10.9 I installed the latest version of JRE from Oracle and then reset the JAVA_HOME to /Library/Java/JavaVirtualMachines/jdk1.7.0_45.jdk/Contents/Home.
I am sure there is a better way but this got me up and running.
hughsmac:~ hbrien$ echo $JAVA_HOME
/Library/Java/JavaVirtualMachines/jdk1.7.0_45.jdk/Contents/Home
For Java 11 (JDK 11) it can be located with the following command:
/usr/libexec/java_home -v 11
Got the same issue after I upgrade my Mac OS and following worked for me:
cmd>vi ~/.bash_profile
Add/update the line for JAVA_HOME:
export JAVA_HOME="/Library/Java/JavaVirtualMachines/jdk1.7.0_15.jdk/Contents/Home"
cmd>source ~/.bash_profile or open a new terminal
I think the jdk version might differ, so just use the version which you have under /Library/Java/JavaVirtualMachines/
If you are in need to have multiple versions of JDK under Mac OS X (Yosemite), it might be helpful to add some scripting for automated switching between them.
What you do is to edit your ~/.bash_profile and add the following:
function setjdk() {
if [ $# -ne 0 ]; then
removeFromPath '/System/Library/Frameworks/JavaVM.framework/Home/bin'
if [ -n "${JAVA_HOME+x}" ]; then
removeFromPath $JAVA_HOME
fi
export JAVA_HOME=`/usr/libexec/java_home -v $#`
export PATH=$JAVA_HOME/bin:$PATH
fi
}
function removeFromPath() {
export PATH=$(echo $PATH | sed -E -e "s;:$1;;" -e "s;$1:?;;")
}
setjdk 1.7
What the script does is to first remove other JDK versions in the PATH so that they won’t interfere with our new JDK version. Then it makes some clever use of /usr/libexec/java_home which is a command that lists installed JDK versions. The -v argument tells java_home to return the path of the JDK with the supplied version, for example 1.7. We also update the PATH to point to the bin directory of the newly found JAVA_HOME directory. At the end we can simply execute the function using
setjdk 1.7
which selects the latest installed JDK version of the 1.7 branch. To select a specific version you can simply execute
setjdk 1.7.0_51
instead. Run /usr/libexec/java_home -V to get more details on how to choose versions.
P.S. Do not forget to source ~/.bash_profile after you save it.
For Fish terminal users on Mac (I believe it's available on Linux as well), this should work:
set -Ux JAVA_8 (/usr/libexec/java_home --version 1.8)
set -Ux JAVA_12 (/usr/libexec/java_home --version 12)
set -Ux JAVA_HOME $JAVA_8 //or whichever version you want as default
This answer is related to Mountain Lion and not Lion. I needed to do this for the AWS Command Line Tools. According to the AWS docs, running which java returns /usr/bin/java.
So, I set JAVA_HOME=/usr in my .bashrc.
Apparently, /usr/bin/java is a symbolic link to /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/java which makes it all work.
Update
As mentioned in the comment below, this JAVA_HOME value is not an ideal solution when the JAVA_HOME environment variable is to be used by things other than the AWS Command Line Tools. It works fine for the AWS Command Line Tools, though, as given in their docs.
for mac user .
java 8 should add
export JAVA_HOME=`/usr/libexec/java_home -v 1.8`
# JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_05.jdk/Contents/Home
java 6 :
export JAVA_HOME=`/usr/libexec/java_home -v 1.6`
# JAVA_HOME=/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
ref :http://qiita.com/seri_k/items/e978c1339ce51f13e297
For Mac Yosemite,
JDK 1.7.0_xx is using
$ ls -ltar /usr/bin/java
/System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/java
JAVA_HOME
/Library/Java/JavaVirtualMachines/jdk1.7.0_xx.jdk/Contents/Home
Anyone using AUSKEY from the Australian Tax Office (ATO) should uninstall AUSKEY.
This sorted out my JAVA_HOME issues.
It is also no longer required for MAC users. Yah!
I'm using Fish shell on High Sierra 10.13.4 and installed Java via Brew.
It's not automatically set up so to set it correctly on my system I run:
set -U JAVA_HOME (/usr/libexec/java_home)
Just set java_home of 1.8 jdk version in netbeans.conf file:
/Applications/NetBeans/NetBeans 8.2.app/Contents/Resources/NetBeans/etc/netbeans.conf
uncomment line:
netbeans_jdkhome="path/to/jdk"
and set path to your 1.8 jdk, in my case:
netbeans_jdkhome="/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home"
This approach lays you to have several jdk versions on mac os
Handy command for knowing java home and other details.
java -XshowSettings:properties -version
and to know all java homes on your mac:
/usr/libexec/java_home -V