ADAMAS-J
@ADAMAS-J (Kazuki Ohshiro)

Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

Spring Bootアプリケーションの実行ができない

解決したいこと

スクリーンショット 2021-10-14 21.23.33.png

独学でJavaの基礎の学習が終わり、「後悔しないためのSpring Boot 入門書:Spring 解体新書(第2版)Spring Bootが丸分かり」にてSpringBoot学習を始めたばかりの初心者です。

右も左も分からない状態ですが、初めてQiitaにて質問をさせていただきます。

現在、第3章の下記の写真のように「データベースから値を取得する」の学習をしています。

スクリーンショット 2021-10-23 10.44.59.png

使用IDE : Eclipse

Spring Boot version : 2.4.1(書籍と同じものを使用)

SpringBootを実行したところ、このようなエラーが発生しました。


  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
[32m :: Spring Boot :: [39m              [2m (v2.4.1)[0;39m

[2m2021-10-23 09:44:40.284[0;39m [32m INFO[0;39m [35m44022[0;39m [2m---[0;39m [2m[  restartedMain][0;39m [36mcom.example.SpringBootSampleApplication [0;39m [2m:[0;39m Starting SpringBootSampleApplication using Java 17-beta on Kazuki-MacBook-Pro-2.local with PID 44022 (/Applications/Eclipse_2021-09.app/Contents/workspace/SpringBootSample/target/classes started by kazukiohshiro in /Applications/Eclipse_2021-09.app/Contents/workspace/SpringBootSample)
[2m2021-10-23 09:44:40.286[0;39m [32m INFO[0;39m [35m44022[0;39m [2m---[0;39m [2m[  restartedMain][0;39m [36mcom.example.SpringBootSampleApplication [0;39m [2m:[0;39m No active profile set, falling back to default profiles: default
[2m2021-10-23 09:44:40.323[0;39m [32m INFO[0;39m [35m44022[0;39m [2m---[0;39m [2m[  restartedMain][0;39m [36m.e.DevToolsPropertyDefaultsPostProcessor[0;39m [2m:[0;39m Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
[2m2021-10-23 09:44:40.323[0;39m [32m INFO[0;39m [35m44022[0;39m [2m---[0;39m [2m[  restartedMain][0;39m [36m.e.DevToolsPropertyDefaultsPostProcessor[0;39m [2m:[0;39m For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG'
[2m2021-10-23 09:44:40.392[0;39m [33m WARN[0;39m [35m44022[0;39m [2m---[0;39m [2m[  restartedMain][0;39m [36mConfigServletWebServerApplicationContext[0;39m [2m:[0;39m Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanDefinitionStoreException: Failed to read candidate component class: file [/Applications/Eclipse_2021-09.app/Contents/workspace/SpringBootSample/target/classes/com/example/SpringBootSampleApplication.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 [/Applications/Eclipse_2021-09.app/Contents/workspace/SpringBootSample/target/classes/com/example/SpringBootSampleApplication.class]; nested exception is java.lang.IllegalArgumentException: Unsupported class file major version 61
[2m2021-10-23 09:44:40.448[0;39m [31mERROR[0;39m [35m44022[0;39m [2m---[0;39m [2m[  restartedMain][0;39m [36mo.s.boot.SpringApplication              [0;39m [2m:[0;39m Application run failed

org.springframework.beans.factory.BeanDefinitionStoreException: Failed to read candidate component class: file [/Applications/Eclipse_2021-09.app/Contents/workspace/SpringBootSample/target/classes/com/example/SpringBootSampleApplication.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 [/Applications/Eclipse_2021-09.app/Contents/workspace/SpringBootSample/target/classes/com/example/SpringBootSampleApplication.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.3.2.jar:5.3.2]
    at org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider.findCandidateComponents(ClassPathScanningCandidateComponentProvider.java:315) ~[spring-context-5.3.2.jar:5.3.2]
    at org.springframework.context.annotation.ClassPathBeanDefinitionScanner.doScan(ClassPathBeanDefinitionScanner.java:276) ~[spring-context-5.3.2.jar:5.3.2]
    at org.springframework.context.annotation.ComponentScanAnnotationParser.parse(ComponentScanAnnotationParser.java:132) ~[spring-context-5.3.2.jar:5.3.2]
    at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:296) ~[spring-context-5.3.2.jar:5.3.2]
    at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:250) ~[spring-context-5.3.2.jar:5.3.2]
    at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:207) ~[spring-context-5.3.2.jar:5.3.2]
    at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:175) ~[spring-context-5.3.2.jar:5.3.2]
    at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:336) ~[spring-context-5.3.2.jar:5.3.2]
    at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:252) ~[spring-context-5.3.2.jar:5.3.2]
    at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:285) ~[spring-context-5.3.2.jar:5.3.2]
    at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:99) ~[spring-context-5.3.2.jar:5.3.2]
    at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:751) ~[spring-context-5.3.2.jar:5.3.2]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:569) ~[spring-context-5.3.2.jar:5.3.2]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:144) ~[spring-boot-2.4.1.jar:2.4.1]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:767) ~[spring-boot-2.4.1.jar:2.4.1]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:759) ~[spring-boot-2.4.1.jar:2.4.1]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:426) ~[spring-boot-2.4.1.jar:2.4.1]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:326) ~[spring-boot-2.4.1.jar:2.4.1]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1309) ~[spring-boot-2.4.1.jar:2.4.1]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1298) ~[spring-boot-2.4.1.jar:2.4.1]
    at com.example.SpringBootSampleApplication.main(SpringBootSampleApplication.java:10) ~[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.4.1.jar:2.4.1]
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 [/Applications/Eclipse_2021-09.app/Contents/workspace/SpringBootSample/target/classes/com/example/SpringBootSampleApplication.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.3.2.jar:5.3.2]
    at org.springframework.core.type.classreading.SimpleMetadataReader.<init>(SimpleMetadataReader.java:49) ~[spring-core-5.3.2.jar:5.3.2]
    at org.springframework.core.type.classreading.SimpleMetadataReaderFactory.getMetadataReader(SimpleMetadataReaderFactory.java:103) ~[spring-core-5.3.2.jar:5.3.2]
    at org.springframework.core.type.classreading.CachingMetadataReaderFactory.getMetadataReader(CachingMetadataReaderFactory.java:123) ~[spring-core-5.3.2.jar:5.3.2]
    at org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider.scanCandidateComponents(ClassPathScanningCandidateComponentProvider.java:429) ~[spring-context-5.3.2.jar:5.3.2]
    ... 26 common frames omitted
Caused by: java.lang.IllegalArgumentException: Unsupported class file major version 61
    at org.springframework.asm.ClassReader.<init>(ClassReader.java:189) ~[spring-core-5.3.2.jar:5.3.2]
    at org.springframework.asm.ClassReader.<init>(ClassReader.java:170) ~[spring-core-5.3.2.jar:5.3.2]
    at org.springframework.asm.ClassReader.<init>(ClassReader.java:156) ~[spring-core-5.3.2.jar:5.3.2]
    at org.springframework.asm.ClassReader.<init>(ClassReader.java:277) ~[spring-core-5.3.2.jar:5.3.2]
    at org.springframework.core.type.classreading.SimpleMetadataReader.getClassReader(SimpleMetadataReader.java:57) ~[spring-core-5.3.2.jar:5.3.2]
    ... 30 common frames omitted


ログにアプリケーションの実行に失敗しているとありますが、何をどのように対処すればいいのか分かりません。

しかし、こちらの警告が出ているところに「おそらく、まだサポートされていない新しいJavaクラスファイルのバージョンが原因です」とありますが、その対処方法が分かりません。

[2m2021-10-23 09:44:40.392[0;39m [33m WARN[0;39m [35m44022[0;39m [2m---[0;39m [2m[  restartedMain][0;39m [36mConfigServletWebServerApplicationContext[0;39m [2m:[0;39m Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanDefinitionStoreException: Failed to read candidate component class: file [/Applications/Eclipse_2021-09.app/Contents/workspace/SpringBootSample/target/classes/com/example/SpringBootSampleApplication.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 [/Applications/Eclipse_2021-09.app/Contents/workspace/SpringBootSample/target/classes/com/example/SpringBootSampleApplication.class]; nested exception is java.lang.IllegalArgumentException: Unsupported class file major version 61

ディレクトリ一覧

スクリーンショット 2021-10-23 10.57.42.png

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.1</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>SpringBootSample</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>SpringBootSample</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

自分で試したこと

springbootの実行において、ログに書かれているエラーの意味を調べたりしましたが、よく分かりませんでした。
質問のしかたが正しいのかどうかすら不安ではありますが、どなたか教えていただけませんでしょうか。
よろしくお願いいたします。

1

1Answer

原因

Spring Boot 2.4.1がJava17に対応していないから
(正確には、Spring boot2.4.1に含まれるクラスファイルがJava17をサポートしていないため

スタックトレースの見方

Java17に対応していないエラーメッセージ

下記のエラーメッセージは「JDK17に対応していないクラスファイルです。」の記載です。
Eclipseの設定を見ても、JDK17が指定されているのがわかるかと思います。

これです→Javaシステム・ライブラリー[JavaSE-17]

[2m2021-10-23 09:44:40.392[0;39m [33m WARN[0;39m [35m44022[0;39m [2m---[0;39m [2m[  restartedMain][0;39m [36mConfigServletWebServerApplicationContext[0;39m [2m:[0;39m Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanDefinitionStoreException: Failed to read candidate component class: file [/Applications/Eclipse_2021-09.app/Contents/workspace/SpringBootSample/target/classes/com/example/SpringBootSampleApplication.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 [/Applications/Eclipse_2021-09.app/Contents/workspace/SpringBootSample/target/classes/com/example/SpringBootSampleApplication.class]; nested exception is java.lang.IllegalArgumentException: Unsupported class file major version 61

Spring bootのバージョン確認

スタックトレースのフルを見ると、Spring-bootのバージョンが2.4.1であることが分かります。

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
[32m :: Spring Boot :: [39m              [2m (v2.4.1)[0;39m

対応方法

JDKのバージョンを下げる

低いバージョンのJDKをダウンロードし、インストールしてください。

推奨は現在のJavaのLTS版主流のJDK11です。
OpenJDK11で検索し、JDKをダウンロードし、インストール後、

設定画面>Java>インストール済みJRE

にJDK11を追加してください。

その後、pom.xmlを以下の通り書き換えてください。

<java.version>11</java.version>

Spring bootのバージョンをJava17対応バージョンに上げる

Spring-BootがJava17に対応したのは2.5.5からです。

リリースノート

Spring Boot 2.5.5 available now

This release includes 41 bug fixes, documentation improvements, and dependency upgrades. Thanks to all those who have contributed with issue reports and pull requests.

Documentation
Document support for Java 17 #26767

pom.xmlを、以下を参考にしながら、2.5.5以上のバージョンを固定指定してください。

Maven Repository: org.springframework.boot » spring-boot » 2.5.5

3Like

Comments

  1. @ADAMAS-J

    Questioner

    解決できました。とても勉強になりました。ご親切にありがとうございました!

Your answer might help someone💌