UnsupportedClassVersionError の解決方法

Java11 でコンパイルした Spring Boot アプリ を Java8 で実行しようとしたら次のようなエラーが出ました。

$ java -jar test.jar 
Exception in thread "main" java.lang.UnsupportedClassVersionError: *********/TestApplication has been compiled by a more recent version of the Java Runtime (class file version 55.0), this version of the Java Runtime only recognizes class file versions up to 52.0
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClass(ClassLoader.java:756)
        at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
        at java.net.URLClassLoader.defineClass(URLClassLoader.java:473)
        at java.net.URLClassLoader.access$100(URLClassLoader.java:74)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:369)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:363)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:362)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
        at org.springframework.boot.loader.LaunchedURLClassLoader.loadClass(LaunchedURLClassLoader.java:151)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Class.java:348)
        at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:46)
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:108)
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:58)
        at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:65)

使用する Java を次のとおり切り替えて再度実行してみます。切り替え方法は次のとおり。

複数のJavaを切り替えて使用する方法 on Linux(CentOS)
複数のJavaを切り替えて使用するには alternativesコマンド を使用します。 使い方は次のとおり。今回は Java8 と ...

Java11 に切り換えて再度実行したら無事に起動しました。

$ java -jar test.jar 

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.7.7)

2023-01-07 06:59:45.381  INFO 1667 --- [           main] *********.TestApplication           : Starting TestApplication v0.0.1-SNAPSHOT using Java 11.0.17 on ********* with PID 1667
2023-01-07 06:59:45.390  INFO 1667 --- [           main] *********.TestApplication           : No active profile set, falling back to 1 default profile: "default"
2023-01-07 06:59:47.715  INFO 1667 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2023-01-07 06:59:47.738  INFO 1667 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2023-01-07 06:59:47.739  INFO 1667 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.70]
2023-01-07 06:59:48.022  INFO 1667 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2023-01-07 06:59:48.022  INFO 1667 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 2345 ms
2023-01-07 06:59:48.735  INFO 1667 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2023-01-07 06:59:48.757  INFO 1667 --- [           main] *********.TestApplication           : Started TestApplication in 4.346 seconds (JVM running for 5.125)
Hello Spring Boot!