I am trying to set JAVA_HOME
by entering export JAVA_HOME=/Library/Java/Home
at terminal.
It sets the JAVA_HOME
for current session.
How can I set it permanently?
I am trying to set JAVA_HOME
by entering export JAVA_HOME=/Library/Java/Home
at terminal.
It sets the JAVA_HOME
for current session.
How can I set it 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
Installing Java on macOS 11 Big Sur
:
JDK
(about 190 MB), which will put the OpenJDK11U-jdk_x64_mac_hotspot_11.0.9_11.pkg
file into your ~/Downloads folder
/Library/Java/JavaVirtualMachines/adoptopenjdk-11.jdk
java --version
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)
~/.bash_profile
. Anyway, open a new terminal and verify: echo $JAVA_HOME
/Library/Java/JavaVirtualMachines/adoptopenjdk-11.jdk/Contents/Home
TEST: Compile and Run your Java Program
HelloStackoverflow.java
.public class HelloStackoverflow {
public static void main(String[] args){
System.out.println("Hello Stackoverflow !");
}//End of main
}//End of HelloStackoverflow Class
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 into Java Bytecode
, an assembly language for the Java Virtual Machine (JVM). The Java Bytecode is stored in a file called HelloStackoverflow.class
.
Running: type the command:
java HelloStackoverflow
# output:
# Hello Stackoverflow !
To set your Java path on mac:
Click I to insert text and use the following text to set JAVA_HOME and PATH
export PATH=$JAVA_HOME/bin:$PATH
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
vim .bash_profile
export JAVA_HOME="/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home
ESC
then type :wq
(save and quit in vim)source .bash_profile
echo $JAVA_HOME
if you see the path you are all set.Hope it helps.
Besides the settings for bash/ zsh terminal which are well covered by the other answers, if you want a permanent system environment variable for terminal + GUI applications (works for macOS Sierra; should work for El Capitan too):
launchctl setenv JAVA_HOME $(/usr/libexec/java_home -v 1.8)
(this will set JAVA_HOME to the latest 1.8 JDK, chances are you have gone through serveral updates e.g. javac 1.8.0_101, javac 1.8.0_131)
Of course, change 1.8 to 1.7 or 1.6 (really?) to suit your need and your system
run this command on your terminal(here -v11 is for version 11(java11))-:
/usr/libexec/java_home -v11
you will get the path on your terminal something like this -:
/Library/Java/JavaVirtualMachines/jdk-11.0.9.jdk/Contents/Home
now you need to open your bash profile in any editor for eg VS Code
if you want to edit your bash_profile in vs code then run this command -:
code ~/.bash_profile
else run this command and then press i to insert the path. -:
open ~/.bash_profile
you will get your .bash_profile now you need to add the path so add this in .bash_profile (path which you get from 1st command)-:
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-11.0.9.jdk/Contents/Home
if you were using code editor then now go to terminal and run this command to save the changes -:
source ~/.bash_profile
else press esc then :wq to exit from bash_profile then go to terminal and run the command given above. process completed. now you can check using this command -:
echo $JAVA_HOME
you will get/Library/Java/JavaVirtualMachines/jdk-11.0.9.jdk/Contents/Home
To set JAVA_HOME permanently in Mac, I tried following steps.
/Library/Java/JavaVirtualMachines
atom ~/.bash_profile
Change the JDK version accordingly
export JAVA_HOME="$(/usr/libexec/java_home -v 1.8)"
export JAVA_HOME='/Library/Java/JavaVirtualMachines/jdk1.8.0_271.jdk/Contents/Home'
export PATH=$JAVA_HOME/bin:$PATH
source ~/.bash_profile
Open a new terminal and check 'echo $JAVA_HOME'
Thanks.
This link may helps you: https://developer.apple.com/library/archive/qa/qa1067/_index.html
Also, you can put the environment variable in one of these files:
~/.bashrc
~/.bash_profile
~/.profile
the answers here are in general correct, but for me I didn't know that I need to do something like source ~/.bash_profile
to be able to really make it work.
so the full answer is:
nano ~/.zshenv // or
nano ~/.zshrc // or
nano ~/.bash_profile
//add this line to zshrc and bash_profile if you want to be 100% sure
export JAVA_HOME=$(/usr/libexec/java_home)
//save
source ~/.bash_profile //the KEY that generally isn't in the answers
source ~/.zshrc //I didn't do this one, but maybe you need it
1) The first step is to if you have Java installed and running your system.
which java
Usually, it should be /usr/bin/java.
2) JAVA_HOME is essentially the full path of the directory that contains a sub-directory named bin which in turn contains the java.
cd /Library/Java/
3.1) If you want to set the path only for the current session then execute this command in your terminal export JAVA_HOME=/Library/Java
3.2) If you want it to persist, you will have to add the command to your ~/.bash_profile
file
vi ~/.bash_profile
export JAVA_HOME=/Library/Java/Home
and save itsource ~/.bash_profile
4) Verify you have correctly added JAVA_HOME path. Below command should give you the proper Java version.
That's it and your ready to use!!
Adding to Dilips's answer, if you are working with JDK 9, use the following (my JDK version is 9.0.4) in Step # 3:
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-9.0.4.jdk/Contents/Home
sql-surfer and MikroDel,
actually, the answer is not that complicated! You just need to add:
export JAVA_HOME=(/usr/libexec/java_home)
to your shell profile/configuration file. The only question is - which shell are you using? If you're using for example FISH, then adding that line to .profile
or .bash_profile
will not work at all. Adding it to config.fish
file though will do the trick. Permanently.
First, figure out where your java home is by running the command /usr/libexec/java_home -v <version>
replacing with whatever version of OpenJDK your running.
Next use vim ~/.bash_profile
to edit your bash profile. Add export JAVA_HOME="<java path>"
replacing with the path to your java home found in the last step.
Finally, run the command source ~/.bash_profile
This should permanently set your JAVA_HOME environment variable.
To make sure it worked run echo $JAVA_HOME
and make sure it returns the path you set
to set JAVA_HOME permenantly in mac make sure you have JDK installed in your system, if jdk is not installed you can download it from here https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
After installing jdk follow these steps :-
1) Open Terminal
2) Type "vim .bash_profile"
3) press "i" to edit or enter the path
4) Type your java instalation dir :- export JAVA_HOME=$(/usr/libexec/java_home)
5) Click ESC then type ":wq" (save and quit in vim)
6) Then type "source .bash_profile"
7) type "echo $JAVA_HOME" if you see the path you are all set.
THANK YOU
Declare two export inside your .bashrc or .zshrc:
export JAVA_8_HOME=$(/usr/libexec/java_home -v1.8)
export JAVA_11_HOME=$(/usr/libexec/java_home -v11)
Add alias for quick change:
alias java8='export JAVA_HOME=$JAVA_8_HOME'
alias java11='export JAVA_HOME=$JAVA_11_HOME'
set default to Java 11
java11
export PATH
export PATH=$JAVA_HOME/bin:$PATH
you could change java11 by java8 inside your .bashrc/zshrc file to change permanently your java version
If you are using fish shell. Then all the variables can be set in .config/fish/config.fish
vim .config/fish/config.fish
Add the following lines
set -g JAVA_HOME "your_path_to_jdk"
save and exit out of vim.
This should be setting your JAVA_HOME. Thanks
If you are using the latest versions of macOS, then you cannot use ~/.bash_profile to export your environment variable since the bash shell is deprecated in the latest version of macOS.
/usr/libexec/java_home
in your terminal and you will get things like /Library/Java/JavaVirtualMachines/jdk1.8.0_261.jdk/Contents/Home
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_261.jdk/Contents/Home
to .zshrcFind out the jenv will set the $JAVA_HOME a short version like "/Users/*****/.jenv/versions/1.8".
Have to uninstall jenv to make the $JAVA_HOME setting works like "JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_261.jdk/Contents/Home"
Most of the answers were suggesting to source .bash_profile were outdated for me, I'm using macOS Ventura 13.4.1 which uses Zsh shell. Knowing which shell you are using helps you determine which configuration file to modify.
Step 1:
echo $SHELL
Step 2:
if you see /bin/zsh then you need to update JAVA_HOME in ~/.zshrc, oterwise if you're seeing /bin/bash you need to update JAVA_HOME in ~/.bash_profile. Here ~/ is the home path. you can find .zshrc and .bash_profile in your home directory. you can go to you home directory by pressing return after cd
Step 3
/usr/libexec/java_home -V
run this command to see the jdks installed on your mac.
Step 4
I was using the Zsh shell, so I had to add this export in my .zshrc file. if you don't have this file in you ~/
home direcroy you need to create it.
export JAVA_HOME=`/usr/libexec/java_home -v 11`
replace the version after -v to the version you want to permanently set and aready have installed.
Step 5
if you're using Zsh shell
source .zshrc
else
source .bash_profile
make sure you're running this command in your home directory if you're not in your home directory run source ~/.zshrc
as ~/ is shorthand for home directory.
add following
setenv JAVA_HOME /System/Library/Frameworks/JavaVM.framework/Home
in your ~/.login file:
If you want to set the java version permanently, i.e., If you have 2 or more Java versions in your local
Then, follow these steps:
1. Check the available java versions using "/usr/libexec/java_home -V" command
2. Set the path inside the .bash_profile
Here, "/Library/Java/JavaVirtualMachines/jdk-11.0.9.jdk/Contents/Home" is the java11 version path from step 1