springboot项目结合jdk11遇到的问题

随着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

再次启动,警告消失,启动正常

 

转载于:https://www.cnblogs.com/yjcblog/p/10332877.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值