原生android 4.4.4 (KitKat)開始使用Java 7,但如今芯片廠商的codebase用的仍是Java 6,須要來回切換,糾結吧~html
先說一下原生android KitKat,參照android developer官網,安裝openjdk便可:java
$ sudo apt-get update $ sudo apt-get install openjdk-7-jdk $ sudo update-alternatives --config java $ sudo update-alternatives --config javac
openjdk會直接安裝到/usr/bin/目錄下android
➜ ~T which javac /usr/bin/javac ➜ ~T javac -version javac 1.7.0_55
若是要切回Java 6,能夠經過修改環境變量,個人java 6是直接在oracle官網<link>下載,放入了~/bin/目錄裏,所以單獨把設置環境變量的腳本獨立出來shell
➜ ~ cat ~/bin/set_java_1.6 export JAVA_HOME=~/bin/jdk1.6.0_45 export PATH=$JAVA_HOME/bin:$PATH export CLASSPATH=$JAVA_HOME ➜ ~ source ~/bin/set_java_1.6 ➜ ~ which javac /home/caohong/bin/jdk1.6.0_45/bin/javac ➜ ~ javac -version javac 1.6.0_45
搞定。bash
更優雅的,參考<OSX setjdk>, 寫一個執行腳本oracle
#!/bin/bash function setjdk { #local vmdir=/System/Library/Frameworks/JavaVM.framework/Versions #修改爲本身的jdk所在的目錄,而且要把目錄名從jdk1.6.0_45改爲jdk/1.6, 執行起來:setjdk 1.6 local vmdir=~/bin/caohong/bin/jdk/ local ver=${1?Usage: setjdk <version>} [ -d $vmdir/$ver ] || { echo Unknown JDK version: $ver return 1 } echo -n "Setting this terminal's JDK to $ver ... " export JAVA_HOME=$vmdir/$ver/Home PATH=$(echo $PATH | tr ':' '\n' | grep -v $vmdir | tr '\n' ':') export PATH=$JAVA_HOME/bin:$PATH java -version }
這個參考腳本里,PATH目錄保證了不會由於執行屢次`setjdk`而把jdk目錄累加起來(tr的做用是把冒號換成換行符,以便用grep -v把原來設置的jdk刪除)app
另外,MAC系統裏,從10.5以後,java目錄能夠經過 /usr/libexec/java_home得到(詳細參考<apple developer: how to get jdk on max osx>): this
➜ ~ /usr/libexec/java_home /Library/Java/JavaVirtualMachines/jdk1.7.0_60.jdk/Contents/Home
對於<OSX setjdk>裏稍微有點過期,由於10.5以前,java的目錄都是放在/System/Library/Frameworks/JavaVM.framework/Versions 目錄下,default值是經過軟連接來指定的:spa
➜ ~ ls -l /System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK lrwxr-xr-x 1 root wheel 59 6 30 19:21 /System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK -> /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents