JVM是經過一個稱爲ClassLoader東西來加載Class文件的,每當JVM啓動,它就會生成三個ClassLoader,它們分別是Bootstrap Loader, ExtClassLoader 和AppClassLoader。這三個ClassLoader做用是不一樣的,它們所加載的class文件也是不一樣的。java
##Bootstrap Loader## Bootstrap Loader是用C++語言實現的,它所加載的是JVM中最底層的類,它加載時的搜索路徑是由sun.boot.class.path所指定的。ide
System.out.println(System.getProperty("sun.boot.class.path"));
輸出結果以下: /home/taomk/opt/jdk1.7.0_51/jre/lib/resources.jar: /home/taomk/opt/jdk1.7.0_51/jre/lib/rt.jar: /home/taomk/opt/jdk1.7.0_51/jre/lib/sunrsasign.jar: /home/taomk/opt/jdk1.7.0_51/jre/lib/jsse.jar: /home/taomk/opt/jdk1.7.0_51/jre/lib/jce.jar: /home/taomk/opt/jdk1.7.0_51/jre/lib/charsets.jar: /home/taomk/opt/jdk1.7.0_51/jre/lib/jfr.jar: /home/taomk/opt/jdk1.7.0_51/jre/classescode
##ExtClassLoader## ExtClassLoader是用來加載java的一些庫的,它加載時的搜索路徑是由java.ext.dirs來決定的,該加載器在加載時不一樣於其餘加載器,它加載時會搜索指定路徑下的全部子目錄,也就是說它會搜索java.ext.dirs所指定下的全部子目錄下的class文件或jar文件。同時也能夠用參數-Djava.ext.dirs來改變它的搜索路徑。dns
System.out.println(System.getProperty("java.ext.dirs"));
輸出結果以下: /home/taomk/opt/jdk1.7.0_51/jre/lib/ext: /usr/java/packages/lib/extip
##AppClassLoader## AppClassLoader也稱System ClassLoader, 它的搜索路徑是由java.class.path來指定的,並且要注意AppClassLoader不會搜索java.class.path下的子目錄的。get
System.out.println(System.getProperty("java.class.path"));
輸出結果以下:class
/home/taomk/opt/jdk1.7.0_51/jre/lib/jce.jar: /home/taomk/opt/jdk1.7.0_51/jre/lib/resources.jar: /home/taomk/opt/jdk1.7.0_51/jre/lib/deploy.jar: /home/taomk/opt/jdk1.7.0_51/jre/lib/jfxrt.jar: /home/taomk/opt/jdk1.7.0_51/jre/lib/javaws.jar: /home/taomk/opt/jdk1.7.0_51/jre/lib/jsse.jar: /home/taomk/opt/jdk1.7.0_51/jre/lib/rt.jar: /home/taomk/opt/jdk1.7.0_51/jre/lib/management-agent.jar: /home/taomk/opt/jdk1.7.0_51/jre/lib/plugin.jar: /home/taomk/opt/jdk1.7.0_51/jre/lib/charsets.jar: /home/taomk/opt/jdk1.7.0_51/jre/lib/jfr.jar: /home/taomk/opt/jdk1.7.0_51/jre/lib/ext/sunec.jar: /home/taomk/opt/jdk1.7.0_51/jre/lib/ext/localedata.jar: /home/taomk/opt/jdk1.7.0_51/jre/lib/ext/zipfs.jar: /home/taomk/opt/jdk1.7.0_51/jre/lib/ext/dnsns.jar: /home/taomk/opt/jdk1.7.0_51/jre/lib/ext/sunjce_provider.jar: /home/taomk/opt/jdk1.7.0_51/jre/lib/ext/sunpkcs11.jar:jdk
ExtClassLoader 和AppClassLoader在JVM中只會存在一個實例,一旦運行了是沒法改變它們的搜索路徑的,也就是沒法在程序中經過調用System.setProperty()來改變java.class.path或java.ext.dirs來改變它們的搜索路徑的。搜索