隨着jdk新版本的發布,后續也會慢慢的使用上了。由於jdk9開始的模塊化處理,如果按以前jdk9之前的版本來處理,工程就啟動不起來了。這里以簡單的spring-cloud-starter-netflix-eureka-server工程為例,記錄下一些遇到的問題:
1.項目構建問題:
首先工程是以gradle構建的,初始依賴:
dependencies { implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-server:2.1.0.RELEASE' testImplementation 'junit:junit:4.12' }
編譯后啟動項目,會發現下面的問題:
java.lang.TypeNotPresentException: Type javax.xml.bind.JAXBContext not present
...
Caused by: java.lang.ClassNotFoundException: javax.xml.bind.JAXBContext
可以看出來,缺失了相關的jar包。經過驗證,需更改依賴,添加如下jar包:
dependencies { implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-server:2.1.0.RELEASE' implementation 'com.google.code.gson:gson:2.8.5' implementation 'javax.xml.bind:jaxb-api:2.3.1' implementation 'com.sun.xml.bind:jaxb-impl:2.3.2' implementation 'com.sun.xml.bind:jaxb-core:2.3.0.1' implementation 'javax.activation:activation:1.1.1' testImplementation 'junit:junit:4.12' }
2.啟動警告問題:
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by com.thoughtworks.xstream.core.util.Fields (file:/C:/.../xstream-1.4.10.jar) to field java.util.TreeMap.comparator
WARNING: Please consider reporting this to the maintainers of com.thoughtworks.xstream.core.util.Fields
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
網上找了下,很多都在說要降低Jdk版本,但我們本身就是為了升級,如果還降下去,就沒意義了。可以按照提示,啟動命令添加 --illegal-access=warn參數,再次查看日志信息
WARNING: Illegal reflective access by com.thoughtworks.xstream.core.util.Fields (file:/C:/.../com.thoughtworks.xstream/xstream/1.4.10/dfecae23647abc9d9fd0416629a4213a3882b101/xstream-1.4.10.jar) to field java.util.TreeMap.comparator
WARNING: Illegal reflective access by com.thoughtworks.xstream.core.util.Fields (file:/C:/.../com.thoughtworks.xstream/xstream/1.4.10/dfecae23647abc9d9fd0416629a4213a3882b101/xstream-1.4.10.jar) to field java.util.TreeSet.m
WARNING: Illegal reflective access by com.thoughtworks.xstream.core.util.Fields (file:/C:/.../com.thoughtworks.xstream/xstream/1.4.10/dfecae23647abc9d9fd0416629a4213a3882b101/xstream-1.4.10.jar) to field java.util.Properties.defaults
WARNING: Illegal reflective access by com.thoughtworks.xstream.core.util.Fields (file:/C:/.../com.thoughtworks.xstream/xstream/1.4.10/dfecae23647abc9d9fd0416629a4213a3882b101/xstream-1.4.10.jar) to field java.lang.reflect.Proxy.h
WARNING: Illegal reflective access by com.thoughtworks.xstream.converters.reflection.AbstractAttributedCharacterIteratorAttributeConverter (file:/C:/.../com.thoughtworks.xstream/xstream/1.4.10/dfecae23647abc9d9fd0416629a4213a3882b101/xstream-1.4.10.jar) to method java.text.AttributedCharacterIterator$Attribute.getName()
WARNING: Illegal reflective access by com.thoughtworks.xstream.core.util.Fields (file:/C:/.../com.thoughtworks.xstream/xstream/1.4.10/dfecae23647abc9d9fd0416629a4213a3882b101/xstream-1.4.10.jar) to field java.awt.font.TextAttribute.instanceMap
WARNING: Illegal reflective access by com.thoughtworks.xstream.core.util.Fields (file:/C:/.../com.thoughtworks.xstream/xstream/1.4.10/dfecae23647abc9d9fd0416629a4213a3882b101/xstream-1.4.10.jar) to field java.util.EnumSet.elementType
WARNING: Illegal reflective access by com.thoughtworks.xstream.core.util.Fields (file:/C:/.../com.thoughtworks.xstream/xstream/1.4.10/dfecae23647abc9d9fd0416629a4213a3882b101/xstream-1.4.10.jar) to field java.util.EnumMap.keyType
可以看出來,這幾個地方,都是使用了反射來獲取信息,但對應權限並沒有開放,導致警告。可以通過添加--add-opens來顯式允許代碼反射訪問對應模塊
根據警告信息,添加如下啟動參數:
--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
再次啟動,警告消失,啟動正常