26

Just going through spring-boot tutorial and getting stuck on step 4.4.4 executing $ mvn spring-boot:run

Here's the error:

$ 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

Everything I can find on Google similar to this error points to the mismatch of java version used by maven, but this is not the case

$ 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
Dennis
  • 729
  • 1
  • 5
  • 8
  • 1
    _"error points to the mismatch of java version used by maven, but this is not the case"_ - but it is. You are using java 8, and trying to compile java 17 application. – 1615903 Jul 27 '22 at 04:59
  • Thank you @1615903! After installing java 17 and adding import statement the issues was resolved. – Dennis Jul 27 '22 at 06:15

9 Answers9

23

You should check which Java version is configured in the JAVA_HOME. Use: mvn -version You should see something like this from mvn output

$ 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"

You can also follow this post for more solutions. https://mkyong.com/maven/maven-error-invalid-target-release-17/

Alter
  • 903
  • 1
  • 11
  • 27
  • Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f) Maven home: C:\Program Files\Java\apache-maven-3.6.3\bin\.. Java version: 17.0.5, vendor: Oracle Corporation, runtime: C:\Program Files\Java\jdk-17.0.5 Default locale: en_US, platform encoding: Cp1252 OS name: "windows 11", version: "10.0", arch: "amd64", family: "windows" Everything seems in order here: error still persists :/ – LocalHackersUnion5156 May 07 '23 at 15:29
8

make sure Maven is using a JAVA version that is same or newer than your target.

  • update your JAVA_HOME to the newer java you installed.
  • close any old terminal (even if using VSCode)
  • and open & run the mvn -version command. make sure the "runtime:" is pointing to your new JAVA path
pref
  • 1,651
  • 14
  • 24
7

Go back to your pom.xml change the java version to what you find in your : mvn -v.

Procrastinator
  • 2,526
  • 30
  • 27
  • 36
3

In my case, this issue was due to the lower Java version which was present in my machine. I was on Java 17 and update to Java 18 and setting the same in the worked for me.

abhay anand
  • 323
  • 3
  • 7
1

As @1615903 pointed out I needed java 17. After installing it, I started getting compilation errors

[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

After adding import statements to MyApplication.java, the app compiled and ran

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;
Dennis
  • 729
  • 1
  • 5
  • 8
1

I got the same error when I used 'Spring Boot Extension Pack' version 0.2.1 from Visual Studio Code. So, I used an older version 0.2.0 of it and this fixed my issue. If any of those following solutions not working, you can try this.

0

u can either set 17 in < target> and < source> in < configuration> of maven-compiler-plugin

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
        <source>17</source>
        <target>17</target>
    </configuration>
</plugin>

OR

set 17 in maven-compiler-source (and target) in < properties>

<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>

then u need to set jdk 17 in JAVA_HOME ( u can check it with mvn -v command )

note that: with ( for example ) jdk-11 u can compile projects with lower java version but in order to compile projects with java 11 u have to set jdk-11 or above.

0

Here is what I did:

  1. Found my java 17 home with command:/usr/libexec/java_home

result:

/Library/Java/JavaVirtualMachines/temurin-17.jdk/Contents/Home

  1. Checked mvn file:vi ~/.mavenrc and replace JAVA_HOME with 17 above
smile
  • 498
  • 7
  • 18
0

Note : this coz while project creation , most of we create project by default without checking our maven version and java version.

Step 1: Check java version running in your system cmd -> java --version

Step 2: Change java version in POX.xml file accordingly for my case it was java 8

    <java.version>1.8</java.version>