I have 2 JAVA version on my Macbook. I want to change version from 14 to 11. I found 2 solutions on the internet but both are not working.
My java -version result is
openjdk version "14.0.1" 2020-04-14
OpenJDK Runtime Environment (build 14.0.1+14)
OpenJDK 64-Bit Server VM (build 14.0.1+14, mixed mode, sharing)
1st Solution (How to set or change the default Java (JDK) version on OS X?),
I run /usr/libexec/java_home -V then I got a result below,
Matching Java Virtual Machines (2):
14.0.1, x86_64: "OpenJDK 14.0.1" /Library/Java/JavaVirtualMachines/openjdk.jdk/Contents/Home
11.0.8, x86_64: "Java SE 11.0.8" /Library/Java/JavaVirtualMachines/jdk-11.0.8.jdk/Contents/Home
Then I did
export JAVA_HOME=`/usr/libexec/java_home -v 11.0.8`
After checking from java -version, The result is still 14.0.1
2nd solution (How to set JAVA_HOME in Mac permanently?), I edit ~/.bash_profile file as below,
export JAVA_HOME=`/usr/libexec/java_home -v 11.0.8`
And I run command
source ~/.bash_profile
echo $JAVA_HOME
It shows the result,
/Library/Java/JavaVirtualMachines/jdk-11.0.8.jdk/Contents/Home
But when I check the result with java -version, It's still 14.0.1 as well
Install sdkman which takes care of the rather tedious command line voodoo you have to employ to try to make this happen. The problem is, JAVA_HOME is just an environment variable, it changes nothing - only tools that explicitly look for it (generally, maven and ant for example) will be affected by messing with it. When you type java on a mac, it runs /usr/bin/java, which is not a file you can change even as root. This java will then invoke the real java, and does not look at JAVA_HOME to get the job done: It is a softlink to /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/java, and because it is in /System you can't change that either, not even as root.
That's why this is so hard, and why you want a nice tool (sdkman) to do it for you.
The removal of quotes around the JAVA_HOME and then setting the path variable variable worked for me (Mac OS Catalina):
export JAVA_HOME=/Library/Java/JavaVirtualMachines/openjdk.jdk/Contents/Home
export PATH=$JAVA_HOME/bin:$PATH
If you need to switch between versions and already have the JDKs installed, you can adjust your bash profile with aliases that reset JAVA_HOME and reinitialize your PATH each time:
export JAVA_11_HOME=$(/usr/libexec/java_home -v11)
export JAVA_14_HOME=$(/usr/libexec/java_home -v14)
alias java11='export JAVA_HOME=$JAVA_11_HOME; export PATH=$JAVA_HOME/bin:$PATH'
alias java14='export JAVA_HOME=$JAVA_14_HOME; export PATH=$JAVA_HOME/bin:$PATH'
Then, after restarting your terminal session, you can switch back and forth:
$ java11
$ java -version
and
$ java14
$ java -version
Try export JAVA_HOME='/usr/libexec/java_home -v 11' (without the minor version), it outputs the right java -version for me on Mac.
I am working on Amazon ec2 for the first time and verified java version by using the command java -version and got the below results:
openjdk version "1.8.0_201"
OpenJDK Runtime Environment (build 1.8.0_201-b09)
OpenJDK 64-Bit Server VM (build 25.201-b09, mixed mode)
And also run the command javac -version, but no results. From this, I have understood the javac is not set and none of the java related applications are running. Can anyone please tell me how to resolve this problem?
javac should be in the bin directory of the java installation dir and java in the jre/bin directory. You could run dirname $(readlink -e $(which java)) to find out the absolute path to the jre/bin dir and then modify it to the bin dir. The result is added to your PATH variable in your ~/.profile.
Add this line to your ~/.profile (or ~/.bash_profile or ~/.bashrc, whichever is read on startup)
export PATH=$PATH:$(readlink -e $(dirname $(readlink -e $(which java)))/../../bin)
Then logout and login again and test if javac -version works as expected.
AFTER SETTING YOUR SYSTEM VARIABLE TO JAVA_HOME YOU NEED TO DO THIS FOR IT WORK ON YOUR COMMAND PROMPT:
After setting the JAVA_HOME system variable you need to also set the path so you can use javac from the command prompt:
After setting the JAVA_HOME system variable:
Inside System Variables find "Path" and click "Edit"
At the end of the Path add this line: ";%JAVA_HOME%\bin" (without the double quotes)
Now click "Ok" and exit out of your command prompt window (If it's still open)
now try "javac -version" and it should work
You can also check your java version, JRE build version, and Java Server VM build by typing "java -version"
Java is an optional package on the latest versions of macOS.
Yet once installed it appears like the JAVA_HOME environment variable is not set properly.
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.
/usr/libexec/java_home is not a directory but an executable. It outputs the currently configured JAVA_HOME and doesn't actually change it. That's what the Java Preferences app is for, which in my case seems broken and doesn't actually change the JVM correctly. It does list the 1.7 JVM but I can toggle/untoggle & drag and drop all I want there without actually changing the output of /usr/libexec/java_home.
Even after installing 1.7.0 u6 from Oracle on Lion and setting it as the default in the preferences, it still returned the apple 1.6 java home. The only fix that actually works for me is setting JAVA_HOME manually:
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.7.0_06.jdk/Contents/Home/
At least this way when run from the command line it will use 1.7. /usr/libexec/java_home still insists on 1.6.
Update: Understanding Java From Command Line on OSX has a better explanation on how this works.
export JAVA_HOME=`/usr/libexec/java_home -v 1.7`
is the way to do it. Note, updating this to 1.8 works just fine.
For me, Mountain Lion 10.8.2, the solution most voted does not work.
I installed jdk 1.7 via Oracle and maven from homebrew.
My solution is from the hadoop-env.sh file of hadoop which I installed from homebrew, too.
I add the below sentence in ~/.bash_profile, and it works.
export JAVA_HOME="$(/usr/libexec/java_home)"
This solution also works for OS X Yosemite with Java 1.8 installed from Oracle.
None of the above answers helped me. I suppose all the answers are for older OS X
For OS X Yosemite 10.10, follow these steps
Use your favorite text editor to open: ~/.bash_profile
//This command will open the file using vim
$ vim ~/.bash_profile
Add the following line in the file and save it ( : followed by "x" for vim):
export JAVA_HOME=$(/usr/libexec/java_home)
Then in the terminal type the following two commands to see output:
$ source ~/.bash_profile
$ echo $JAVA_HOME
In the second line, you are updating the contents of .bash_profile file.
Update for Java 9 and some neat aliases.
In .bash_profile:
export JAVA_HOME8=`/usr/libexec/java_home --version 1.8`
export JAVA_HOME9=`/usr/libexec/java_home --version 9`
Note, that for the latest version it is 9 and not 1.9.
Set active Java:
export JAVA_HOME=$JAVA_HOME8
export PATH=$JAVA_HOME/bin:$PATH
Some additional alias to switch between the different versions:
alias j8='export JAVA_HOME=$JAVA_HOME8; export PATH=$JAVA_HOME/bin:$PATH'
alias j9='export JAVA_HOME=$JAVA_HOME9; export PATH=$JAVA_HOME/bin:$PATH'
Test in terminal:
% j8
% java -version
java version "1.8.0_121"
Java(TM) SE Runtime Environment (build 1.8.0_121-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.121-b13, mixed mode)
% j9
% java -version
java version "9"
Java(TM) SE Runtime Environment (build 9+181)
Java HotSpot(TM) 64-Bit Server VM (build 9+181, mixed mode)
EDIT: Update for Java 10
export JAVA_HOME10=`/usr/libexec/java_home --version 10`
alias j10='export JAVA_HOME=$JAVA_HOME10; export PATH=$JAVA_HOME/bin:$PATH'
EDIT: Update for Java 11
export JAVA_HOME11=`/usr/libexec/java_home --version 11`
alias j11='export JAVA_HOME=$JAVA_HOME11; export PATH=$JAVA_HOME/bin:$PATH'
The above didn't work for me with Amazon's EC2 tools, because it expects bin/java etc. underneath JAVA_HOME. /System/Library/Frameworks/JavaVM.framework/Home did work.
For OS X you can do:
export JAVA_HOME=`/System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/java_home`
On Mac OS X Lion, to set visualgc to run, I used:
export JAVA_HOME=/System/Library/Frameworks/JavaVM.framework/Versions/1.6.0/Home
The following worked for me. I'm using ZSH on OSX Yosemite with Java 8 installed.
The following command /usr/libexec/java_home emits the path to JDK home:
/Library/Java/JavaVirtualMachines/jdk1.8.0_05.jdk/Contents/Home
In your ~/.zshrc,
export JAVA_HOME = "/Library/Java/JavaVirtualMachines/jdk1.8.0_05.jdk/Contents/Home"
for macOS Mojave 10.14.1 and JAVA 11.0.1
I set the profile as
export JAVA_HOME=$(/usr/libexec/java_home)
key in terminal this to confirm:
$JAVA_HOME/bin/java -version
java version "11.0.1" 2018-10-16 LTS
Java(TM) SE Runtime Environment 18.9 (build 11.0.1+13-LTS)
Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.1+13-LTS, mixed mode)
A better (more upgradable) way is to use the following:
/System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK/Home
This should work with AWS also since it has bin underneath Home
Newer Oracle JVMs such as 1.7.0_21-b12 seem to install here:
/Library/Internet\ Plug-Ins/JavaAppletPlugin.plugin/Contents/Home
My approach is:
.bashrc
export JAVA6_HOME=`/usr/libexec/java_home -v 1.6`
export JAVA7_HOME=`/usr/libexec/java_home -v 1.7`
export JAVA_HOME=$JAVA6_HOME
# -- optional
# export PATH=$JAVA_HOME/bin:$PATH
This makes it very easy to switch between J6 and J7
I Had to explicitly set it to the exact path on my Macbook air .
Steps followed:
try to echo $JAVA_HOME (if it's set it'll show the path), if not, try to search for it using sudo find /usr/ -name *jdk
Edit the Bash p with - sudo nano ~/.bash_profile
Add the exact path to JAVA Home (with the path from step 2 above)
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home
Save and exit
Check JAVA_Home using - echo $JAVA_HOME
I am running MACOS MOJAVE - 10.14.2 (18C54) on a Macbook Air with JAVA 8
OSX Yosemite, ZSH, and Java SE Runtime Environment 8, I had to:
$ sudo ln -s /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands /System/Library/Frameworks/JavaVM.framework/Versions/Current/bin
and in ~/.zshrc change JAVA_HOME to
export JAVA_HOME="/System/Library/Frameworks/JavaVM.framework/Versions/Current"
For Mac OS X 10.9 I installed the latest version of JRE from Oracle and then reset the JAVA_HOME to /Library/Java/JavaVirtualMachines/jdk1.7.0_45.jdk/Contents/Home.
I am sure there is a better way but this got me up and running.
hughsmac:~ hbrien$ echo $JAVA_HOME
/Library/Java/JavaVirtualMachines/jdk1.7.0_45.jdk/Contents/Home
For Java 11 (JDK 11) it can be located with the following command:
/usr/libexec/java_home -v 11
Got the same issue after I upgrade my Mac OS and following worked for me:
cmd>vi ~/.bash_profile
Add/update the line for JAVA_HOME:
export JAVA_HOME="/Library/Java/JavaVirtualMachines/jdk1.7.0_15.jdk/Contents/Home"
cmd>source ~/.bash_profile or open a new terminal
I think the jdk version might differ, so just use the version which you have under /Library/Java/JavaVirtualMachines/
If you are in need to have multiple versions of JDK under Mac OS X (Yosemite), it might be helpful to add some scripting for automated switching between them.
What you do is to edit your ~/.bash_profile and add the following:
function setjdk() {
if [ $# -ne 0 ]; then
removeFromPath '/System/Library/Frameworks/JavaVM.framework/Home/bin'
if [ -n "${JAVA_HOME+x}" ]; then
removeFromPath $JAVA_HOME
fi
export JAVA_HOME=`/usr/libexec/java_home -v $#`
export PATH=$JAVA_HOME/bin:$PATH
fi
}
function removeFromPath() {
export PATH=$(echo $PATH | sed -E -e "s;:$1;;" -e "s;$1:?;;")
}
setjdk 1.7
What the script does is to first remove other JDK versions in the PATH so that they won’t interfere with our new JDK version. Then it makes some clever use of /usr/libexec/java_home which is a command that lists installed JDK versions. The -v argument tells java_home to return the path of the JDK with the supplied version, for example 1.7. We also update the PATH to point to the bin directory of the newly found JAVA_HOME directory. At the end we can simply execute the function using
setjdk 1.7
which selects the latest installed JDK version of the 1.7 branch. To select a specific version you can simply execute
setjdk 1.7.0_51
instead. Run /usr/libexec/java_home -V to get more details on how to choose versions.
P.S. Do not forget to source ~/.bash_profile after you save it.
For Fish terminal users on Mac (I believe it's available on Linux as well), this should work:
set -Ux JAVA_8 (/usr/libexec/java_home --version 1.8)
set -Ux JAVA_12 (/usr/libexec/java_home --version 12)
set -Ux JAVA_HOME $JAVA_8 //or whichever version you want as default
This answer is related to Mountain Lion and not Lion. I needed to do this for the AWS Command Line Tools. According to the AWS docs, running which java returns /usr/bin/java.
So, I set JAVA_HOME=/usr in my .bashrc.
Apparently, /usr/bin/java is a symbolic link to /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/java which makes it all work.
Update
As mentioned in the comment below, this JAVA_HOME value is not an ideal solution when the JAVA_HOME environment variable is to be used by things other than the AWS Command Line Tools. It works fine for the AWS Command Line Tools, though, as given in their docs.
for mac user .
java 8 should add
export JAVA_HOME=`/usr/libexec/java_home -v 1.8`
# JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_05.jdk/Contents/Home
java 6 :
export JAVA_HOME=`/usr/libexec/java_home -v 1.6`
# JAVA_HOME=/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
ref :http://qiita.com/seri_k/items/e978c1339ce51f13e297
For Mac Yosemite,
JDK 1.7.0_xx is using
$ ls -ltar /usr/bin/java
/System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/java
JAVA_HOME
/Library/Java/JavaVirtualMachines/jdk1.7.0_xx.jdk/Contents/Home
Anyone using AUSKEY from the Australian Tax Office (ATO) should uninstall AUSKEY.
This sorted out my JAVA_HOME issues.
It is also no longer required for MAC users. Yah!
I'm using Fish shell on High Sierra 10.13.4 and installed Java via Brew.
It's not automatically set up so to set it correctly on my system I run:
set -U JAVA_HOME (/usr/libexec/java_home)
Just set java_home of 1.8 jdk version in netbeans.conf file:
/Applications/NetBeans/NetBeans 8.2.app/Contents/Resources/NetBeans/etc/netbeans.conf
uncomment line:
netbeans_jdkhome="path/to/jdk"
and set path to your 1.8 jdk, in my case:
netbeans_jdkhome="/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home"
This approach lays you to have several jdk versions on mac os
Handy command for knowing java home and other details.
java -XshowSettings:properties -version
and to know all java homes on your mac:
/usr/libexec/java_home -V
I have been allocated a Linux box in which has java available
# java -version
java version "1.7.0_09-icedtea"
OpenJDK Runtime Environment (rhel-2.3.4.1.el6_3-x86_64)
OpenJDK 64-Bit Server VM (build 23.2-b09, mixed mode)
# ls -l /usr/bin/java
lrwxrwxrwx. 1 root root 22 Feb 8 2013 /usr/bin/java -> /etc/alternatives/java
I am new to Java and not sure if JRE is installed in ths box but based upon search:
# rpm -q jre
package jre is not installed
# find / -iname java -print 2>/dev/null
/usr/lib/java
/usr/lib/jvm/java-1.7.0-openjdk-1.7.0.9.x86_64/jre/bin/java
/usr/lib/jvm/java-1.6.0-openjdk-1.6.0.0.x86_64/jre/bin/java
/usr/lib/jvm/java-1.5.0-gcj-1.5.0.0/jre/bin/java
/usr/bin/java
/usr/share/java
/var/lib/alternatives/java
/etc/alternatives/java
/etc/java
/etc/pki/java
But
# echo $JAVA_HOME
# echo $JRE_HOME
So is JAVA is installed and JRE also - am I correct but what value I should set for JAVA_HOME and JRE_HOME env variables?
I am not sure about any other version of Linux, but in Ubuntu you need open home/.bashrc and add in the end of file
export JAVA_HOME=/usr/lib/jvm/java-1.7.0-openjdk-1.7.0.9.x86_64
export PATH=$JAVA_HOME/bin:$PATH
after that, save file and relogin.
EDIT
java will know where is jre by JAVA_HOME.
firstly try to get out of root user if possible than after that change below in your ~/.bash_profile
JAVA_HOME=/usr/java/<Java version 7 jdk>; export JAVA_HOME // you can also try JAVA_HOME=/usr/java/jdk 7 version/bin/java
PATH=$JAVA_HOME/bin:$PATH; export PATH
save it and then
now source ~/.bashrc
after that try
echo $JAVA_HOME it will produce the expected result.
JAVA_HOME should be set as parent folder of the java installed path
and JRE_HOME should be /jre
set $JAVA_HOME=/usr/lib/jvm/java-1.7.0-openjdk-1.7.0.9.x86_64
set $JRE_HOME=/usr/lib/jvm/java-1.6.0-openjdk-1.6.0.0.x86_64/jre
Based on the output you have posted, i can draw the following conclusions:
Based on the fact that java -version works on your machine, you have the JRE installed.
The 'echo' statements do not produce any output as you have not set the environment variables for them.
On a side note, if you are planning on doing Java Development, then you need the JDK installed. The JRE comes as a subset of the JDK once installed
I'm trying to run Maven on cygwin. I've added maven to the path and my Java home looks like this:
$ echo $JAVA_HOME
/cygdrive/c/Program Files/Java/jdk1.6.0
Yet when I try to run mvn --version, I get this:
Error: JAVA_HOME is not defined correctly.
We cannot execute /cygdrive/c/Program Files/Java/jdk1.7.0_01/bin/java
Any idea why it's not working? Java -version works fine, so it's not a problem with java. I've also tried jdk1.6.0 instead.
Running which java gives,
$ which java
/cygdrive/c/windows/system32/java
and java -version gives,
$ java -version
java version "1.7.0_01"
Java(TM) SE Runtime Environment (build 1.7.0_01-b08)
Java HotSpot(TM) Client VM (build 21.1-b02, mixed mode, sharing)
If I run mvn.bat instead of mvn, I didn't have this problem.
So, since Cygwin will run the mvn file, I replaced this mvn file with a softlink (ln -s mvn.bat mvn) to the batch file. This way, running mvn will silently run the mvn.bat which handles these problems gracefully.
Anyone Using Cygwin and Maven, here's exactly what you need:
In your Cygwin bash prompt:
$ vim ~/.bashrc
or:
$ nano ~/.bashrc
(Which ever you fancy...)
Append the following:
alias mvn=mvn.bat
Save and Exit. Then run:
$ source ~/.bashrc
Now you should be able to use the mvn command as you do in cmd prompt.
Cygwin uses a dos console to execute Maven builds (mvn.bat).
Set your Java home to the C:\Program Files\Java\jdk1.7.0_01 location.
I was facing the same problem while running gradle from cygwin tool.
I used to set the Java home including the bin folder:
C:\Program Files\Java\jdk1.7.0_67\bin
But later I realized that some application do not recognize if you include bin folder, so I changed the class path to C:\Program Files\Java\jdk1.7.0_67 and it started working.
Setting JAVA_HOME to /cygdrive/c/Progra~1/Java/jdk1.6.0 might help.