How can you change the default version of Java on a mac?
-
4what does executing `/usr/libexec/java_home` tell you? – Bart Feb 23 '14 at 06:00
-
2/usr/libexec/java_home returns /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home – Venkat Feb 23 '14 at 06:01
-
Could be you just need to reboot. Have you tried that? – Bart Feb 23 '14 at 06:04
-
1Yep restarted the computer and the terminal... no change. – Venkat Feb 23 '14 at 06:55
-
9Could you please accept the highly voted answer from @markhellewell below? It would have helped me find it a bit faster, and it's just nice. :) – Taytay Nov 16 '16 at 01:10
-
@Venkat It's now almost 7 years since the question, did any of the answers solve the problem? ;) As Taytay mentioned, would be nice to have an accepted answer for such a popular question. – simon Feb 09 '21 at 12:46
-
Agreed. Please (try to) accept an answer. This still shows up as a "unanswered question" in SOF queries. – granadaCoder Oct 03 '22 at 12:10
36 Answers
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

- 3
- 1

- 24,390
- 1
- 21
- 21
-
2@MattO'Brien it’s something which depends on the shell you’re using, and the configuration you have in place, so I can’t give a complete solution for that… – markhellewell Nov 14 '14 at 02:25
-
17
-
11This did not work for me. Grab the path from running java_home -V and add this to the export command, like this export JAVA_HOME="/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home" – oden Nov 28 '14 at 01:04
-
@oden it does work as long as your “java_home” executable isn’t broken. Also, as pointed out, the backticks are required (they are replaced by the result of the expression contained within them) – markhellewell Nov 30 '14 at 03:16
-
82This works in Bash - `export JAVA_HOME=$(/usr/libexec/java_home -v 1.8)` - also note, java_home allows partial matches so you don't have to include the full version as it'll find the latest installed. This makes updates easier. – antonyh Dec 24 '14 at 15:28
-
20
-
With proper setup, the response from `java_home` decides which `java` command gets executed. However, since `java_home` doesn't depend on `JAVA_HOME`, setting it will not change anything about the response from `java -version`. If you have in mind *some* mechanism which connects `JAVA_HOME` with the `java` command, your answer does not mention it. – Marko Topolnik Mar 03 '15 at 06:11
-
1@MarkoTopolnik I can’t tell if you are trolling. It is *most certainly* possible to tell the `java` command which version of Java you want to run by setting JAVA_HOME. The key thing here is that JAVA_HOME is being set by the output of the `java_home -v …` command. Backticks replace the shell command contained within them with the result of that command. Consider the following: JAVA_HOME=`java_home -v 1.8` becomes, after evaluation JAVA_HOME=/path/to/java1.8/home – markhellewell Mar 04 '15 at 10:34
-
Hm, I stand corrected. `/System/Library/Frameworks/JavaVM.framework/Commands/java` does honor `JAVA_HOME`, if present. If not, or if invalid, it relies on a direct call to `/usr/libexec/java_home`. – Marko Topolnik Mar 04 '15 at 12:06
-
2As a note to the future reader, this mechanism works only if you have installed Apple's Java before installing Oracle's, and that seems to be an ever less common case. – Marko Topolnik Mar 04 '15 at 17:15
-
-
Another option is to uninstall the java version you don't want (if its default) :) – rogerdpack May 18 '16 at 17:12
-
`/usr/libexec/java_home -V` is giving: Matching Java Virtual Machines (2): 1.8.0_121-zulu-8.20.0.5, x86_64: "Zulu 8" /Library/Java/JavaVirtualMachines/zulu-8.jdk/Contents/Home 1.8.0_121, x86_64: "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home `export JAVA_HOME=$(/usr/libexec/java_home -v 1.8.0_121)` In this case, won’t help much, because it appears to select the first that starts with 1.8.0_121 ?? Any ideas how to handle this case? – dexter Mar 24 '17 at 12:41
-
2018 and still this works but only for the current running shell, for system-wide setting please see the answer below or https://stackoverflow.com/a/44169445/5237164 – Chester M Oct 25 '18 at 05:02
-
10I wish I could upvote this every time I have to google "os x switch java version" and get back here – Raibaz Dec 19 '18 at 16:14
-
3Doesn't work. I ran the exact same steps and I still see jdk 8 instead of `9.0.1`. – Adam Arold Jan 23 '19 at 12:16
-
this isn't working for me anymore. it fixes just JAVA_HOME but maven or other programs are still using my other java version. combined with this solution, i came to a new idea https://stackoverflow.com/questions/17885494/how-can-i-change-mac-oss-default-java-vm-returned-from-usr-libexec-java-home/55763491#55763491 – Yuna Braska Apr 19 '19 at 14:39
-
Just an enhancement for this: add this fun to bash_profle `function jc() {export JAVA_HOME=$(/usr/libexec/java_home -v 1.8)}`.... Then, invoke from terminal: jc 1.8 or jc 12.0.1 this will change the java version – sujay May 13 '19 at 22:11
-
If you are using a fish terminal then you can use the following command: `set -x JAVA_HOME (/usr/libexec/java_home -d64 -v1.8)` or better yet add it to your .config/fish/config.fish file – ormurin May 14 '20 at 10:04
-
1Thanks, this is working for me. However, I have to do it every time I restart my terminal. Any suggestion on how to make the change permanent? – Julian May 23 '20 at 10:30
-
-
Where does $JAVA_HOME comes from ? overall the answer works, but I don't get what it does. When this variable is set what happens? I suspect that when java process is starting it checks for this variable, and if it is present, it starts the java with the appropriate version, am I righ ? – mykhailoklym94 Oct 07 '20 at 10:20
-
This work for me, i change from java 11 to java 8 in a downgrade... – Anthony Piñero May 26 '21 at 02:09
-
Couldn't find any other solution but this worked like a charm, thanks! :D – Akanshi Srivastava Aug 23 '21 at 06:43
-
1What do you do when `java -version` lists a version that is not listed with `/usr/libexec/java_home -V`? It confuses me a lot. – Joop Nov 06 '21 at 16:04
-
This is the best answer, using jenv or sdkman adds just unnecessary magic, which breaks sooner or later. – simon Dec 03 '21 at 14:02
-
This does not seem to work for Java 8 (OpenJDK) on macOS 12.1. I have created a separate question, specifically for that: https://stackoverflow.com/questions/70557354/unable-to-create-z-shell-alias-to-activate-java-8-openjdk-on-macos – leifericf Jan 02 '22 at 15:50
-
Does not answer the question how to select the Java version for macOS. – not2savvy Jan 29 '22 at 11:44
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).
Solution without 3rd party tools:
- leave all JDKs at their default location, under
/Library/Java/JavaVirtualMachines
. The system will pick the highest version by default. - To exclude a JDK from being picked by default, rename its
Contents/Info.plist
toInfo.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'sjava
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.

- 19,846
- 6
- 37
- 65
-
22This is the answer I was looking for, since `jenv` nor `JAVA_HOME` are able to set the system-wide java version, i.e. the one Eclipse would use. – Giovanni Lovato Jun 02 '17 at 06:52
-
1I've renamed "Info.plist" to "Info.plist.disabled" yet my java control panel is still picking up the latest java version installed on my machine. Does anyone know if this is still working? As of july/2017 with Sierra 10.12.6. – Rod Jul 26 '17 at 13:05
-
4@Rodrigo Yes, as mentioned in "Details of investigation" (linked above): "Installing a JDK also overwrites `/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/`. Apparently this is the one that the new Preferences pane will launch -- this is the only exception I found to my solution: that thingy will still use JDK9 (latest I installed), but no other Java application does." – Hugues M. Jul 26 '17 at 13:09
-
1@HuguesM. I'm on Sierra and I've been trying to get a legacy application to use Apple's JDK 6 when I launch it (the application requires Java 6). I tried renaming Info.plist, but still when I launched an application it seemed to hang as usual (ever since the upgrade to Sierra). The only other JDK installed is 7. – user766353 Oct 29 '17 at 18:13
-
3Worth noting that java versions are found in /Library/Java/JavaVirtualMachines, and that the above excellent working solution is within that context. – Andrew Kostka Dec 08 '17 at 16:53
-
1Thanks 'control java version system-wide' is what I was looking into – Gaurav Vashisth Jan 05 '18 at 15:22
-
4Exactly, this is the best and the most functional solution for this question. Unfortunately so. – Aerodynamika Feb 19 '18 at 13:22
-
2I wrote a script to make this proces even easier using your method! https://github.com/JoniVR/JavaVersionSwitcher – JoniVR Sep 30 '18 at 15:02
-
1Does not work. It changes the version of java invoked on the command line, but when I try to run Eclipse, it points to the old version and generates this error:"The JVM shared library "/Library/Java/JavaVirtualMachines/openjdk-11.0.1.jdk/Contents/Home/bin/../lib/server/libjvm.dylib" does not contain the JNI_CreateJavaVM symbol. – ccleve Nov 11 '18 at 03:38
-
4I cannot thank you enough for this answer. This fixed launching applications from the OS launcher. – Karthic Raghupathi Nov 14 '18 at 20:05
-
2
-
1working with `dcevm-11.0.1` and `openjdk-11.0.1.jdk` installed at the same time – vigilancer Dec 05 '18 at 14:47
-
1I did vote up this answer, but I have come to realize this does not work (at least not on macOS 10.14.2). Renaming Info.plist to Info.plist.disabled resulted in Java 11 to no longer be the default version of Java. This was desired, however, Java 11 then was no longer available at all as a filterable Java version using java_home -v. – Mike Ryan Jan 02 '19 at 19:25
-
1Even you set the JAVA_HOME like `JAVA_HOME=$(/usr/libexec/java_home -v 1.8)` , you need to do this because, you do not know how some java apps are looking for the java home. Very helpful post. – mcvkr Apr 02 '19 at 18:24
-
1please mark as the answer this is it, systemwide! Some applications for whatever reason only use this as the JDK and there is no way to change it in the application--changing this system wide setting works – Justin Hanley Sep 18 '19 at 15:35
-
1Note, there is a downside to mv'ing the Info.plist files aside - the java_home tool will no longer acknowledge that they exist, as that is the configuration file it reads. – jrg Apr 15 '20 at 10:44
-
1When using `jenv`, consider `jenv enable-plugin export` which will set up `JAVA_HOME` based on jenv settings. You do need to restart your shell the first time. – binkley Apr 04 '21 at 04:02
-
1This worked for me too, as I was trying to get Ghidra to use JDK11 but it kept using JDK16 and having compatibility issues. One way I found to _validate_ that a given JDK was effectively disabled (after renaming `Info.plist`) was to open a shell, run `unset JAVA_HOME`, and then run `java -version`. This way `java` will have to pick a JDK, just like Ghidra or any other UI-based app would do (as opposed to launched from a terminal). Before renaming anything it would say 16.0, and when I renamed `Info.plist` in JDK16 it stopped using it by default. Thanks for this post! – Nicolas Favre-Felix May 16 '21 at 22:05
-
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)

- 21,337
- 9
- 51
- 89

- 4,433
- 1
- 12
- 9
-
7This is the syntax for Java 9: ```alias j9="export JAVA_HOME=`/usr/libexec/java_home -v 9`; java -version"``` – elektromin Oct 03 '17 at 06:37
-
Using ``alias j10="export JAVA_HOME=`/usr/libexec/java_home`; java -version"`` and then typing `j10` allows you to switch back to the current version of Java (in my case Java 10) – intagli May 01 '18 at 12:58
-
I would make a cli tool similar to `nvm` for switching Java versions, but the acronym `jvm` already means something else :) – Andy Sep 22 '18 at 18:48
-
I get this error when sourcing: `/Users/me/.zshrc:8: permission denied: /Library/Java/JavaVirtualMachines/openjdk-12.jdk/Contents/Home` – cosbor11 Apr 04 '19 at 22:28
-
@cosbor11 for `.zshrc` use `export JAVA_HOME=$(/usr/libexec/java_home -v 12)` – oliolioli Aug 22 '19 at 12:12
-
add j8 after this declaration line, so that every time it default it to Java8. If required execute j11 to change version. – Suseendran P Jul 27 '20 at 05:33
-
Be aware that when running `sudo java -version` the version will be different than running as your user.. – royka Jan 25 '22 at 23:50
A small fish function based on /usr/libexec/java_home
function jhome
set JAVA_HOME (/usr/libexec/java_home $argv)
echo "JAVA_HOME:" $JAVA_HOME
echo "java -version:"
java -version
end
If you don't use fish, you can do something similar in bash:
#!/bin/bash
jhome () {
export JAVA_HOME=`/usr/libexec/java_home $@`
echo "JAVA_HOME:" $JAVA_HOME
echo "java -version:"
java -version
}
Then to switch between javas do:
$> jhome #switches to latest java
$> jhome -v 1.7 #switches to java 1.7
$> jhome -v 1.6 #switches to java 1.6

- 1,890
- 2
- 14
- 10
-
2this only changes for the specific terminal session. Is there a way to update this to change it system wide? – ENG618 Mar 23 '16 at 12:19
-
1I had to write `export JAVA_HOME=(/usr/libexec/java_home $argv)` instead of the second line of your `jhome` function. – Daniel A.A. Pelsmaeker Sep 13 '18 at 09:26
Update...
With macOS Monterey, v12.0.1 Cask is no longer a Brew command. Just follow the steps but in step 4 just do $ brew install ...
or $ brew install --cask ...
instead of $ brew cask install ...
Thanks to @Shivam Sahil comment.
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

- 1,535
- 16
- 13
-
Mind making a minor edit @hermeslm? Seeing this: ```brew tap caskroom/cask Error: caskroom/cask was moved. Tap homebrew/cask instead.``` – hafichuk Sep 02 '21 at 17:55
-
Good catch @hafichuk, this worked at the moment of this answer. I will edit it, thanks. – hermeslm Sep 03 '21 at 20:44
-
@hermeslm `brew cask install` won't work with newer versions, add an update of: `brew install cask packageName`. ReferenceL:https://stackoverflow.com/questions/30413621/homebrew-cask-option-not-recognized – Shivam Sahil Nov 25 '21 at 10:23
-
I had to run `brew install adoptopenjdk/openjdk/adoptopenjdk8` instead of `brew cask install adoptopenjdk8` – Vlad R Feb 01 '22 at 14:17
-
Nov 2022 update for Mac: `export JAVA_11_HOME=/opt/homebrew/Cellar/openjdk@11/11.0.16.1/libexec/openjdk.jdk/Contents/Home export JAVA_17_HOME=/opt/homebrew/Cellar/openjdk@17/17.0.4.1/libexec/openjdk.jdk/Contents/Home export JAVA_18_HOME=/opt/homebrew/Cellar/openjdk/18.0.2.1/libexec/openjdk.jdk/Contents/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' # default to Java 14 java18` – lkahtz Nov 23 '22 at 07:02
-
For those that would like some feedback use e.g. `alias java17='export JAVA_HOME=$JAVA_17_HOME && echo JAVA_HOME set to $JAVA_HOME'` – Bjørnar Hvidsten Dec 20 '22 at 16:23
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

- 2,812
- 1
- 18
- 19
-
-
Instead of "jenv use java 1.8", I had to use "jenv shell 1.8". "shell" could also be "global" or "local" depending on the need. – Steve Gelman Sep 09 '16 at 14:44
-
3jenv is nice when it works. Would recommend against it though, as it doesn't "really" change the default java version used, evident by running the command "/usr/libexec/java_home -V", only the JAVA_HOME. This causes weird problems with some applications, e.g. Eclipse. – simon Jan 17 '18 at 10:01
-
1Besides that paranoidal part of me suggests against curling anything to bash. Even if you __think__ the site is trusted no-one can **guarantee** you it's un-hackable. Use your OS' package manager. – Aleksandr Kravets Aug 07 '18 at 09:43
-
That info is outdated. Check [official documentation](http://www.jenv.be/) or [more detailed instructions](https://developer.bring.com/blog/configuring-jenv-the-right-way/). – fabianopinto Dec 13 '18 at 07:25
-
Configuring `jenv` is different now. Please refer to this - https://www.jenv.be/ – Munim Dec 20 '19 at 13:31
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
-
1how do i set JAVA_HOME Variable? is there a particular place I set it in? – Venkat Feb 23 '14 at 05:53
-
-
1I've updated the answer. Do you know the SDK version that you have installed? The folder name takes the SDK version number, so the commands above might change a bit. – Trein Feb 23 '14 at 05:59
-
-
The variable `JAVA_HOME` will be exported on the current Terminal session. You can add it to our `.bashrc` if you want. I will adapt the commands to the last version. – Trein Feb 23 '14 at 06:02
-
@Jack you will need to open your *.profile* file with *vi* through the terminal. – Bart Feb 23 '14 at 06:03
-
I suggest you to edit `.bashrc` with a text editor instead of `.profile`. – Trein Feb 23 '14 at 07:17
-
@Trein I'd advise against using your hack and use [my](http://stackoverflow.com/questions/19563766/eclipse-kepler-for-os-x-mavericks-request-java-se-6/19594116#19594116) version instead. It is less error prone and decidedly easier to implement. It also enables people to use both 1.6 and 1.7 without breaking either. – Nikolas Grottendieck Apr 09 '14 at 17:27
-
@Jack edit your .bash_profile type in your terminal: vim ~/.bash_profile – Mussa Oct 13 '14 at 16:07
-
I got this error after following your answer No Java virtual machine was found after searching the following locations: /System/Library/Frameworks/JavaVM.framework/Versions/1.6/Commands/java – Bharat Oct 31 '14 at 14:28
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

- 439
- 4
- 6
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

- 918
- 9
- 21
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
-
I'm using a similar tool: https://sdkman.io/usage - IMHO much better approach than manual aliases etc – SathOkh Apr 30 '20 at 08:30
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)

- 15,253
- 21
- 95
- 158
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..

- 2,422
- 5
- 28
- 42
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.

- 6,261
- 10
- 66
- 96
-
-
This solution allows running VisualVM (currently not supporting JDK 16) with both JDK 15 and 16 installed. – pyb Mar 22 '21 at 16:42
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
}
put this in your ~/.profile and use it in your terminal like so setjdk 1.8
, setjdk 1.7
, setjdk 9
etc etc...
If you don't have removeFromPath
then it is:
function removeFromPath() {
export PATH=$(echo $PATH | sed -E -e "s;:$1;;" -e "s;$1:?;;")
}

- 6,321
- 5
- 58
- 86

- 402
- 5
- 10
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

- 341
- 4
- 11
-
Thanks! Things of note, you must include the ` marks. and obviously, replace 1.8 with whatever version you might need. In my case 13.0.2 – Jamie S Mar 21 '20 at 00:35
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

- 15,456
- 11
- 71
- 120
-
so does it mean i should put in `~/.zprofile` if I am using `~/.zshrc` (or `zsh` terminal) – elliotching Nov 29 '21 at 01:56
-
-
Beware -- if you do not have the flagged version installed, then `java_home` will return the *default* version (unless you include the -F flag). So this can be approach can be misleading! – Magnus Jun 14 '22 at 17:06
-
Not sure if I understand the concern, can you give an example? if someone is setting these variables in `.bash_profile`, then they are aware of the changes that are being done, it does not happen automatically. – Vishrant Jun 14 '22 at 18:15
add following command to the ~/.zshenv
file
export JAVA_HOME=`/usr/libexec/java_home -v 1.8`

- 1,515
- 14
- 10
- Add the following line of code to your .zshrc (or bash_profile):
alias j='f(){ export JAVA_HOME=
/usr/libexec/java_home -v $1
};f'
- Save to session:
$ source .zshrc
- Run command (e.g. j 13, j14, j1.8...)
$ 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".

- 101
- 1
- 4
-
1after I add the script to .zshrc, then run it like j 11, the terminal outputs: `f:export: not valid in this context: -v` – Dika Apr 01 '22 at 02:01
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

- 958
- 1
- 9
- 14
From the Apple's official java_home(1) man page:
**USAGE**
/usr/libexec/java_home helps users set a $JAVA_HOME in their login rc files, or provides a way for
command-line Java tools to use the most appropriate JVM which can satisfy a minimum version or archi-
tecture requirement. The --exec argument can invoke tools in the selected $JAVA_HOME/bin directory,
which is useful for starting Java command-line tools from launchd plists without hardcoding the full
path to the Java command-line tool.
Usage for bash-style shells:
$ export JAVA_HOME=`/usr/libexec/java_home`
Usage for csh-style shells:
% setenv JAVA_HOME `/usr/libexec/java_home`

- 12,407
- 12
- 64
- 88
JDK Switch Script
I have adapted the answer from @Alex above and wrote the following to fix the code for Java 9.
$ cat ~/.jdk
#!/bin/bash
#list available jdks
alias jdks="/usr/libexec/java_home -V"
# jdk version switching - e.g. `jdk 6` will switch to version 1.6
function jdk() {
echo "Switching java version $1";
requestedVersion=$1
oldStyleVersion=8
# Set the version
if [ $requestedVersion -gt $oldStyleVersion ]; then
export JAVA_HOME=$(/usr/libexec/java_home -v $1);
else
export JAVA_HOME=`/usr/libexec/java_home -v 1.$1`;
fi
echo "Setting JAVA_HOME=$JAVA_HOME"
which java
java -version;
}
Switch to Java 8
$ jdk 8
Switching java version 8
Setting JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home
/usr/bin/java
java version "1.8.0_131"
Java(TM) SE Runtime Environment (build 1.8.0_131-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.131-b11, mixed mode)
Switch to Java 9
$ jdk 9
Switching java version 9
Setting JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-9.0.1.jdk/Contents/Home
/usr/bin/java
java version "9.0.1"
Java(TM) SE Runtime Environment (build 9.0.1+11)
Java HotSpot(TM) 64-Bit Server VM (build 9.0.1+11, mixed mode)

- 21,361
- 14
- 77
- 80
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.

- 983
- 9
- 11
First find out where do you store the environment variables-
- emacs
- bash_profile
- zshrc file
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..

- 1
- 1

- 1,130
- 1
- 10
- 20
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

- 33,549
- 14
- 108
- 109
-
1this helped me in a second to solve issue in KMM "project Command PhaseScriptExecution failed with a nonzero exit code" – micdev Dec 13 '22 at 12:30
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

- 1,967
- 1
- 22
- 18
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

- 5,819
- 7
- 49
- 75
This will guide you on how to switch JAVA versions on your MacOS device. This can be useful for working with apps which don’t work with updated JAVA versions. For example, applications such as Cassandra will only work with JAVA version 8.
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 (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”
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.
(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.
Done.

- 1,387
- 11
- 13
If you have multiple versions and you want to run something by using a specific version, use this example:
/usr/libexec/java_home -v 1.7.0_75 --exec java -jar you-file.jar

- 190
- 2
- 12
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`

- 235
- 2
- 8
-
This has already been mentioned in at least one of the other answers. – Eric Aya Jul 27 '23 at 09:25
-
@Eric Aya the answer had too many details to waste time in urgent situations. – Abhishek Khaiwale Aug 02 '23 at 07:15
Based on @markhellewell answer I created a couple of alias functions that will do it for you. Just add these to your shell startup file
#list available jdks
alias jdks="/usr/libexec/java_home -V"
# jdk version switching - e.g. `jdk 6` will switch to version 1.6
function jdk() {
echo "Switching java version";
export JAVA_HOME=`/usr/libexec/java_home -v 1.$1`;
java -version;
}
https://gist.github.com/Noyabronok/0a90e1f3c52d1aaa941013d3caa8d0e4

- 9,250
- 11
- 70
- 81
Here is how I do it on my Linux (Ubuntu / Mint mate), I guess Mac can do it similarly.
Install & config
Steps:
- [Basic - part]
- Download jdk (the .tgz file) by hand.
- Uncompress & rename properly, at a proper location.
e.g/mnt/star/program/java/jdk-1.8
- Make a soft link, which will be changed to switch java version later.
e.gln -s /mnt/star/program/java/jdk-1.8 /mnt/star/program/java/java
Thus/mnt/star/program/java/java
is the soft link. - Set
JAVA_HOME
in a start script.
Could use file like/etc/profile.d/eric.sh
, or just use~/.bashrc
.
e.gJAVA_HOME=/mnt/star/program/java/java
- Then open a new bash shell.
java -version
should print the java version. - [More version - part]
- Download & install more Java version, as need, similar as above steps.
e.g
/mnt/star/program/java/jdk-11
- [Switch - part]
- In
~/.bashrc
, define variable for various Java version.
e.g
_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
- In
~/.bashrc
, define command to switch Java version.
e.g
## 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"
- In terminal,
source ~/.bashrc
to make the changes take effect. - Then could switch using the defined commands.
Commands - from above config
Commands:
jv11
Switch to Java 11jv8
Switch to Java 8jvd
Switch to default Java version, which is denoted by_E_JAVA_HOME_D
defined above.jv
Show java version.
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:~$
Mechanism
- It switch by changing the soft link, which is used as
JAVA_HOME
.
Tips
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
orjdk-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.gsource $HOME/.eric_var
).

- 22,183
- 20
- 145
- 196
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
}

- 1,950
- 17
- 14
Very simple answer:
- Run and get all the installed JDK path
/usr/libexec/java_home -V
- Run direct commands from jenv.be
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

- 573
- 5
- 12
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

- 5,100
- 13
- 43
- 62
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

- 11
- 5
-
-
I don't think `update-alternatives` exists on Mac, as per https://apple.stackexchange.com/questions/76704/how-to-set-alternatives-in-10-8. Worth checking :) – Paul Benn Jun 01 '23 at 15:00
TOO EASY SOLUTION: What a headache - this was a quick easy solution that worked for me.
Mac OS Sierra Version 10.12.13
Use the shortcut keys: CMD+SHIFT+G - type in "/Library/"
Find the JAVA folder
Right Click Java Folder = Move to Trash (Password Required)
Install: Java SE Development Kit 8 jdk-8u131-macosx-x64.dmg | Download Javascript SDK
- Make sure the new JAVA folder appears in /LIBRARY/
- Install Eclipse | Install Eclipse IDE for Java Developers
- Boom Done

- 19
- 1