2

I have installed java11 using

brew install java11

Then I tried the below to get JAVA_HOME

echo $JAVA_HOME

It returned empty . so following the instructions in echo $JAVA_HOME returns nothing

when I have run ls -l /usr/bin/java i cannot see any jvm path as shown in the above link. Below is what I got

 ~ % ls -l /usr/bin/java
-rwxr-xr-x  1 root  wheel  168432 Dec  8 05:09 /usr/bin/java

ls -l /usr/lib/jvm/
ls: /usr/lib/jvm/: No such file or directory

How to set JAVA_HOME

Deepika reddy
  • 65
  • 1
  • 6

1 Answers1

3

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
NewbieDeveloper
  • 408
  • 1
  • 10
  • This doesn't work for me. When I source the `.bash_profile` after your first command, I get this error: `The operation couldn't be completed. Unable to locate a Java Runtime. Please visit http://www.java.com for information on installing Java.` But I have installed java via `brew install openjdk@11`. And `which java` yields a correct path. Any ideas? – Adrian Keister Jan 26 '23 at 22:46
  • I'm thinking a version of openjdk that runs on M1 might be needed? – Adrian Keister Jan 26 '23 at 23:04