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
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):
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.
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
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
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.
Run /usr/libexec/java_home in your terminal and you will get things like /Library/Java/JavaVirtualMachines/jdk1.8.0_261.jdk/Contents/Home
Add export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_261.jdk/Contents/Home to .zshrc
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
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 the OpenJDK11U-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
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 into Java Bytecode, an assembly language for the Java Virtual Machine (JVM). The Java Bytecode is stored in a file called HelloStackoverflow.class.
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