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 .zshrc1) 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!!
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
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
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
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
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
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.
add following
setenv JAVA_HOME /System/Library/Frameworks/JavaVM.framework/Home
in your ~/.login file:
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
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
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.
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.
Source: Stackoverflow.com