【问题标题】:spring-boot: Fatal error compiling: invalid target release: 17spring-boot:致命错误编译:无效目标版本:17
【发布时间】:2022-09-26 17:22:11
【问题描述】:

刚刚通过spring-boot tutorial 并卡在执行$ mvn spring-boot:run 的步骤4.4.4

这是错误:

$ mvn spring-boot:run
[INFO] Scanning for projects...
[INFO] 
[INFO] -----------------------< com.example:myproject >------------------------
[INFO] Building myproject 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] >>> spring-boot-maven-plugin:3.0.0-SNAPSHOT:run (default-cli) > test-compile @ myproject >>>
[INFO] 
[INFO] --- maven-resources-plugin:3.2.0:resources (default-resources) @ myproject ---
[INFO] Using \'UTF-8\' encoding to copy filtered resources.
[INFO] Using \'UTF-8\' encoding to copy filtered properties files.
[INFO] skip non existing resourceDirectory /Users/dnatochy/development/springboot/testapp1/src/main/resources
[INFO] skip non existing resourceDirectory /Users/dnatochy/development/springboot/testapp1/src/main/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.10.1:compile (default-compile) @ myproject ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /Users/dnatochy/development/springboot/testapp1/target/classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  1.078 s
[INFO] Finished at: 2022-07-26T21:27:48-07:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.10.1:compile (default-compile) on project myproject: Fatal error compiling: invalid target release: 17 -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

我在谷歌上找到的与此错误类似的所有内容都指向 maven 使用的 java 版本不匹配,但事实并非如此

$ java -version
java version \"1.8.0_333\"
Java(TM) SE Runtime Environment (build 1.8.0_333-b02)
Java HotSpot(TM) 64-Bit Server VM (build 25.333-b02, mixed mode)

$ javac -version
javac 1.8.0_333

$ mvn -v
Apache Maven 3.8.6 (84538c9988a25aec085021c365c560670ad80f63)
Maven home: /Users/myuser/tools/apache-maven-3.8.6
Java version: 1.8.0_333, vendor: Oracle Corporation, runtime: /Library/Java/JavaVirtualMachines/jdk1.8.0_333.jdk/Contents/Home/jre
Default locale: en_US, platform encoding: UTF-8
OS name: \"mac os x\", version: \"12.4\", arch: \"x86_64\", family: \"mac\"

$ echo $JAVA_HOME
/Library/Java/JavaVirtualMachines/jdk1.8.0_333.jdk/Contents/Home
  • \"错误指向maven使用的java版本不匹配,但事实并非如此\"- 但它是。您正在使用 java 8,并尝试编译 java 17 应用程序。
  • 谢谢@1615903!安装 java 17 并添加 import 语句后,问题得到解决。

标签: spring-boot


【解决方案1】:

正如@1615903 指出我需要java 17。安装后,我开始收到编译错误

[ERROR] COMPILATION ERROR : 
[INFO] -------------------------------------------------------------
[ERROR] /Users/myuser/development/springboot/testapp1/src/main/java/MyApplication.java:[1,2] cannot find symbol
  symbol: class RestController
[ERROR] /Users/myuser/development/springboot/testapp1/src/main/java/MyApplication.java:[2,2] cannot find symbol
  symbol: class EnableAutoConfiguration
[ERROR] /Users/myuser/development/springboot/testapp1/src/main/java/MyApplication.java:[5,10] cannot find symbol
  symbol:   class RequestMapping
  location: class MyApplication
[ERROR] /Users/myuser/development/springboot/testapp1/src/main/java/MyApplication.java:[11,17] cannot find symbol
  symbol:   variable SpringApplication

将导入语句添加到 MyApplication.java 后,应用程序编译并运行

import org.springframework.web.bind.annotation.RestController;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.web.bind.annotation.RequestMapping;

【讨论】:

    【解决方案2】:

    您应该检查在 JAVA_HOME 中配置了哪个 Java 版本。 使用:mvn -version 您应该从mvn 输出中看到类似的内容

    $ mvn -version
    Apache Maven 3.8.5 (3599d3414f046de2324203b78ddcf9b5e4388aa0)
    Maven home: path\apache-maven-3.8.5
    Java version: 17.0.3.1, vendor: Oracle Corporation, runtime: path\Java\jdk-17.0.3.1
    Default locale: en_US, platform encoding: Cp1252
    OS name: "windows 10", version: "10.0", arch: "amd64", family: "windows"
    
    

    您也可以关注这篇文章以获得更多解决方案。 https://mkyong.com/maven/maven-error-invalid-target-release-17/

    【讨论】:

      猜你喜欢
      • 2015-06-14
      • 2018-08-03
      • 2015-06-01
      • 2015-10-14
      • 2017-10-29
      • 2015-04-02
      • 2021-04-10
      • 2020-01-12
      • 2019-03-02
      相关资源
      最近更新 更多