V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
summerlv
V2EX  ›  问与答

Java1.8 换版本为 openjdk17 后 springboot 程序跑不起来,求助~~~

  •  
  •   summerlv · 2022-03-01 16:46:36 +08:00 · 2459 次点击
    这是一个创建于 999 天前的主题,其中的信息可能已经有所发展或是发生改变。

    本来使用的是 oracle jdk1.8 跑的 springboot 2.3.0 程序,但是后面要求换为 openjdk17 ,这样一换后 springboot 跑不起来了。请问大家有遇到过换 java 版本的问题吗? 报错如下:

    org.springframework.beans.factory.BeanDefinitionStoreException: Failed to read candidate component class: file [D:\MIPS\MIPSAuth\mips-api\target\classes\com\mips\cloud\MipsApiApplication.class]; nested exception is org.springframework.core.NestedIOException: ASM ClassReader failed to parse class file - probably due to a new Java class file version that isn't supported yet: file [D:\MIPS\MIPSAuth\mips-api\target\classes\com\mips\cloud\MipsApiApplication.class]; nested exception is java.lang.IllegalArgumentException: Unsupported class file major version 61
        at org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider.scanCandidateComponents(ClassPathScanningCandidateComponentProvider.java:452) ~[spring-context-5.2.6.RELEASE.jar:5.2.6.RELEASE]
        at org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider.findCandidateComponents(ClassPathScanningCandidateComponentProvider.java:315) ~[spring-context-5.2.6.RELEASE.jar:5.2.6.RELEASE]
        at org.springframework.context.annotation.ClassPathBeanDefinitionScanner.doScan(ClassPathBeanDefinitionScanner.java:276) ~[spring-context-5.2.6.RELEASE.jar:5.2.6.RELEASE]
        at org.springframework.context.annotation.ComponentScanAnnotationParser.parse(ComponentScanAnnotationParser.java:132) ~[spring-context-5.2.6.RELEASE.jar:5.2.6.RELEASE]
        at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:295) ~[spring-context-5.2.6.RELEASE.jar:5.2.6.RELEASE]
        at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:249) ~[spring-context-5.2.6.RELEASE.jar:5.2.6.RELEASE]
        at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:206) ~[spring-context-5.2.6.RELEASE.jar:5.2.6.RELEASE]
        at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:174) ~[spring-context-5.2.6.RELEASE.jar:5.2.6.RELEASE]
        at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:319) ~[spring-context-5.2.6.RELEASE.jar:5.2.6.RELEASE]
        at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:236) ~[spring-context-5.2.6.RELEASE.jar:5.2.6.RELEASE]
        at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:280) ~[spring-context-5.2.6.RELEASE.jar:5.2.6.RELEASE]
        at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:96) ~[spring-context-5.2.6.RELEASE.jar:5.2.6.RELEASE]
        at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:706) ~[spring-context-5.2.6.RELEASE.jar:5.2.6.RELEASE]
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:532) ~[spring-context-5.2.6.RELEASE.jar:5.2.6.RELEASE]
        at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:143) ~[spring-boot-2.3.0.RELEASE.jar:2.3.0.RELEASE]
        at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:758) ~[spring-boot-2.3.0.RELEASE.jar:2.3.0.RELEASE]
        at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:750) ~[spring-boot-2.3.0.RELEASE.jar:2.3.0.RELEASE]
        at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) ~[spring-boot-2.3.0.RELEASE.jar:2.3.0.RELEASE]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) ~[spring-boot-2.3.0.RELEASE.jar:2.3.0.RELEASE]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1237) ~[spring-boot-2.3.0.RELEASE.jar:2.3.0.RELEASE]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) ~[spring-boot-2.3.0.RELEASE.jar:2.3.0.RELEASE]
        at com.mips.cloud.MipsApiApplication.main(MipsApiApplication.java:12) ~[classes/:na]
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) ~[na:na]
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
        at java.base/java.lang.reflect.Method.invoke(Method.java:568) ~[na:na]
        at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) ~[spring-boot-devtools-2.3.0.RELEASE.jar:2.3.0.RELEASE]
    Caused by: org.springframework.core.NestedIOException: ASM ClassReader failed to parse class file - probably due to a new Java class file version that isn't supported yet: file [D:\MIPS\MIPSAuth\mips-api\target\classes\com\mips\cloud\MipsApiApplication.class]; nested exception is java.lang.IllegalArgumentException: Unsupported class file major version 61
        at org.springframework.core.type.classreading.SimpleMetadataReader.getClassReader(SimpleMetadataReader.java:60) ~[spring-core-5.2.6.RELEASE.jar:5.2.6.RELEASE]
        at org.springframework.core.type.classreading.SimpleMetadataReader.<init>(SimpleMetadataReader.java:49) ~[spring-core-5.2.6.RELEASE.jar:5.2.6.RELEASE]
        at org.springframework.core.type.classreading.SimpleMetadataReaderFactory.getMetadataReader(SimpleMetadataReaderFactory.java:103) ~[spring-core-5.2.6.RELEASE.jar:5.2.6.RELEASE]
        at org.springframework.core.type.classreading.CachingMetadataReaderFactory.getMetadataReader(CachingMetadataReaderFactory.java:123) ~[spring-core-5.2.6.RELEASE.jar:5.2.6.RELEASE]
        at org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider.scanCandidateComponents(ClassPathScanningCandidateComponentProvider.java:429) ~[spring-context-5.2.6.RELEASE.jar:5.2.6.RELEASE]
        ... 26 common frames omitted
    Caused by: java.lang.IllegalArgumentException: Unsupported class file major version 61
        at org.springframework.asm.ClassReader.<init>(ClassReader.java:196) ~[spring-core-5.2.6.RELEASE.jar:5.2.6.RELEASE]
        at org.springframework.asm.ClassReader.<init>(ClassReader.java:177) ~[spring-core-5.2.6.RELEASE.jar:5.2.6.RELEASE]
        at org.springframework.asm.ClassReader.<init>(ClassReader.java:163) ~[spring-core-5.2.6.RELEASE.jar:5.2.6.RELEASE]
        at org.springframework.asm.ClassReader.<init>(ClassReader.java:284) ~[spring-core-5.2.6.RELEASE.jar:5.2.6.RELEASE]
        at org.springframework.core.type.classreading.SimpleMetadataReader.getClassReader(SimpleMetadataReader.java:57) ~[spring-core-5.2.6.RELEASE.jar:5.2.6.RELEASE]
        ... 30 common frames omitted
     
     
    Process finished with exit code 0
    
    13 条回复    2022-03-02 10:19:16 +08:00
    Jooooooooo
        1
    Jooooooooo  
       2022-03-01 16:47:31 +08:00
    报错信息不是很清晰吗: Unsupported class file major version 61, 这说的就是 jdk 的版本.
    fanchenio
        2
    fanchenio  
       2022-03-01 16:51:16 +08:00
    你确定是用 jdk17 跑的么,上面提示说了不支持类文件主版本 61 ,也就是不支持 JDK17 ,怀疑是你启动项目时用错的 JDK ?没有切换到 JDK17 ?
    raptium
        3
    raptium  
       2022-03-01 16:52:45 +08:00
    应该是 Spring Boot 版本太低了。我还没有写过 Java 17 的 Spring Boot 项目,不过搜到一篇这个 https://blog.codecentric.de/en/2021/12/migrating-spring-boot-java-17/
    summerlv
        4
    summerlv  
    OP
       2022-03-01 16:55:14 +08:00
    我看了下 idea 中的 Project Structure 啊,是 17 版本,而且确认了每个 module 的 Sources 和 Dependences 都是 17 的
    fanchenio
        5
    fanchenio  
       2022-03-01 17:07:09 +08:00
    楼主去这个网址看一下: https://docs.spring.io/spring-boot/docs/2.3.12.RELEASE/reference/html/getting-started.html#getting-started ,这个是 SpringBoot2.3.x 版本文档,里面写了。

    Spring Boot 2.3.12.RELEASE requires Java 8 and is compatible up to Java 15 (included).

    好像只支持 JDK8~15 ?
    sagaxu
        6
    sagaxu  
       2022-03-01 17:13:01 +08:00 via Android
    8 到 17 步子太大,建议先 8 到 11 ,使用一段时间确认没有问题后,再 11 到 17
    potatowish
        7
    potatowish  
       2022-03-01 17:20:31 +08:00
    SpringBoot2.6 开始才支持 JDK17
    echo1937
        8
    echo1937  
       2022-03-01 18:02:50 +08:00 via iPhone
    @potatowish 好像是 2.5.5 就开始支持了,springboot initializer 就可以建出来了。
    echo1937
        9
    echo1937  
       2022-03-01 18:05:57 +08:00 via iPhone
    Spring Boot 2.5.5 requires Java 8 and is compatible up to and including Java 17. Spring Framework 5.3.10 or above is also required.

    Spring Boot 2.5.4 requires Java 8 and is compatible up to and including Java 16. Spring Framework 5.3.9 or above is also required.
    night98
        10
    night98  
       2022-03-01 22:54:28 +08:00
    maven xml 编译目标修改为 jdk17 ,然后 idea - maven 设置里面也是一样,跑起来应该没啥问题,估计会有点 bug 啥的
    summerlv
        11
    summerlv  
    OP
       2022-03-02 08:44:35 +08:00
    @raptium 这个问题目前解决了,就是 springboot 版本和 jdk 版本的问题,我将 springboot 版本升级到了 2.5.7 ,然后 springcloud 版本根据和 springboot 的对应关系升级为:2020.0.5 项目可以跑起来了。参考: https://blog.codecentric.de/en/2021/12/migrating-spring-boot-java-17/
    另外就是还要注意出现的一系列的其他包版本依赖问题。
    谢谢~

    但是目前项目有个问题。由于升级到了 jdk17 后,我的一个用于认证的微服务跑不起来了,其他微服务可以跑起来。感觉是和
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-oauth2</artifactId>
    <version>2.2.5.RELEASE</version>
    这个依赖有关,我看了下这个依赖会依赖一个叫 javax.xml.bind:jaxb-api:2.3.1 的依赖。

    然后我运行起来的报错主要是:Caused by: java.lang.NoSuchMethodException: Caused by:
    java.lang.NoSuchMethodException: sun.misc.Unsafe.defineClass(java.lang.String,[B,int,int,java.lang.ClassLoader,java.security.ProtectionDomain)
    at java.base/java.lang.Class.getMethod(Class.java:2227) ~[na:na]
    at com.sun.xml.bind.v2.runtime.reflect.opt.Injector$3.run(Injector.java:201) ~[jaxb-impl-2.3.0.1.jar:2.3.0.1]
    at com.sun.xml.bind.v2.runtime.reflect.opt.Injector$3.run(Injector.java:197) ~[jaxb-impl-2.3.0.1.jar:2.3.0.1]
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:569) ~[na:na]
    ... 74 common frames omitted

    我搜了下这种类型的问题是说包冲突啥的,我也没看明白:
    https://stackoverflow.com/questions/69637244/java-lang-nosuchmethodexception-sun-misc-unsafe-defineclassjava-lang-string-b

    https://github.com/mojohaus/jaxb2-maven-plugin/issues/182

    https://shiligui.blog.csdn.net/article/details/117125213

    目前我的环境是都调为了 17:

    <maven.compiler.source>17</maven.compiler.source>
    <maven.compiler.target>17</maven.compiler.target>
    <java.version>17</java.version>
    summerlv
        12
    summerlv  
    OP
       2022-03-02 08:50:28 +08:00
    报错如下:

    java.security.PrivilegedActionException: null
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:573) ~[na:na]
    at com.sun.xml.bind.v2.runtime.reflect.opt.Injector.<clinit>(Injector.java:197) ~[jaxb-impl-2.3.0.1.jar:2.3.0.1]


    Caused by: java.lang.NoSuchMethodException: sun.misc.Unsafe.defineClass(java.lang.String,[B,int,int,java.lang.ClassLoader,java.security.ProtectionDomain)
    at java.base/java.lang.Class.getMethod(Class.java:2227) ~[na:na]
    at com.sun.xml.bind.v2.runtime.reflect.opt.Injector$3.run(Injector.java:201) ~[jaxb-impl-2.3.0.1.jar:2.3.0.1]
    at com.sun.xml.bind.v2.runtime.reflect.opt.Injector$3.run(Injector.java:197) ~[jaxb-impl-2.3.0.1.jar:2.3.0.1]
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:569) ~[na:na]
    ... 74 common frames omitted
    summerlv
        13
    summerlv  
    OP
       2022-03-02 10:19:16 +08:00
    目前解决了 auth 启动不了的问题

    添加了依赖:

    <dependency>
    <groupId>org.glassfish.jaxb</groupId>
    <artifactId>jaxb-runtime</artifactId>
    <version>2.3.1</version>
    </dependency>

    然后运行起来连接数据库的时候出现了问题,搜了下是 druid 数据库连接池的问题,由 1.2.8 改成了 1.2.5
    <dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <!-- <version>1.2.8</version>-->
    <!--
    SqlServer driver validateConnection false #4451
    https://github.com/alibaba/druid/issues/4451 -->
    <version>1.2.5</version>
    </dependency>


    这次升级总算是完成了,真累啊~~~
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   890 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 23ms · UTC 22:00 · PVG 06:00 · LAX 14:00 · JFK 17:00
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.