How to set JAVA_HOME in Mac permanently?
You can use /usr/libexec/java_home -v <version you want>
to get the path you need for JAVA_HOME
. For instance, to get the path to the 1.7 JDK you can run /usr/libexec/java_home -v 1.7
and it will return the path to the JDK. In your .profile
or .bash_profile
just add
export JAVA_HOME=`/usr/libexec/java_home -v <version>`
and you should be good. Alternatively, try and convince the maintainers of java tools you use to make use of this method to get the version they need.
To open '.bash_profile' type the following in terminal :
nano ~/.bash_profile
and add the following line to the file:
export JAVA_HOME=`/usr/libexec/java_home -v <version>`
Press CTRL+X to exit the bash. Press 'Y' to save changes.
To check whether the path has been added, type following in terminal:
source ~/.bash_profile
echo $JAVA_HOME
I was facing the same issue in MAC Catalina, If I edit .bash_profile i found export JAVA_HOME="/Library/Java/JavaVirtualMachines/jdk1.8.0_201.jdk/Contents/Home But When I run terminal echo $JAVA_HOME it was returning empty, Later I found that the file .zshrc was missing I created this file with
touch .zshrc
Then edit it by nano .zshrc
and wrote
source ~/.bash_profile
Which solves my issue permanently
To set your Java path on mac:
- Open terminal on mac, change path to the root cd ~
- vi .bash_profile (This opens the bash_profile file)
-
Click I to insert text and use the following text to set JAVA_HOME and PATH
- export JAVA_HOME='/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home'
-
export PATH=$JAVA_HOME/bin:$PATH
- Type :wq to save and exit the file.
- Type source .bash_profile to execute the .bash_profile file.
- You can type echo $JAVA_HOME or echo $PATH
Installing Java on macOS 11 Big Sur
:
- the easiest way is to select OpenJDK 11 (LTS), the HotSpot JVM, and macOS x64 is to get the latest release here: adoptopenjdk.net
- Select macOS and x64 and download the
JDK
(about 190 MB), which will put theOpenJDK11U-jdk_x64_mac_hotspot_11.0.9_11.pkg
file into your~/Downloads folder
- Clicking on pkg file, will install into this location:
/Library/Java/JavaVirtualMachines/adoptopenjdk-11.jdk
- Almost done. After opening a terminal, the successful installation of the JDK can be confirmed like so:
java --version
- output:
openjdk 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)
-
JAVA_HOME
is an important environment variable and it’s important to get it right. Here is a trick that allows me to keep the environment variable current, even after a Java Update was installed. In~/.zshrc
, I set the variable like so:export JAVA_HOME=$(/usr/libexec/java_home)
- In previous macOS versions, this was done in
~/.bash_profile
. Anyway, open a new terminal and verify:echo $JAVA_HOME
- output:
/Library/Java/JavaVirtualMachines/adoptopenjdk-11.jdk/Contents/Home
- output:
TEST: Compile and Run your Java Program
- Open a text editor, copy the code from below and save the file as
HelloStackoverflow.java
.
public class HelloStackoverflow {
public static void main(String[] args){
System.out.println("Hello Stackoverflow !");
}//End of main
}//End of HelloStackoverflow Class
- From a terminal set the working directory to the directory containing
HelloStackoverflow.java
, then type the command:
javac HelloStackoverflow.java
-
If you're lucky, nothing will happen
-
Actually, a lot happened.
javac
is the name of the Java compiler. It translates Java intoJava Bytecode
, an assembly language for the Java Virtual Machine (JVM). The Java Bytecode is stored in a file calledHelloStackoverflow.class
. -
Running: type the command:
java HelloStackoverflow
# output:
# Hello Stackoverflow !
Try this link http://www.mkyong.com/java/how-to-set-java_home-environment-variable-on-mac-os-x/
This explains correctly, I did the following to make it work
- Open Terminal
- Type
vim .bash_profile
- Type your java instalation dir in my case
export JAVA_HOME="/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home
- Click
ESC
then type:wq
(save and quit in vim) - Then type
source .bash_profile
-
echo $JAVA_HOME
if you see the path you are all set.
Hope it helps.