JDK,JRE在mac OS X中的目錄結構
首先要明確一個問題,JDK與JRE的區別是什麼?
JRE: Java Runtime Environment. It is basically the Java Virtual Machine where your Java programs run on. It also includes browser plugins for Applet execution.
JDK: It’s the full featured Software Development Kit for Java, including JRE, and the compilers and tools (like JavaDoc, and Java Debugger) to create and compile programs.
對應於命令列模式:
java -version 顯示JRE版本
JDK包含了JRE,是一個工具集
javac -version 顯示JDK版本
那麼,顯示的是什麼版本,是系統的版本還是命令的版本?
這裡需要知道另外一個命令:
/urs/libexec/java_home -V 顯示系統中所有的JDK版本
比如我的macbook上的顯示:
1.8.0_45, x86_64: "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_45.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
預設用的是第一個版本,繼續:
echo $JAVA_HOME
輸出為空
export JAVA_HOME=”`/usr/libexec/java_home -v ‘1.6*’` “
輸出:
/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
此時如果輸入
java -version 或者 javac -version 顯示的版本都是1.6.0,改動之前是1.8.0
猜想:執行java和javac命令受到java_home的影響,java_home改變了這兩個命令的真實位置。
下面來尋找它們之間的關係:
1.找到java
javac
命令的真實位置
> which java; which javac
/usr/bin/java
/usr/bin/javac
> ll /usr/bin/java; ll /usr/bin/javac
/usr/bin/java -> /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/java
/usr/bin/javac -> /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/javac
挖掘一下後面的一長串地址:
> ll /System/Library/Frameworks/JavaVM.framework/Versions/
lrwxr-xr-x 1 root wheel 10 6 7 20:26 1.4 -> CurrentJDK
lrwxr-xr-x 1 root wheel 10 6 7 20:26 1.4.2 -> CurrentJDK
lrwxr-xr-x 1 root wheel 10 6 7 20:26 1.5 -> CurrentJDK
lrwxr-xr-x 1 root wheel 10 6 7 20:26 1.5.0 -> CurrentJDK
lrwxr-xr-x 1 root wheel 10 6 7 20:26 1.6 -> CurrentJDK
lrwxr-xr-x 1 root wheel 10 6 7 20:26 1.6.0 -> CurrentJDK
drwxr-xr-x 7 root wheel 238 6 7 20:27 A
lrwxr-xr-x 1 root wheel 1 6 7 20:26 Current -> A
lrwxr-xr-x 1 root wheel 59 6 7 20:26 CurrentJDK -> /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents
> ll /System/Library/Frameworks/JavaVM.framework/Versions/Current/
total 80
drwxr-xr-x 45 root wheel 1530 6 7 20:27 Commands
drwxr-xr-x 4 root wheel 136 4 15 01:19 Frameworks
-rwxr-xr-x 1 root wheel 105200 5 28 2014 JavaVM
drwxr-xr-x 42 root wheel 1428 6 7 20:27 Resources
drwxr-xr-x 3 root wheel 102 6 7 20:27 _CodeSignature
在Commands中找到java和javac,執行它們,得到的版本號與
export JAVA_HOME=” `/usr/libexec/java_home -v ‘1.8*’ `”
> /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/java -version
顯示為版本1.8
基本上到這裡,可以得出一個結論:java和javac使用的真實命令位置,由$JAVA_HOME決定,它決定了命令列使用的JDK版本。
這個機制是怎樣的?
當我們 > export JAVA_HOME
,usr/libexec/java_home -v '1.8*'
給出了JDK的真實位置:
/Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home
來看看這個目錄有什麼:
ll /Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home
total 52192
-rw-rw-r– 1 root wheel 3244 4 11 02:11 COPYRIGHT
-rw-rw-r– 1 root wheel 40 4 11 02:12 LICENSE
-rw-rw-r– 1 root wheel 159 4 11 02:12 README.html
-rw-rw-r– 1 root wheel 110114 3 10 08:47 THIRDPARTYLICENSEREADME-JAVAFX.txt
-rw-rw-r– 1 root wheel 178403 4 11 02:12 THIRDPARTYLICENSEREADME.txt
drwxrwxr-x 46 root wheel 1564 4 11 02:21 bin
drwxrwxr-x 9 root wheel 306 4 11 02:11 db
drwxrwxr-x 9 root wheel 306 4 11 02:11 include
-rw-rw-r– 1 root wheel 5172318 3 10 08:47 javafx-src.zip
drwxrwxr-x 10 root wheel 340 4 11 02:14 jre
drwxrwxr-x 14 root wheel 476 4 11 02:14 lib
drwxrwxr-x 5 root wheel 170 4 11 02:12 man
-rw-rw-r– 1 root wheel 528 4 11 02:12 release
-rw-rw-r– 1 root wheel 21239600 4 11 02:12 src.zip
令人驚奇是,這個目錄下面的bin中的命令,
與java
命令所在目錄/System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/
是一樣的,也就是說 java
其實執行的是bin中命令java
待續