Apple JDK(s) Location: $ ls -latr /System/Library/Java/JavaVirtualMachines
Oracle JDK(s) Location: $ ls -latr /Library/Java/JavaVirtualMachines
Apple Versions Location: /System/Library/Frameworks/JavaVM.framework/Versions
There are 2 ways to update java version on mac:
1. Following are equivalent for ~/.bash_profile
* export JAVA_HOME=$(/usr/libexec/java_home -v 1.7)
* export JAVA_HOME=`/usr/libexec/java_home -v '1.7*'`
2. update the softlink like below on mac:
* $ java -version
* $ cd /System/Library/Frameworks/JavaVM.framework/Versions/
* $ rm CurrentJDK
* $ ln -s /Library/Java/JavaVirtualMachines/jdk1.7.0_21.jdk/Contents/ CurrentJDK
* $ java -version
even after updating version by above mechanism maven still pointing to mac-osx 1.6, because it internally use mac java 6:
$ java -version: java version "1.7.0_51"
$ mvn -version: Apache Maven 3.2.1
When i check it '$M2_HOME/bin/mvn' we can see during start-up, maven look for 2 files '/etc/mavenrc' and '~/.mavenrc'.
So my /etc/mavenrc looks like this:
JAVA_HOME=`/usr/libexec/java_home` # pick the latest version
or we can also define java specific version as : JAVA_HOME=`/usr/libexec/java_home -v 1.7`
bash-style: $ export JAVA_HOME=`/usr/libexec/java_home`
csh-style: % setenv JAVA_HOME `/usr/libexec/java_home`
vi ~/.bash_profile
export JAVA_HOME=$(/usr/libexec/java_home -v 1.7)
# export M2_HOME="/Users/adixit/Documents/XYZ/apache-maven-3.2.3" # brew install take care of setting path
# export PATH="$PATH:${HOME}/Documents/XYZ/apache-maven-3.2.3/bin" # brew install take care of setting path
$ echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
sudo ln -nsf /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents /System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
First run /usr/libexec/java_home -V which will output something like the following:SISs-MacBook-Pro:Commands adixit$ /usr/libexec/java_home -V
Matching Java Virtual Machines (4):
1.8.0_25, x86_64: "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home
1.7.0_72, x86_64: "Java SE 7" /Library/Java/JavaVirtualMachines/jdk1.7.0_72.jdk/Contents/Home
1.6.0_65-b14-466.1, x86_64: "Java SE 6" /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
1.6.0_65-b14-466.1, i386: "Java SE 6" /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home
Pick the version you want to be the default (1.8.0_25 for arguments sake) then:
export JAVA_HOME=`/usr/libexec/java_home -v 1.8.0_25`
SISs-MacBook-Pro:Commands adixit$ java -version
java version "1.8.0_25"
Java(TM) SE Runtime Environment (build 1.8.0_25-b17)
Java HotSpot(TM) 64-Bit Server VM (build 25.25-b02, mixed mode)
SISs-MacBook-Pro:Commands adixit$
Just add the export JAVA_HOME… line to your shell’s init file.
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
After installing java 7 you will need to do the sudo ln -snf in order to change the link to current java. To do so, open Terminal and issue the command:$ sudo ln -nsf /Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents /System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK
to set JAVA_HOME:
export JAVA_HOME="/Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home"
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
We can also use jenv script to automatically do execute above task using script.
/System/Library/Frameworks/JavaVM.framework/Versions
/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
AppleJava6: /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
OracleJava7: /Library/Java/JavaVirtualMachines/jdk1.7.0_72.jdk/Contents/Home
OracleJava8: /Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home
MacBook-Pro:~ adixit$ jenv add /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
oracle64-1.6.0.65 added
1.6.0.65 added
1.6 added
MacBook-Pro:~ adixit$ jenv add /Library/Java/JavaVirtualMachines/jdk1.7.0_72.jdk/Contents/Home
oracle64-1.7.0.72 added
1.7.0.72 added
1.7 added
MacBook-Pro:~ adixit$ jenv add /Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home
oracle64-1.8.0.25 added
1.8.0.25 added
1.8 added
MacBook-Pro:~ adixit$ jenv global 1.7.0.72
MacBook-Pro:~ adixit$ jenv local 1.7.0.72
MacBook-Pro:~ adixit$ jenv shell 1.7.0.72
MacBook-Pro:~ adixit$ jenv versions
References:
- Apple way to became root on mac appledoc & terminal.
- Give sudo access on macbook.
- Change java version on mac and source at github.
- jenv utility that let you change environment on mac.
- oracle blog
- jenv
No comments:
Post a Comment