intellij idea - Error: java: invalid source release 1.9
Asked Answered
E

21

196

I'm trying to run my JSQL parser class, but I'm getting Error: java: invalid source release 1.9.

I tried to following this answer. I changed File> Build,Execution,Deployment> Java Compiler> Project bytecode version: 1.8. However, I can't change the Module language level and Project language level to 1.8 because there's not option for that. I still get the same error below.

Error enter image description here

Code

package cs4321.project2;

import java.io.FileReader;
import net.sf.jsqlparser.parser.CCJSqlParser;
import net.sf.jsqlparser.statement.Statement;
import net.sf.jsqlparser.statement.select.Select;

public class Parser {
    private static final String queriesFile = "resources/input/queries.sql";

    public static void main(String[] args) {
        try {
            CCJSqlParser parser = new CCJSqlParser(new FileReader(queriesFile));
            Statement statement;
            while ((statement = parser.Statement()) != null) {
                System.out.println("Read statement: " + statement);
                Select select = (Select) statement;
                System.out.println("Select body is " + select.getSelectBody());
            }
        } catch (Exception e) {
            System.err.println("Exception occurred during parsing");
            e.printStackTrace();
        }
    }
}
Edmon answered 18/9, 2017 at 13:43 Comment(6)
What version of intellij?Retrace
@Retrace intellij idea 2017.2.2Edmon
seems fine it should work . Are you working with maven project?Retrace
However, I can't change the Module language level and Project language level to 1.8 because there's not option for that. why is that so? and what error do you get while trying to do so? Also 1.9 seems to be the java version. Do you have Java-9 configured on your machine/intelliJ?Literal
See https://mcmap.net/q/54471/-idea-javac-source-release-1-7-requires-target-release-1-7Kanya
awesome that answer worked! thanks! https://mcmap.net/q/54471/-idea-javac-source-release-1-7-requires-target-release-1-7Edmon
C
439

Select the project, then File > ProjectStructure > ProjectSettings > Modules -> sources You probably have the Language Level set at 9:

screenshot

Just change it to 8 (or whatever you need) and you're set to go.

Also, check the same Language Level settings mentioned above, under Project Settings > Project

enter image description here

Carditis answered 23/9, 2017 at 19:24 Comment(3)
Oh, finally I know what the project language level means, thanksSatiety
Also, in image above, don't forget to go to the Modules sectionTrueblue
Better change in pom.xml <java.version>1.8</java.version> As, otherwise it may reset configuration upon reimport into intellijCloudscape
S
59

Sometimes the problem occurs because of the incorrect version of the project bytecode.

So verify it : File -> Settings -> Build, Execution, Deployment -> Compiler -> Java Compiler -> Project bytecode version and set its value to 8

Example

Sea answered 25/10, 2019 at 22:38 Comment(3)
it was the solution for me on the top of Project Settings->Modules -> Language LevelKurtzman
This is a great complementary answer to the accepted one.Gertrudgertruda
Worked for me. Oct 2021.Spool
E
35

Maven Projects - and Project Structure[imported as maven in IntelliJ]

Try - If any of below pom entries present - values should be same at java level

<java.version>16</java.version>
<maven.compiler.source>16</maven.compiler.source>
<maven.compiler.target>16</maven.compiler.target>
  1. Project Structure -> Project language level
  2. Module -> Language level


GRADLE projects

Since we started working with GRADLE,

Build.gradle: - even below line didn't help

sourceCompatibility = '16'

I am getting this ERROR most of the time and Solution is Change Gradle JVM to XX

Preferences | Build, Execution, Deployment | Build Tools | Gradle -> JVM enter image description here

Elbertelberta answered 20/8, 2021 at 9:18 Comment(1)
THANK YOU! After following/checking all the answers above this is what did it. I just manually changed the build.gradle file. Checked in the settings (like your screenshot) again and Gradle was using a different JDK than everything elseDudeen
M
24

I have had the same problem. There is an answer:

  • 1.CTRL + ALT + SHIFT + S;
    1. Then go to "Modules";
    2. "Dependencies;
    3. Change "Module SDK".

Got it! Now u have Java 9!

Mensural answered 27/11, 2017 at 20:4 Comment(0)
H
19

intellij-invalid-source

You should to set the JAVA SDK and appropriate language level in the project settings. Click to enlarge.

Horal answered 8/9, 2019 at 10:10 Comment(0)
M
4

When using maven project.

check pom.xml file

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>9</java.version>
</properties>

if you have jdk 8 installed in your machine, change java.version property from 9 to 8

Mireielle answered 12/3, 2019 at 6:1 Comment(0)
V
4

Gradle I had the same issue and changing all the settings given in the earlier solutions made no difference. Than I went to the build.gradle and found this line and deleted it.

sourceCompatibility = '11'

and it worked! :)

Venosity answered 9/1, 2020 at 19:13 Comment(1)
No prefix - just that? Didn't work for me :(Chan
E
4

I was having this issue while running a SpringBoot project (Maven)

In my POM file I changed the java version from 11 to 8 and it worked:

<properties>
    <java.version>8</java.version> //The default was 11
</properties>

Make sure to Load maven changes else the change won't reflect.

Ellenaellender answered 2/8, 2020 at 12:56 Comment(0)
L
3

I've just had a similar issue. The project had opened using Java 9 but even after all the modules and project were set back to 1.8, I was still getting the error.

I needed to force Gradle to refresh the project and then everything ran as expected.

Lozier answered 4/4, 2019 at 6:34 Comment(1)
Same for maven, after changing all intellij settings you have to run the maven sync with "Reimport All Maven Projects"; afterwards the test is greenDarton
F
1

Alternatively via Project Settings:

  • Project Settings
  • Project
  • Project language Level (set to fit your needs)

Depending on how your build is set up, this may be the way to go.

Freeness answered 11/5, 2018 at 8:49 Comment(0)
L
1

For anyone struggling with this issue who tried DeanM's solution but to no avail, there's something else worth checking, which is the version of the JDK you have configured for your project. What I'm trying to say is that if you have configured JDK 8u191 (for example) for your project, but have the language level set to anything higher than 8, you're gonna get this error.

In this case, it's probably better to ask whoever's in charge of the project, which version of the JDK would be preferable to compile the sources.

Lordinwaiting answered 21/12, 2018 at 11:46 Comment(1)
Indeed, I had a JDK version 11 and project language level 9. Setting them both the same cleared the error.Deponent
B
1

In Project Structure in Project SDK: modify SDK to 11 or higher and in Project language level: modify to 11 - Local variable syntax for lambda parameters

Bittersweet answered 28/1, 2020 at 14:49 Comment(0)
M
1

After I changed everything mentioned in answer, it was still giving me same error. I had to change "Use complier" option from "Eclipse" to "Javac" and it solved the issue for me.

enter image description here

Mountain answered 2/11, 2022 at 10:11 Comment(0)
C
0

I also had the same problem in IntellijIdea, after selecting the project, then File > ProjectStructure > ProjectSettings > Modules -> sources the option was showing - the Language Level set at 9:

So, I Just change it to 8. Still my issue didn't got resolve.

The main issue was with pom.xml. I reimported the pom.xml file and my issue got resolved.

So, whenever you changed the pom.xml file, IDEA needs to update the project structure. For example if you've added there some more dependencies, IDEA needs to add them as project libraries.

In "Settings > Build, Execution, Deployment > Build Tools > Maven > Importing" you can choose "Import Maven projects automatically". This will automatically invoke "Reimport" action when the pom.xml is changed.

enter image description here

Clockwork answered 8/1, 2019 at 8:49 Comment(0)
M
0

Just additional note:

when you creating a new project from start.spring.io make sure you select your JDK version theirs by default is set to JDK 11, later it will cause the above issue.

Muzhik answered 15/9, 2020 at 5:49 Comment(0)
R
0

File > Project Structure > Project > Project SDK > Choose Version File > Project Structure > Project > Project Language Level > Choose Version File > Project Structure > Modules > Sources Tab > Language Level > Choose Level File > Project Structure > Modules > Dependencies Tab > Module SDK > Choose Version

In Pom.xml under properties change to your version

<properties>
    <java.version>16</java.version>
</properties>

Right Click Pom then Mavem then Reload Project

Ridgway answered 28/5, 2021 at 8:35 Comment(0)
R
0

I was able to overcome this issue only clearing/removing the VM configuration. But you should do all the following checks:

  1. Menu File/Project Structure/Project (select the SDK and the correct language level -- in may case 1.8.0_201 and "8 - Lambdas...")

  2. Menu File/Project Structure/Modules/Sources (on the right) - Select the same Language Level

  3. Menu File/Project Structure/Dependencies (also on the right) - Select the same Module SDK

  4. (If still does not work...) Menu Run/Edit configurations

enter image description here

I removed the lines from the configuration and all start running.

--module-path
${PATH_TO_FX}
--add-modules
javafx.controls,javafx.fxml
Roofer answered 27/12, 2021 at 18:42 Comment(0)
K
0

Below process worked for me: File -> Project Structure -> Platform Settings -> SDKs -> (ADD JDK Required version either from disk or download it)

Kreda answered 4/4, 2023 at 9:31 Comment(0)
H
0

May caused by mismatching configuration of your Maven as well as project Structure.

  1. Re-set your project configuration.

IntelliJ IDEA - File - Project Structure - Project Settings

Project: Project language level : please select one (e.g. 8 - Lambdas, type annotations etc.)

Modules: Language Level: please select one same as above. (e.g. 8 - Lambdas, type annotations etc.)

Apply the setting.

  1. Re-upgrade your Maven XML configuration.

confirm your pom.xml file whether contains the properties configure xml code as below or not.

please change the version to '8'.

<properties>
    <maven.compiler.source>8</maven.compiler.source>
    <maven.compiler.target>8</maven.compiler.target>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
  1. Re-load Maven Project by IDEA.

right click pom.xml -> maven -> Reload project.

Heliacal answered 16/4, 2023 at 6:15 Comment(0)
L
0

While setting Language level do note that various language levels are split across LTS Versions and Other Versions headers. I missed that and spent a while figuring why level 8 wasn't showing up in my dropdown

IntelliJ Language level dropdown

Ref: What is Project Language level in IntelliJ IDEA?

Lannielanning answered 16/10, 2023 at 5:49 Comment(0)
M
0

Whatever version you wanted to use, add it's path in environment variable at top, it will resolve this issue.

Mesh answered 4/2 at 15:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.