Reputation: 4082
I just purchased a brand new MacBook Pro.
This is my first MAC ever and I'm still trying to get the hang of navigating my way around.
Anyway, I'm also new to Java and I've been practicing on my Windows PC before it permanently died.
Now that I'm on this MAC, I installed my JDK and now I need to set the JAVA_HOME
environment variable.
I have no idea what to do.
I tried following some of these guides and didn't get very far.
I was able to locate the terminal and I think I created some multiple files. I'm getting messages like this:
(1) Another program may be editing the same file. If this is the case, be careful not to end up with two different instances of the same file when making changes. Quit, or continue with caution.
(2) An edit session for this file crashed. If this is the case, use ":recover" or "vim -r /Users/Erwin/.bash_profile" to recover the changes (see ":help recovery"). If you did this already, delete the swap file "/Users/Erwin/.bash_profile.sw p" to avoid this message.
Can somebody tell how to set Java in Mac OSX environment step by step?
Upvotes: 380
Views: 1041221
Reputation: 556
Adrian Petrescu's answer is correct.
https://stackoverflow.com/a/22842806/10090030
Additionally, you can verify your changes by running the following command in the terminal:
print $JAVA_HOME
If the changes were applied successfully, you'll see:
/Library/Java/JavaVirtualMachines/jdk-xx.jdk/Contents/Home
If the configuration is incorrect, nothing will be displayed.
Upvotes: 0
Reputation: 549
First for MacOS let's check the java path go to finder:
command + space
type
finder
on the finder press the following key to open goto:
command+shift+g
and you can go to this path below, depending upon the java version, you can go to here;
/Library/Java/JavaVirtualMachines/jdk-23.jdk/Contents/Home
In case you didn't find java on this path: /Library/Java/JavaVirtualMachines/jdk-23.jdk/Contents/Home/
then you need to install java sdk for macOS, You can find it in java official page, and install ARM64 DMG Installer, based on your macOS.
After this check again this path as with above steps:
/Library/Java/JavaVirtualMachines/jdk-23.jdk/Contents/Home
then add this to your .zshrc file,
in case you don't have .zshrc file, go to terminal type
cd
then type
ls -al
still you didn't see the .zshrc file then make one using
nano ~/.zshrc
and press i
then insert the path
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk23.jdk/Contents/Home
press escape
:x
and hit enter to save
now include this in source by typing following from the terminal:
source ~/.zshrc
let's check if it's saved or not, type
vim ~/.zshrc
again press i and press :x to exit
This should work!!!
if this doesn't work then make
nano ~/.bash_profile
and do the same process as we did.
my ~/.zshrc file looks like this:
export PATH="/opt/homebrew/bin:$PATH"
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-23.jdk/Contents/Home
export ANDROID_HOME=$HOME/Library/Android/sdk
export PATH=$PATH:$ANDROID_HOME/platform-tools
export PATH=$PATH:$ANDROID_HOME/tools
export PATH=$PATH:$ANDROID_HOME/tools/bin
export PATH=$PATH:$ANDROID_HOME/emulator
Upvotes: 1
Reputation: 662
For mac users https://stackoverflow.com/a/77953587/4792285
Install using homebrew https://formulae.brew.sh/formula/openjdk
brew install openjdk@21
OR
brew install openjdk@11
Then set up the .profile on your mac (/Users/your_username/.profile). If this file is hidden you may need to press Command + Shift + . (the period key) to make it visible.
add JAVA_HOME and update the PATH variable
export JAVA_HOME=$(/usr/libexec/java_home)
export PATH="$JAVA_HOME:$PATH"
open a new terminal and type
echo $JAVA_HOME
echo $PATH
to verify your change.
https://stackoverflow.com/a/77953587/4792285
Upvotes: -1
Reputation: 9
I had multiple versions of JDK installed, so what worked for me is that I manually configured my SpringToolSuite4 to use Java 11
Upvotes: 0
Reputation: 172
so i used below to set the JAVA_HOME to my openjdk11 -
Upvotes: 1
Reputation: 4769
In Mac OSX 10.5 or later, Apple recommends to set the $JAVA_HOME variable to /usr/libexec/java_home
, just export $JAVA_HOME
in file ~/.bash_profile
or ~/.profile
.
Open the terminal and run the below command.
$ vim .bash_profile
export JAVA_HOME=$(/usr/libexec/java_home)
Save and exit from Vim editor, then run the source
command on .bash_profile
$ source .bash_profile
$ echo $JAVA_HOME
/Library/Java/JavaVirtualMachines/1.7.0.jdk/Contents/Home
Upvotes: 41
Reputation: 1156
I did it by putting
export JAVA_HOME=`/usr/libexec/java_home`
(backtics) in my .bashrc. See my comment on Adrian's answer.
Additionally update PATH variable as well.
PATH=$PATH:$JAVA_HOME/bin
Upvotes: 61
Reputation: 659
Quick Guide for M1
Add java sdk into your m1 check version
java --version
Get all java versions installed in ur mac
/usr/libexec/java_home -V
Execute for Java path from library
/usr/libexec/java_home
(specify java version if you have multiple version, In my case -v17.0.5
/usr/libexec/java_home -v17.0.5
Mac>User>'YourUserName/Home'>.zshrc
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-17.0.5.jdk/Contents/Home
Upvotes: 17
Reputation: 3490
More simply on a mac terminal with a modern OSX
$ vim ~/.zshrc
Type "a" to being editing, and then paste (ctrl + v):
$ JAVA_HOME=/usr/libexec/java_home
then hit "escape" and type exactly ":wq" in order to write to the file and quit vim mode.
Finally, when out of vim mode and back in your terminal, type
$ source ~/.zshrc
This will refresh so that your terminal is aware of the changes.
Check the changes by typing
$ echo $JAVA_HOME
and you should see /usr/libexec/java_home
Upvotes: 5
Reputation: 1
Upvotes: 0
Reputation: 2294
I'm able to solve this issue by setting JAVA_HOME in .bash_profile file
export JAVA_HOME=/usr/local/opt/openjdk@17
Note: I installed openjdk version 17 using 'brew'. I got this location from brew console. I'm using 'bash' instead of 'zsh' in my mac.
Upvotes: 1
Reputation: 51
For Mac M1
Download & Install install JDK
Open terminal check java version
java -version
Now create a file
touch .zprofile
Open the file
open -t .zprofile
Add the below line
export JAVA_HOME=$(/usr/libexec/java_home)
Upvotes: 4
Reputation: 1
I resolved it on macOS Monterey by using the option provided by Google
Under Gradle JDK, choose the Embedded JDK option.
Upvotes: 0
Reputation: 395
JAVA 11 via Homebrew - tested on macos Ventura 2022
.zshrc
export JAVA_HOME=/opt/homebrew/opt/openjdk@11/libexec/openjdk.jdk/Contents/Home
Upvotes: 6
Reputation: 3504
In the latest Mac, you have to add the Set $JAVA_HOME environment variable in .zprofile. Here, we simple way to open it. Press ⌘ + Shift + . from keyboard. Just open it and add the $JAVA_HOME environment variable as explained here:
Upvotes: 1
Reputation: 18039
If you're using bash, all you have to do is:
echo export "JAVA_HOME=\$(/usr/libexec/java_home)" >> ~/.bash_profile
If you're using zsh (which probably means you're running macOS Catalina or newer), then it should instead be:
echo export "JAVA_HOME=\$(/usr/libexec/java_home)" >> ~/.zshrc
In either case, restart your shell.
If you have multiple JDK versions installed and you want it to be a specific one, you can use the -v
flag to java_home
like so:
echo export "JAVA_HOME=\$(/usr/libexec/java_home -v 1.7)" >> ~/.bash_profile
Upvotes: 656
Reputation: 1864
I just spent 2 hours setting this variable. The other answers did not work properly for me. I'm using macOS Catalina 10.15.4.
First, find your actual Java SDK Home directory:
/usr/libexec/java_home
Manually navigate there to make sure you don't have any mistakes due to incorrect versions, etc. For me, this was:
/Library/Java/JavaVirtualMachines/jdk-13.0.2.jdk/Contents/Home
Next, edit your terminal's profile. If you're using zsh, this will be:
vim ~/.zshrc
If you're not using zsh, this will be:
vim ~/.bash_profile
Inside, add the following new line anywhere in the file:
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-13.0.2.jdk/Contents/Home
Restart your terminal app (or source ~/.bash_profile), and it should work properly.
Upvotes: 160
Reputation: 341
If you are using Zsh, then try to add this line in ~/.zshrc file & restart terminal.
export JAVA_HOME=$(/usr/libexec/java_home)
Upvotes: 7
Reputation: 83
Since I'm using openjdk managed with sdkman, I added
sudo ln -sfn /path/to/my/installed/jdk/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk.jdk
Adding this to your system lets java_home
recognize your installed version of Java even when its not installed via standard packages
Upvotes: 1
Reputation: 9240
It is recommended to check default terminal shell before set JAVA_HOME environment variable, via following commands:
$ echo $SHELL
/bin/bash
If your default terminal is /bin/bash (Bash), then you should use @Adrian Petrescu method.
If your default terminal is /bin/zsh (Z Shell), then you should set these environment variable in ~/.zshenv file with following contents:
export JAVA_HOME="$(/usr/libexec/java_home)"
Similarly, any other terminal type not mentioned above, you should set environment variable in its respective terminal env file.
Upvotes: 28
Reputation: 2954
Download & Install install JDK
$ java -version
Set JAVA_HOME environment variable
$ open -t .zprofile
Or create . zprofile file
$
open -t .zprofile
export JAVA_HOME=$(/usr/libexec/java_home)
Save .zprofile and close the bash file & then write in the terminal for work perfectly.
$ source .zprofile
Setup test in terminal
$ echo $JAVA_HOME
/Library/Java/JavaVirtualMachines/jdk-13.0.1.jdk/Contents/Home
Upvotes: 49
Reputation: 51
I got it working by adding to ~/.profile. Somehow after updating to El Capitan beta, it didnt work even though JAVA_HOME was defined in .bash_profile.
If there are any El Capitan beta users, try adding to .profile
Upvotes: 4