简体   繁体   English

intellij 想法 - 错误:java:源代码版本 1.9 无效

[英]intellij idea - Error: java: invalid source release 1.9

I'm trying to run my JSQL parser class, but I'm getting Error: java: invalid source release 1.9 .我正在尝试运行我的 JSQL 解析器类,但出现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.我更改了文件>构建、执行、部署>Java编译器>项目字节码版本: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.但是,我无法将模块语言级别和项目语言级别更改为 1.8,因为没有选项。 I still get the same error below.我仍然在下面遇到相同的错误。

Error错误在此处输入图片说明

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();
        }
    }
}

Select the project, then File > ProjectStructure > ProjectSettings > Modules -> sources You probably have the Language Level set at 9:选择项目,然后 File > ProjectStructure > ProjectSettings > Modules -> sources 您可能将 Language Level 设置为 9:

截屏

Just change it to 8 (or whatever you need) and you're set to go.只需将其更改为 8(或您需要的任何值),您就可以开始了。

Also, check the same Language Level settings mentioned above, under Project Settings > Project此外,在项目设置 > 项目下检查上述相同的语言级别设置

在此处输入图片说明

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所以验证一下:文件 -> 设置 -> 构建、执行、部署 -> 编译器 -> Java 编译器 -> 项目字节码版本并将其值设置为 8

例子

I have had the same problem.我曾经也有过一样的问题。 There is an answer:有一个答案:

  • 1. CTRL + ALT + SHIFT + S ; 1. CTRL + ALT + SHIFT + S ;
    1. Then go to " Modules" ;然后转到“模块”
    2. " Dependencies ; "依赖项
    3. Change " Module SDK ".更改“模块 SDK ”。

Got it!知道了! Now u have Java 9!现在你有了 Java 9!

智能无效源

You should to set the JAVA SDK and appropriate language level in the project settings.您应该在项目设置中设置 JAVA SDK 和适当的语言级别。 Click to enlarge.点击放大。

GRADLE projects GRADLE 项目

Since we started working with GRADLE,自从我们开始使用 GRADLE 以来,

Build.gradle: - even below line didn't help Build.gradle: - 即使低于线也没有帮助

sourceCompatibility = '16'

I am getting this ERROR most of the time and Solution is Change Gradle JVM to XX我大部分时间都收到此错误,解决方案是将 Gradle JVM 更改为 XX

Preferences |偏好 | Build, Execution, Deployment |构建、执行、部署 | Build Tools |构建工具 | Gradle -> JVM摇篮-> JVM 在此处输入图片说明

I was having this issue while running a SpringBoot project (Maven)我在运行 SpringBoot 项目 (Maven) 时遇到了这个问题

In my POM file I changed the java version from 11 to 8 and it worked:在我的 POM 文件中,我将 java 版本从 11 更改为 8 并且它起作用了:

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

Make sure to Load maven changes else the change won't reflect.确保Load maven changes否则更改将不会反映。

When using maven project.使用 Maven 项目时。

check pom.xml file检查 pom.xml 文件

<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如果您的机器上安装了 jdk 8,请将java.version属性从9更改为8

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.该项目已使用 Java 9 打开,但即使在所有模块和项目都设置回 1.8 之后,我仍然收到错误消息。

I needed to force Gradle to refresh the project and then everything ran as expected.我需要强制 Gradle 刷新项目,然后一切都按预期运行。

Gradle I had the same issue and changing all the settings given in the earlier solutions made no difference. Gradle我遇到了同样的问题,更改早期解决方案中给出的所有设置没有任何区别。 Than I went to the build.gradle and found this line and deleted it.然后我去了 build.gradle 并找到了这一行并将其删除。

sourceCompatibility = '11'

and it worked!它奏效了! :) :)

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.根据您的构建是如何设置的,这可能是要走的路。

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.对于尝试过 DeanM 的解决方案但无济于事的任何人来说,还有其他值得检查的问题,即您为项目配置的 JDK 版本。 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.我想说的是,如果您已经为您的项目配置了 JDK 8u191(例如),但将语言级别设置为高于 8 的任何级别,您将收到此错误。

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.在这种情况下,最好询问项目负责人,哪个版本的 JDK 更适合编译源代码。

项目SDK 的项目结构将 SDK 修改为 11 或更高版本,在项目语言级别:修改为 11 - lambda 参数的局部变量语法

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:我在 IntellijIdea 中也遇到了同样的问题,在选择项目后,文件 > 项目结构 > 项目设置 > 模块 - > 源选项显示 - 语言级别设置为 9:

So, I Just change it to 8. Still my issue didn't got resolve.所以,我只是将其更改为 8。我的问题仍然没有得到解决。

The main issue was with pom.xml.主要问题是 pom.xml。 I reimported the pom.xml file and my issue got resolved.我重新导入了 pom.xml 文件,我的问题得到了解决。

So, whenever you changed the pom.xml file, IDEA needs to update the project structure.因此,无论何时更改 pom.xml 文件,IDEA 都需要更新项目结构。 For example if you've added there some more dependencies, IDEA needs to add them as project libraries.例如,如果您添加了更多依赖项,IDEA 需要将它们添加为项目库。

In "Settings > Build, Execution, Deployment > Build Tools > Maven > Importing" you can choose "Import Maven projects automatically".在“设置 > 构建、执行、部署 > 构建工具 > Maven > 导入”中,您可以选择“自动导入 Maven 项目”。 This will automatically invoke "Reimport" action when the pom.xml is changed.当 pom.xml 更改时,这将自动调用“重新导入”操作。

enter image description here在此处输入图片说明

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.当您从 start.spring.io 创建新项目时,请确保选择您的 JDK 版本,默认情况下,他们的 JDK 版本设置为 JDK 11,稍后会导致上述问题。

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文件 > 项目结构 > 项目 > 项目 SDK > 选择版本文件 > 项目结构 > 项目 > 项目语言级别 > 选择版本文件 > 项目结构 > 模块 > 源选项卡 > 语言级别 > 选择级别文件 > 项目结构 > 模块 > 依赖项选项卡 >模块 SDK > 选择版本

In Pom.xml under properties change to your version在 Pom.xml 下的属性更改为您的版本

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

Right Click Pom then Mavem then Reload Project右键单击 Pom 然后 Maven 然后重新加载项目

I was able to overcome this issue only clearing/removing the VM configuration.我只能通过清除/删除 VM 配置来解决这个问题。 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...")菜单文件/项目结构/项目(选择 SDK 和正确的语言级别——可能是 1.8.0_201 和“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菜单文件/项目结构/依赖项(也在右侧) - 选择相同的模块 SDK

  4. (If still does not work...) Menu Run/Edit configurations (如果仍然不起作用...)菜单运行/编辑配置

在此处输入图片说明

I removed the lines from the configuration and all start running.我从配置中删除了这些行并开始运行。

--module-path
${PATH_TO_FX}
--add-modules
javafx.controls,javafx.fxml

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 IntelliJ IDEA - java:错误:无效源版本:18 - IntelliJ IDEA - java: error: invalid source release: 18 连续获取java:无效源版本:编译时出错1.9 - Continuously getting the java: invalid source release: 1.9 error when compiling 错误:Java:无效的目标版本:11 - IntelliJ IDEA - Error: Java: invalid target release: 11 - IntelliJ IDEA 错误:java:无效的源版本:Intellij 中的 8。 这是什么意思? - Error:java: invalid source release: 8 in Intellij. What does it mean? 错误:java:错误:源代码版本无效:13 使用带有 IntelliJ 的 JDK12 - Error:java: error: invalid source release: 13 using JDK12 with IntelliJ IntelliJ 2021.2.2 在编译程序时给出错误“java:错误:无效的源版本:18” - IntelliJ 2021.2.2 gives error 'java: error: invalid source release: 18' when compiling program IntelliJ - 无效的源版本:17 - IntelliJ - Invalid source release: 17 Java 错误:IntelliJ IDEA 不支持发行版 14 - Java error: release version 14 not supported in IntelliJ IDEA Java:无效的源版本:9 - Java: invalid source release: 9 IntelliJ Idea:Maven 编译警告关于 Java 11 上支持的注释处理器源版本“RELEASE_6” - IntelliJ Idea: Maven compilation warning about supported annotation processor source version 'RELEASE_6' on Java 11
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM