What should I set JAVA_HOME environment variable on macOS X 10.6?
I just set JAVA_HOME
to the output of that command, which should give you the Java path specified in your Java preferences. Here's a snippet from my .bashrc
file, which sets this variable:
export JAVA_HOME=$(/usr/libexec/java_home)
I haven't experienced any problems with that technique.
Occasionally I do have to change the value of JAVA_HOME
to an earlier version of Java. For example, one program I'm maintaining requires 32-bit Java 5 on OS X, so when using that program, I set JAVA_HOME
by running:
export JAVA_HOME=$(/usr/libexec/java_home -v 1.5)
For those of you who don't have java_home
in your path add it like this.
sudo ln -s /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/java_home /usr/libexec/java_home
References:
Oracle explains the java_home command
An article for configuring the JDK in Spring Tool Suite (Eclipse
2019) on MacOS
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 should I set the JAVA_HOME environment variable on macOS?
I'm not sure why you are afraid to test, you can safely test this:
In your terminal session, input the following:
echo "export JAVA_HOME=`/usr/libexec/java_home`"
This will print the following line:
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home
Copy the above and paste it into your terminal window, then press enter key. and execute java -version
to see it work correctly. If everything is okay, you can attach the code into your .profile
:
But adding this line directly is better idea because you don't have to update .profile
when you upgrade JDK.
export JAVA_HOME=`/usr/libexec/java_home`
Please refer to the man page for the java_home tool. In short, it provides the appropriate path for JAVA_HOME for a normal installation on macOS
Where is JAVA_HOME on macOS Mojave (10.14) to Lion (10.7)?
With the Java optional package or Oracle JDK installed,
adding one of the following lines to your ~/.bash_profile file will set the environment variable accordingly.
export JAVA_HOME="$(/usr/libexec/java_home -v 1.6)"
or
export JAVA_HOME="$(/usr/libexec/java_home -v 1.7)"
or
export JAVA_HOME="$(/usr/libexec/java_home -v 1.8)"
or simply
export JAVA_HOME="$(/usr/libexec/java_home)"
Note: If you installed openjdk on mac using brew, run sudo ln -sfn /usr/local/opt/openjdk/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk.jdk
for the above to work
Update: added -v flag based on Jilles van Gurp response.
Setting $JAVA_HOME correctly on Mac OS
In your question, the command shows spaces around the '=' sign in the assignment, which will mess it up.
The assignment should look like this:
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_31.jdk/Contents/Home
You definitely shouldn't have the $(...)
around it - that tries to execute the text inside the parentheses, which is why you get that "No such file or directory" error. Perhaps that came from trying to use examples which show how to execute the OSX java_home
command, as shown in the answer to this question:
What should I set JAVA_HOME to on OSX
Why I am not able to see the JAVA_HOME path on my MAC OS X 10.11?
Where to define it
Ok, first of all, we have to make clear where to set JAVA_HOME
.
Simplified, you can define it in two files: either ~/.bashrc
or ~/bash_profile
. By default the former is executed for what is called "interactive non-login shells" while the latter is used for "login shells".
A "login shell" is exactly what you'd expect: a shell which is started after login via command line. An "interactive non-login shell" is a shell which is started from within a GUI for example. So, according to that, we should put our export statement into ~/.bashrc
.
Side note: While OS X's "Terminal" application reads both files mentioned, this is not the default behavior and therefor should not be treated as such. And thats why I wrote an explanation.
What do define
You need to export JAVA_HOME in the ~/.bashrc
file so that every time a shell is opened, the variable is set.
On OS X, the Java Development Kits and Runtime Environments are stored under /Library/Java/JavaVirtualMachines/
for quite a while now. Have a look there. This is how it looks at my machine:
/Library/Java/JavaVirtualMachines/
├── jdk1.7.0_45.jdk
├── jdk1.8.0_20.jdk
├── jdk1.8.0_25.jdk
└── jdk1.8.0_51.jdk
The subfolders look similar to this
jdk1.7.0_45.jdk/
└── Contents
├── Home
├── Info.plist
└── MacOS
And there we got it. So if you wanted to point to the JDK 1.7.0_45, you'd put the following statement into your .bashrc
export JAVA_HOME="/Library/Java/JavaVirtualMachines/jdk1.7.0_45.jdk/Contents/Home"
To make sure that the correct binaries for that Java version are called, you also should add the following somewhere after the above statement:
export PATH=$JAVA_HOME/bin:$PATH
Failed to find 'JAVA_HOME' environment variable in Mac
You didn't install JAVA_HOME
correctly, therefore it fails.
You have to do it like following:
- 10 JDK 8 Installation for OS X
After installation you have to add installed java to system variable:
- How to Set $JAVA_HOME environment variable on Mac OS X
- What should I set JAVA_HOME to on OSX
Run in terminal for verification:
java -version
The output should be something like:
java version "1.8.0_06-ea"
Java(TM) SE Runtime Environment (build 1.8.0_06-ea-b13)
Java HotSpot(TM) 64-Bit Server VM (build 23.2-b04, mixed mode)
After this check, you can import java to your IDE.
Related Topics
Difference Between @Before, @Beforeclass, @Beforeeach and @Beforeall
How to Handle Invalid Ssl Certificates with Apache Httpclient
Hibernate Opensession() VS Getcurrentsession()
How to Make Httpurlconnection Use a Proxy
How Does Auto Boxing/Unboxing Work in Java
Cannot Parse String in Iso 8601 Format, Lacking Colon in Offset, to Java 8 Date
How to Restart a Java Application
Converting Symbols, Accent Letters to English Alphabet
Swingpropertychangesupport to Dynamically Update Jtextarea
Abstracttablemodel Gui Display Issue
Call an Executable and Pass Parameters
Controlling Maven Final Name of Jar Artifact
How to Use Vectordrawables in Android API Lower Than 21