一、執行run方法後java.exe調用jvm.dll文件建立jvm虛擬
二、建立虛擬機的過程當中建立出引導類加載器實例(c++)
三、c++代碼調用java代碼建立jvm啓動器
sum.misc.Launcher.getLauncher()
四、launcher.getClassLoader()進行java類的加載
加載-》驗證-》準備—》解析—》初始化 1.驗證:驗證格式是否正確 2.準備:靜態變量初值始賦值 Ex:int=0,boolean=false 3.解析:符號引用替換爲直接引用(靜態引用) 4.初始化:初始化靜態變量和靜態代碼
五、執行類中的main方法
六、執行結束,銷燬JVM
java類加載器分爲四層
引導類加載器、擴展類加載器、應用程序類加載器、自定義加載器
頂級加載器爲引導類加載器BootstrapClass,該加載器爲C++代碼實現,負責加載支持jvm運行的jdk下lib目錄下的核心類,E.x:java.lang.String類
bootstrapLoader加載如下文件:java
file:/C:/Program%20Files/Java/jdk1.8.0_271/jre/lib/resources.jar file:/C:/Program%20Files/Java/jdk1.8.0_271/jre/lib/rt.jar file:/C:/Program%20Files/Java/jdk1.8.0_271/jre/lib/sunrsasign.jar file:/C:/Program%20Files/Java/jdk1.8.0_271/jre/lib/jsse.jar file:/C:/Program%20Files/Java/jdk1.8.0_271/jre/lib/jce.jar file:/C:/Program%20Files/Java/jdk1.8.0_271/jre/lib/charsets.jar file:/C:/Program%20Files/Java/jdk1.8.0_271/jre/lib/jfr.jar file:/C:/Program%20Files/Java/jdk1.8.0_271/jre/classes
二級加載器爲擴展類加載器ExtClassLoader,該加載器爲java代碼實現,負責加載支持jvm運行的的jdk中的lib文件下ext中的擴展包c++
C:\Program Files\Java\jdk1.8.0_271\jre\lib\ext; C:\Windows\Sun\Java\lib\ext
三級加載器爲AppClassLoader負責加載classPath目錄下的文件,主要就是編寫的業務實現類
appClassLoader加載如下文件:web
C:\Program Files\Java\jdk1.8.0_271\jre\lib\charsets.jar; C:\Program Files\Java\jdk1.8.0_271\jre\lib\deploy.jar; C:\Program Files\Java\jdk1.8.0_271\jre\lib\ext\access-bridge-64.jar; C:\Program Files\Java\jdk1.8.0_271\jre\lib\ext\cldrdata.jar; C:\Program Files\Java\jdk1.8.0_271\jre\lib\ext\dnsns.jar; C:\Program Files\Java\jdk1.8.0_271\jre\lib\ext\jaccess.jar; C:\Program Files\Java\jdk1.8.0_271\jre\lib\ext\jfxrt.jar; C:\Program Files\Java\jdk1.8.0_271\jre\lib\ext\localedata.jar; C:\Program Files\Java\jdk1.8.0_271\jre\lib\ext\nashorn.jar ;C:\Program Files\Java\jdk1.8.0_271\jre\lib\ext\sunec.jar; C:\Program Files\Java\jdk1.8.0_271\jre\lib\ext\sunjce_provider.jar; C:\Program Files\Java\jdk1.8.0_271\jre\lib\ext\sunmscapi.jar; C:\Program Files\Java\jdk1.8.0_271\jre\lib\ext\sunpkcs11.jar; C:\Program Files\Java\jdk1.8.0_271\jre\lib\ext\zipfs.jar; C:\Program Files\Java\jdk1.8.0_271\jre\lib\javaws.jar; C:\Program Files\Java\jdk1.8.0_271\jre\lib\jce.jar; C:\Program Files\Java\jdk1.8.0_271\jre\lib\jfr.jar; C:\Program Files\Java\jdk1.8.0_271\jre\lib\jfxswt.jar; C:\Program Files\Java\jdk1.8.0_271\jre\lib\jsse.jar; C:\Program Files\Java\jdk1.8.0_271\jre\lib\management-agent.jar; C:\Program Files\Java\jdk1.8.0_271\jre\lib\plugin.jar; C:\Program Files\Java\jdk1.8.0_271\jre\lib\resources.jar; C:\Program Files\Java\jdk1.8.0_271\jre\lib\rt.jar;e:\IdeaProjects\demo3\target\classes; C:\Users\Administrator\.m2\repository\org\springframework\boot\spring-boot-starter-web\2.4.2\spring-boot-starter-web-2.4.2.jar; C:\Users\Administrator\.m2\repository\org\springframework\boot\spring-boot-starter\2.4.2\spring-boot-starter-2.4.2.jar; C:\Users\Administrator\.m2\repository\org\springframework\boot\spring-boot\2.4.2\spring-boot-2.4.2.jar; C:\Users\Administrator\.m2\repository\org\springframework\boot\spring-boot-autoconfigure\2.4.2\spring-boot-autoconfigure-2.4.2.jar; C:\Users\Administrator\.m2\repository\org\springframework\boot\spring-boot-starter-logging\2.4.2\spring-boot-starter-logging-2.4.2.jar; C:\Users\Administrator\.m2\repository\ch\qos\logback\logback-classic\1.2.3\logback-classic-1.2.3.jar; C:\Users\Administrator\.m2\repository\ch\qos\logback\logback-core\1.2.3\logback-core-1.2.3.jar; C:\Users\Administrator\.m2\repository\org\apache\logging\log4j\log4j-to-slf4j\2.13.3\log4j-to-slf4j-2.13.3.jar; C:\Users\Administrator\.m2\repository\org\apache\logging\log4j\log4j-api\2.13.3\log4j-api-2.13.3.jar; C:\Users\Administrator\.m2\repository\org\slf4j\jul-to-slf4j\1.7.30\jul-to-slf4j-1.7.30.jar; C:\Users\Administrator\.m2\repository\jakarta\annotation\jakarta.annotation-api\1.3.5\jakarta.annotation-api-1.3.5.jar; C:\Users\Administrator\.m2\repository\org\yaml\snakeyaml\1.27\snakeyaml-1.27.jar; C:\Users\Administrator\.m2\repository\org\springframework\boot\spring-boot-starter-json\2.4.2\spring-boot-starter-json-2.4.2.jar; C:\Users\Administrator\.m2\repository\com\fasterxml\jackson\core\jackson-databind\2.11.4\jackson-databind-2.11.4.jar; C:\Users\Administrator\.m2\repository\com\fasterxml\jackson\core\jackson-annotations\2.11.4\jackson-annotations-2.11.4.jar; C:\Users\Administrator\.m2\repository\com\fasterxml\jackson\core\jackson-core\2.11.4\jackson-core-2.11.4.jar; ……………………因爲加載太多,此處省略…………………………
四級加載器爲用戶自定義加載器,實現方式和三級加載器相似,可自定義loadClass方法調用父類loadClass方法實現類的加載。spring
java類加載器的加載順序以下圖所示
AppClassLoader邏輯上的父級加載器爲ExtClassLoader,Ext的父級則是BootstrapClassLoader
類加載的順序是由APP首先檢查自身是否已經加載過,若是沒有,調用父級ExtClassLoader,若是ExtClassLoader也沒有加載過則調用BootstrapClassLoader,三級加載器會從最高級開始在本身的加載範圍內尋尋找類型,若是沒有找到則交給子集類加載器進行加載。
邏輯順序是3-2-1進行查找,都沒有找到則從1-2-3順序新建,建立成功或者查找到後則直接返回apache
實現自定義加載器,重構classLoad()方法的實現,跳過雙親委派邏輯即刻json