現象
idea啓動項目時出現警告:
"/Applications/IntelliJ IDEA.app/Contents/jbr/Contents/Home/bin/java" ...
Connected to the target VM, address: '127.0.0.1:62251', transport: 'socket'
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by com.google.inject.internal.cglib.core.$ReflectUtils$1 (file:/Users/lizz/.m2/repository/com/google/inject/guice/4.1.0/guice-4.1.0.jar) to method java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int,java.security.ProtectionDomain)
WARNING: Please consider reporting this to the maintainers of com.google.inject.internal.cglib.core.$ReflectUtils$1
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
原因分析
錯誤提示:com.google.inject.internal.cglib.core.$ReflectUtils$1訪問了java.lang.ClassLoader類。
因爲JDK9的新特性模塊系統(module system)對jdk中的模塊進行了反射檢查,對於反射了jdk中的類的操作進行警告。
處理辦法
方法一:降低jdk版本
將idea中的項目運行環境從高版本調整爲JDK8即可,如果moudle格式,可能還需要將每個moudle單獨設置。
File>Project Structure>Project Settings>Project>Project SDK.
方法二:開放訪問權限
JDK9+可以使用啓動參數中增加“--add-opens <package>/<class>=ALL-UNNAMED”的方法運行其他模塊訪問。
在RUN>Edit Configurations>VM options 中增加參數,如:
--add-opens java.base/java.lang=ALL-UNNAMED --add-opens java.base/java.util=ALL-UNNAMED --add-opens java.desktop/java.awt.font=ALL-UNNAMED
--add-opens java.base/java.lang=ALL-UNNAMED --add-opens java.base/java.util=ALL-UNNAMED --add-opens java.base/java.lang.reflect=ALL-UNNAMED --add-opens java.base/java.text=ALL-UNNAMED --add-opens java.desktop/java.awt.font=ALL-UNNAMED