1

I am new to coding and Java.

I created a vanilla Spring Boot app. It compiles and runs. No problem there.

As part of the courseware I was told to create a jar file using "mvnw package" command. I am getting the following error:

BUILD FAILURE

[INFO] ------------------------------------------------------------------------
[INFO] Total time:  1.315 s [INFO] Finished at: 2021-01-01T16:14:28-05:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile)
  on project taskone: Fatal error compiling : invalid target release: 11 -> [Help 1]

The pom file:

<?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.lazybreans</groupId>
    <artifactId>taskone</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>taskone</name>
    <description>Demo project for Spring Boot</description>

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

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</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-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

In the properties tab -> Java Build Path -> Libraries -> Modulepath -> JRE System Library [JavaSE-11]

Classpath -> Maven Dependencies

In eclipse -> window -> preferences -> Maven -> Installations -> Embedded 3.6.3/1.16.0......

Please help.

Gerold Broser
  • 14,080
  • 5
  • 48
  • 107
Nick Sharma
  • 35
  • 1
  • 9
  • I do see that the maven version on Eclipse is 3.6.3 and the error is showing plugin 3.8.1. Not sure if this is the reason? – Nick Sharma Jan 01 '21 at 21:26
  • Simply you are not building with JDK 11 The configuration in Eclipse is unrelated to that. You have to configure that on plain command line... First get your build working on plain command line. – khmarbaise Jan 01 '21 at 21:26
  • 1
    Can you share the output `java -version`? – Mureinik Jan 01 '21 at 21:27

2 Answers2

2

Eclipse supports adding a whole bunch of JDKs so that you can develop software that targets a variety of Java versions. These JDK "registrations" live inside the IDE itself and don't exist outside it.

It appears that the installation that your CLI environment points to ($JAVA_HOME, usually) is an older version. You can upgrade it, or optionally you can use a tool such as SDKMAN! to handle a variety of installations that you can select between.

chrylis -cautiouslyoptimistic-
  • 75,269
  • 21
  • 115
  • 152
  • @Mureinik openjdk version "11.0.8" 2020-07-14 LTS OpenJDK Runtime Environment Corretto-11.0.8.10.1 (build 11.0.8+10-LTS) OpenJDK 64-Bit Server VM Corretto-11.0.8.10.1 (build 11.0.8+10-LTS, mixed mode) – Nick Sharma Jan 01 '21 at 23:13
  • It works thank you. Changed the $JAVA_HOME to the latest version and it worked. – Nick Sharma Jan 01 '21 at 23:31
0

See compiler:compile's <release> parameter.

It's:

    <properties>
        <maven.compiler.release>11</maven.compiler.release>
    </properties>

not:

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

With the latter you're hiding java.version of System.getProperties().

Gerold Broser
  • 14,080
  • 5
  • 48
  • 107
  • I am getting lost in this. There are so many configuration areas. $JAVA_HOME, in Eclipse preferences, in the project properties and then the pom file. I am not sure how all of these work together. – Nick Sharma Jan 02 '21 at 01:00
  • @NickSharma I understand you. It was the same for me at the beginning. Did you start at the Maven Welcome page https://maven.apache.org/ and read through the linked pages? – Gerold Broser Jan 02 '21 at 01:07
  • Nope. Will do it. Thank you. – Nick Sharma Jan 02 '21 at 05:00
  • @NickSharma See also [this answer to _Maven: Lifecycle vs. Phase vs. Plugin vs. Goal_](https://stackoverflow.com/a/30953905/1744774) and the referenced books therein. – Gerold Broser Jan 02 '21 at 10:24