javaspringspring-bootmavenspring-initializr

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


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>


Solution

  • 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 :)