ChatGPT解决这个技术问题 Extra ChatGPT

Error:java: javacTask: source release 8 requires target release 1.8

Using IntelliJ IDE can't compile any projects. Screenshots of settings below:

Used JDK:

https://i.stack.imgur.com/cpggk.png

Project SDK and Language level:

https://i.stack.imgur.com/0gEQl.png

Language Level:

https://i.stack.imgur.com/8z6A4.png

Anybody have any ideas?

That doesn't look like an error from "pure Intellij"; aren't you using an ant buildscript or something?

Y
Yellowjacket

Go to File > Settings > Build, Execution, Deployment > Compiler > Java Compiler If on a Mac, it's under Intellij IDEA > Preferences... > Build, Execution, Deployment > Java Compiler Change Target bytecode version to 1.8 of the module that you are working for.

If you are using Maven

Add the compiler plugin to pom.xml under the top-level project node:

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

(Hoisted from the comments.)

Note: If you don't mind reimporting your project, then the only thing you really need to do is change the pom and reimport the project, then IntelliJ will pick up the correct settings and you don't have to manually change them.


Works in IntelliJ 14 Ultimate.
On older IDEA versions, try: Settings -> Compiler -> Java Compiler -> Per-module bytecode version
If you're on a Mac, it's under Intellij IDEA > Preferences... > Build, Execution, Deployment > Java Compiler
For Intellij newer than 2016.2, there's one more place to check: Project Structure -> Project Settings -> Modules -> Denpendencies -> Module SDK.
When you have a POM hierarchy with more than two levels and your compiler plugin is defined in the parent POM, IntelliJ might not recognize it at the deepest level. I say might not, because for some other modules it does, even when they extend the same parent POM.
G
Gregory Mazur

https://i.stack.imgur.com/OW3jL.png

also check the Project Settings


S
Steve Chaloner

This looks like the kind of error that Maven generates when you don't have the compiler plugin configured correctly. Here's an example of a Java 8 compiler config.

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

<!-- ... -->

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

<!-- ... -->

</project>

For other Maven newbies like myself, this should be under project, build, and then plugins.
Also if <source>1.8</source> and <target>1.8</target> is configured, the above error can occur.
This worked for me, thank you! The version tag did not resolve (I changed it to match my version of Maven) so I commented it out. I believe Maven should automatically fetch the correct latest version in that case, based on this thread: stackoverflow.com/questions/17239394/….
A
Anonsage

The quickest way I found:

press:CTRL + SHIFT + A (For Mac ⌘ + SHIFT + A)

type: java compiler

press: ENTER

In the Settings window, set the Target bytecode to 1.8

(or 9 for java9)


This absolutely worked for me (thanks!) but I don't understand why this is different than what's in the pom.xml, module settings, etc...
press ⌘, to get preference.
L
Lord Nighton

There are two ways to solve this problem:

Set settings (File -> Settings -> Build, Execution, Deployment -> Java Compiler): Add a build section to your pom.xml:


M
Marvin Richter

Many answers regarding Maven are right but you don't have to configure the plugin directly.

Like described on the wiki page of the Apache Maven Compiler Plugin you can just set the 2 properties used by the plugin.

<project>
  [...]
  <properties>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>
  [...]
</project>

@ Marvin Richter I tried your solution but it does not work with IntelliJ - My guess is that it's correct in pure maven build (command-line), but IntelliJ just ignore these properties. I had to configure the plugin as mentioned in other solutions.
No I did not, because the other solutions worked. And now I migrated my project to gradle.
Works like a charm after you do Right click on pom.xml -> Maven -> Reimport. This should be the accepted answer for Maven users.
You actually need the plugin org.apache.maven.plugins:maven-compiler-plugin in your pom or gradle file, what in the documentation intented to say is that having that property set in the pom properties will be enough, basically because they set the defaults values of the plugin, e.g. <source>${maven.compiler.source}</source>
This should be the accepted answer. Worked for me on IntelliJ 2018.2 on mac os 10.13.5
S
Sam Barnum

I fixed this by going to Project Structure -> Modules, find the module in question, click on Dependencies tab, change Module SDK to Project SDK.


V
Vishal

I fixed it just by changing target compile version to 1.8. Its in:

File >> Settings >> Build, Execution, Deployment >> Compiler >> Java Compiler


Thanks, Vishal. We have to specify project bytecode version, or at least per-module bytecode version with Intellij Idea. Just setting it in pom.xml for maven-compiler-plugin didn't solve the problem for me.
M
Muhammad Usman

You need to go to the /.idea/compiler.xml and change target to required jdk level.

https://i.stack.imgur.com/WNe7s.png


C
Cloud

In my case I fixed this issue by opening .iml file of project (it is located in project root folder and have name same as the name of project) and changing line <orderEntry type="jdk" jdkName="1.7" jdkType="JavaSDK" /> to <orderEntry type="jdk" jdkName="1.8" jdkType="JavaSDK" />

I had everything configured as in others answers here but by some reason Idea updated .iml file incorrectly.


It seems sometimes Intellij does not update the config files even when you configured all menus. Invalidate caches and restart (after editing the config files), did the trick for me. Strange.
Thanks so much. Finally after trying and failing every other solution specified above, this worked.
u
user4884640

I fixed it by modify my POM file. Notice the last comment under the highest voted answer.

<plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>2.3.2</version>
        <configuration>
            <source>1.8</source>
            <target>1.8</target>
            <encoding>UTF-8</encoding>
        </configuration>
</plugin>

The source must matches the target.


A
Andreas

I just re-import maven button, then the error disappeared.

https://i.stack.imgur.com/9Or08.png


Thanks. That hint made it
B
Badr

In your Gradle app level file >> compileOptions add this two lines

android {
  compileOptions {
    ...
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
    ...
   }
}

B
B.shruti

If you are working with Android-studio 1.3, Follow the below steps -

Go to File - Project Structure

Under modules- app-Properties tab, choose Source Compatibility -1.8 and

Target Compatibility - 1.8.

And you are good to go.


B
Bachiri Taoufiq Abderrahman

Under compiler.xml file you will find :

<bytecodeTargetLevel>
  <module name="your_project_name_main" target="1.8" />
  <module name="your_project_name_test" target="1.8" />
</bytecodeTargetLevel>

and you can change the target value from your old to the new for me i needed to change it from 1.5 to 1.8


N
Nicolas Zozol

With Intellij, using Maven, you must check that Intellij has auto-imported your project. You can check by clicking on the Maven tab on the right of your Editor.

https://i.stack.imgur.com/yA1Zf.png

If your Project is not here, then add the pom.xml file by clicking on +.

Obviously, the project must also have the relevant <build/> :

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

t
thunder

I've just spent a while struggling with the same problem. The only thing that worked for me was not using the built mvn (3.3.9) but pointing it to an external downloaded version (3.5.0). Finally the project opened and everything was good.


H
Hieu Vo

https://i.stack.imgur.com/UHjwL.png


G
Gani

This issue occurs if your module is configured with Annotation processor and other module is not.Set the same configuration for all the modules as it wold be cyclic dependency.


A
Abd Abughazaleh

the below code working fine by my side. I just add it in the pom.xml file.

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

e
eis

For me, the problem was about Maven not able to find proper configurations, since these items were specified in parent pom.

Changing File -> Settings -> Build, Excecution, Deployment -> Maven -> User Settings file to point to my custom settings with proper repositories fixed the problem that was otherwise hiding.

Found out about the problem through Help -> Show log in explorer -> clicking the log file, when previously only got the error in the title and "java.lang.NullPointerException" in the console.


A
Axmedbek

Solution of the problem is very simple.You have to open .idea/compiler.xml file on your İdea Project and

You should write appropriate target version


d
dzimney

If none of the other answers work, check your Module SDK.

I had this error pop up for me after I had updated the project SDK to 1.8, the Javac compiler to 1.8, etc. The setting that was the problem for me was the "Module SDK".

(on Linux) Go to File > Project Structure... then in the window that opens, select Modules under Project Settings. Select the module in question from the list and then the Dependencies tab and make sure that Module SDK is set appropriately.


s
snap

I have checked all of the above but the error still occurs.

But reimport all maven Projects (reload button inside Maven Projects panel) works in my case.