How to set JAVA_HOME in macOS and Linux permanently
- Details
- Written by Nam Ha Minh
- Last Updated on 30 December 2021   |   Print Email
export JAVA_HOME=$HOME/jdk-version/Contents/Home
export PATH=$JAVA_HOME/bin:$PATH
However, these commands have effect only in the current session. If you exit the terminal and open a new one, you will the changes lost.So how to permanently set JAVA_HOME and update PATH environment variables on macOS / Linux? Read on to know how. And I suppose that you already have JDK (Java Development Kit) installed.1. Set JAVA_HOME permanently in Z-shell
Z-shell (zsh) is the default shell in latest macOS operating systems. Firstly, you need to create a Z-shell resource file (.zshrc) in your user home directory, using the following command in terminal:cat > .zshrc
Then type the following commands as content of the file:export JAVA_HOME=$HOME/jdk-17.0.1.jdk/Contents/Home
export PATH=$JAVA_HOME/bin:$PATH
Note that the HOME environment variable refers to your user home directory. And I suppose that you have JDK installed in jdk-17.0.1.jdk directory, which is in your home directory.
Now, quit the terminal. Then open a new one. Type java -version command again and you should see the change is permanent.
2. Set JAVA_HOME permanently in Bash shell
To setup JAVA_HOME with Bash shell, you create the Bash profile file (.bash_profile instead of .zshrc) in your user home directory, like this:cat > .bash_profile
export JAVA_HOME=$HOME/jdk-17.0.1.jdk/Contents/Home
export PATH=$JAVA_HOME/bin:$PATH

Related Articles:
- How to set JAVA_HOME environment variable on Windows 10
- What are JVM, JRE and JDK
- How to write, compile and run a hello world Java program for beginners
- Java Core Language Tutorials
About the Author:
Nam Ha Minh is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.
Comments