How to set JAVA_HOME environment variable on Mac OS X 10.9?
If you're using bash, all you have to do is:
echo export "JAVA_HOME=\$(/usr/libexec/java_home)" >> ~/.bash_profile
If you're using zsh (which probably means you're running macOS Catalina or newer), then it should instead be:
echo export "JAVA_HOME=\$(/usr/libexec/java_home)" >> ~/.zshrc
In either case, restart your shell.
If you have multiple JDK versions installed and you want it to be a specific one, you can use the -v
flag to java_home
like so:
echo export "JAVA_HOME=\$(/usr/libexec/java_home -v 1.7)" >> ~/.bash_profile
How to set JAVA_HOME environment variable on macOS?
in .bash_profile:
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.xx/Contents/Home
How can I change the JAVA_HOME directory permanently on MAC?
This isn't so much a Gradle question, as an environment question. You would need to set JAVA_HOME
in your shell's configuration. If you can tell me what shell you're using, I can point you in the right direction.
Since you're using ZSH, you can open ~/.zshenv
and paste this in:
export JAVA_HOME="/Library/Java/JavaVirtualMachines/jdk1.8.0_221.jdk/Contents/Home/"
Another solution would be to use something like Jenv to manage your Java environment, with the Gradle
plugin enabled.
Wrong JAVA_HOME after upgrade to macOS Big Sur v11.0.1
Seems in macOS Big Sur v11.0.1 the behavior of the /usr/libexec/java_home -v ...
command has changed: it is sensitive to the previously set value of JAVA_HOME
environment variable.
Exact behavior is not clear, I couldn't find any documentation on this, but in my experiments it reported the version already set in JAVA_HOME
, regardless of the -v
switch:
% JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.7.0_45.jdk/Contents/Home /usr/libexec/java_home -v 1.8.0_162
/Library/Java/JavaVirtualMachines/jdk1.7.0_45.jdk/Contents/Home
Additionally, I noticed that it reports nothing, if JAVA_HOME
is set, but doesn't point to a valid java home (also for -V
):
% JAVA_HOME=dummy /usr/libexec/java_home -v 1.7.0_45
% JAVA_HOME=dummy /usr/libexec/java_home -V
%
Solution is to ensure JAVA_HOME
is not set before executing /usr/libexec/java_home
:
% unset JAVA_HOME ; /usr/libexec/java_home -v 1.8.0_162
/Library/Java/JavaVirtualMachines/jdk1.8.0_162.jdk/Contents/Home
Related Topics
Exception in Thread 'Main' Java.Lang.Noclassdeffounderror:
Integer Arithmetic in Java with Char and Integer Literal
Spring Boot Redirect Http to Https
How to Copy File Inside Jar to Outside the Jar
Extract String Between Two Strings in Java
Is It Better Practice to Use String.Format Over String Concatenation in Java
How to Show Jrbeancollectiondatasource Data with Help of Table Component
Cannot Find Main Class in File Compiled with Ant
If I Synchronized Two Methods on the Same Class, Can They Run Simultaneously
How to Create a Folder in Java
Differencebetween Iterator and Iterable and How to Use Them
How to Sort Arraylist<Long> in Decreasing Order
Encrypt and Decrypt with Aes and Base64 Encoding
Does Java Support Inner/Local/Sub Methods
Jtable with Horizontal Scrollbar