dallinski
dallinski

Reputation: 431

IntelliJ target bytecode reverting

For a project that I'm currently working on, IntelliJ gave me the compile error Error:java: javacTask: source release 8 requires target release 1.8. I went into Settings>Build, Execution, Deployment>Compiler>Java and saw that the target bytecode version for one of my modules was set to 1.5, so I changed it to 1.8, compiled, and it worked. But then the next day, I got the same error. I went into settings and the target bytecode for that one module was back at 1.5. I changed it to 1.8 and it compiled/ran just fine. This has now happened multiple times and I am frustrated by the number of times I have to go into settings to manually change the target bytecode version.

Why does the target bytecode version keep reverting? I don't have 1.5 specified in the pom or anywhere else, so I am baffled as to why the bytecode version keeps getting set to 1.5.

Upvotes: 22

Views: 15397

Answers (6)

lojza
lojza

Reputation: 1901

Try under "Preference => Build, Execution, Deployment => Compiler => Java Compiler" add all missing modules with + button in "Per-module bytecode version". For some unknown reason I was missing two.

Upvotes: 0

Franz Wong
Franz Wong

Reputation: 1124

I have the same issue with "IntelliJ IDEA 2022.2.3 (Community Edition)".

This is my "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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>java-test-17</artifactId>
    <version>1.0-SNAPSHOT</version>

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

</project>

I can manually change the "Target bytecode version" to 17. (Preference => Build, Execution, Deployment) => Compiler => Java Compiler).

However, if I reload maven project (Right click "pom.xml" => Maven => Reload project), the "Target bytecode version" will be changed back to 1.5.

Reload maven project

Target bytecode version

To prevent that, I added "maven-compiler-plugin" to pom.xml. Then I reload maven project (Right click "pom.xml" => Maven => Reload project). Now, the "Target bytecode version" is 17.

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>java-test-17</artifactId>
    <version>1.0-SNAPSHOT</version>

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

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.10.1</version>
            </plugin>
        </plugins>
    </build>

</project>

Upvotes: 0

Please try below option, it worked for me.

step 01: Change the version under Module and project. step 02: build the module.

Hope it should resolve the issue.

enter image description here

enter image description here

Upvotes: 0

scot
scot

Reputation: 1280

FYI.

I still had this problem despite setting the pom.xml with both the settings above from the answers as mentioned by @isapir and @carlos-a-ibarra respectively.

I discovered this was due to an Intellij setting:

screen shot of intellij Maven settings

This config can be found in Build,Execution,Deployment > Build Tools > Maven > Importing

By default, it is set to the 'Internal JRE' setting, which in the latest version of Idea is 11. I had to select it to use the 1.8 JDK the project was configured to use.

This is annoying, because if you have a mix of 11 and 8 projects, you'll have to manually toggle this setting back and forth.

It caused the module output to always fall back to 11 every time the pom.xml was reimported.

intellij idea project module settings

It's a really annoying bug in IntelliJ.

Upvotes: 5

isapir
isapir

Reputation: 23572

You need to specify the source and target versions in your pom.xml file, but since the maven-compiler-plugin is added by default, an easier way to do that would be to set the following properties:

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

Upvotes: 14

Carlos A. Ibarra
Carlos A. Ibarra

Reputation: 6142

You need to dd this to your POM:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.3</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
    </plugins>
</build>

Upvotes: 11

Related Questions