Why is Spring changing Java version configs and how to set it properly?

356 Views Asked by At

I'm using Spring Initializr (https://start.spring.io/) to generate a Spring Boot project. I've set Java version as 1.8 and hence, Spring Boot version as 2.7.16. Also added Lombok and PostgreSQL dependencies.

When I run my project, however, it displays the following logs:


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

2023-10-11 15:07:43.625  INFO 12939 --- [           main] c.j.integrador.IntegradorApplication     : Starting IntegradorApplication using Java 17.0.2 on pc with PID 12939 (/home/<path>/integrador/target/classes started by user in /home/<path>/integrador)
2023-10-11 15:07:43.631  INFO 12939 --- [           main] c.j.integrador.IntegradorApplication     : No active profile set, falling back to 1 default profile: "default"
2023-10-11 15:07:44.783  INFO 12939 --- [           main] c.j.integrador.IntegradorApplication     : Started IntegradorApplication in 1.908 seconds (JVM running for 3.661)

Why is it getting set as Java 17.0.2?

I've already tried changing adding the following lines to POM.xml, although it didn't solve my problem:

<properties>
    <maven.compiler.target>1.8</maven.compiler.target>
    <maven.compiler.source>1.8</maven.compiler.source>
</properties>

For instance, here's my complete 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.7.16</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.jotec</groupId>
    <artifactId>integrador</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>integrador</name>
    <description>Integrador de Marketplaces</description>
    <properties>
        <java.version>1.8</java.version>
        <maven.compiler.target>1.8</maven.compiler.target>
        <maven.compiler.source>1.8</maven.compiler.source>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</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>

3

There are 3 best solutions below

0
robertinho On BEST ANSWER

To solve it, I had to set the correct JRE to run with my Java application on Eclipse.

Initially, I already had the correct version for Java 1.8 installed on my machine. As I ran the following command, I managed to find the current JRE home path: $ java -XshowSettings:properties -version

...
java.home = /usr/lib/jvm/java-8-openjdk-amd64/jre
...
OpenJDK Runtime Environment (build 1.8.0_362-8u372-ga~us1-0ubuntu1~18.04-b09)
...

Then I proceeded to access Window>Preferences>Java>Installed JREs menu and added a Stantard VM setting the specified path in the JRE home directory (also deleting the previous one I wouldn't use).

Thank you :)

0
mio On
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>

These properties only tell the compiler what version the source and the target (generated code) should be compliant with. I think (but I am not sure) they are only passed to the compiler for the -source and -target flags.

These properties have nothing to do with the java runtime you use to execute the application. You started the spring boot application with Java 17.0.2. But that is not related to the compiler options.

0
NoDataFound On

When you configure maven.compiler.source to java 8, you ask for it to be compiled under Java 8.

In Eclipse:

  • Importing a maven.compiler.source=8 project will tell m2e to use Java-SE-1.8 as Execution Environments
  • In Execution Environments, Eclipse will list all JRE that match the "base level" (here: JavaSE-1.8)
  • You need to select a JRE with a perfect match

Execution Environments

If you don't have such "perfect match" JRE, you need to add one from Installed JREs in the preferences then select it in Execution Environments