首页 文章

IDEA:javac:源版本1.7需要目标版本1.7

提问于
浏览
425

当运行JUnit测试时,使用IntelliJ IDEA,我得到了

enter image description here

我怎么能纠正这个?

  • 使用SDK 1.7

  • 模块语言级别为1.7

Maven构建工作正常 . (这就是我在IDEA配置问题中相信这一点的原因)

18 回答

  • 595

    如果Maven构建工作正常,请尝试同步Maven和IntelliJ IDEA项目的结构 .

    Maven tool window中,单击刷新按钮
    refresh
    . 按下此按钮,IntelliJ IDEA将在Maven工具窗口中解析项目结构 .

    请注意,如果您正在使用EAP构建,这可能无济于事,因为Maven同步功能有时可能会被破坏 .

  • 4

    我在设置>编译器> java编译器中的've found required options ('目标字节码版本')在我的情况下(intelij idea 12.1.3)

  • 1

    如果所有以前的解决方案都不适合您(这是我的情况),您可以删除intellij配置文件:

    • project_directory / .idea / compiler.xml

    • project_directory / .idea / encodings.xml

    • project_directory / .idea / misc.xml

    • project_directory / .idea / modules.xml

    • project_directory / .idea / vcs.xml

    • project_directory / .idea / workspace.xml

    Intellij稍后会重新生成新的 . However, BE CAREFUL, this will also delete all intellij configuration made on the projet (i.e: configuration of debug mode, ...)

  • 1

    从一个时刻到另一个时刻,我也没有明确的理由得到了这个错误 . 我在编译器/模块等上更改了各种设置 . 但最后我通过重新导入Maven项目重新创建了IntelliJ项目,问题解决了 . 我认为这是一个错误 .

    IntelliJ 12 129.961

  • 0

    确保选择了正确的依赖关系 . 文件>项目结构

    选择项目并导航到“依赖关系”选项卡 . 从下拉列表中选择正确的依赖性或创建新的 .

  • 13

    检查.idea / misc.xml有时您需要手动更改languageLevel =“JDK_1_X”属性

  • 9

    我发现了另一种遇到此错误的方法 . 如果你一直在重新组织你的目录结构,你可以得到这个,并且你的一个poms指向不再配置javac的旧父(因为该配置被移动到中间级别) . 如果发生这种情况,顶级默认为1.5,行为不当的低级别pom继承它 .

    因此,当您看到此错误时,检查的另一件事是您的pom结构正确匹配您的目录结构 .

  • 4

    我通过将字段设置为空白来解决它:

    设置>编译器> Java编译器>项目字节码版本

    比IntelliJ使用JDK默认版本 .

  • 1

    在以下路径中修改项目的编译器设置文件,并将“target”更改为1.7:

    /project/.idea/compiler.xml

    <bytecodeTargetLevel>
      <module name="project-name" target="1.7" />
    </bytecodeTargetLevel>
    
  • 1

    如果你使用maven 3和JDK 7,你有没有看过你应该喜欢的构建配置?

    <build>
        <finalName>SpringApp</finalName>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
            ...
        </plugins>
        ...
    </build>
    
  • 5

    我解决了波纹管方法

    文件>>项目结构>>项目>>项目语言水平 - >设置正确的版本(例如:1.5)

  • 61

    您需要在构建配置中更改Java编译器版本 .

    enter image description here

  • 2

    我遇到了这个,修复是转到项目设置>模块>单击特定模块>依赖项选项卡 . 我注意到模块SDK仍然设置为1.6,我将其更改为1.7并且它工作正常 .

  • 22

    如果是Gradle项目,请在build.gradle文件中搜索以下设置:

    sourceCompatibility = "xx"
    targetCompatibility = "xx"
    

    对于所有子目录,在根build.gradle文件中,您可以放置:

    subprojects { project ->
        sourceCompatibility = "1.7"
        targetCompatibility = "1.7"
    }
    

    虽然您可以在Idea> Settings中手动设置语言级别,但如果它是Gradle项目,Idea会自动同步Gradle设置中的模块.iml文件(使用Idea 15进行测试) . 因此,刷新gradle时,所有手动更改都会被覆盖 .

    基于Gradle documentation,如果未设置,则使用当前的JVM配置 .

  • 352

    很可能你在这里从Maven导入的编译器选项不正确:

    compiler options

    还要检查屏幕截图中列出的项目和模块字节码( target )版本设置 .

    配置 source 语言级别的其他位置:

    • 项目结构| Project

    project

    • 项目结构|模块(检查每个模块)| Sources

    sources

    Maven default language level1.5 (5.0),您将在上面的屏幕截图中看到此版本为模块语言级别 .

    这可以使用 pom.xml 中的maven-compiler-plugin配置进行更改:

    <project>
      [...]
      <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>
      [...]
    </project>
    

    要么

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

    Maven Projects工具窗口中_377152_ Maven项目后,IntelliJ IDEA将遵循此设置:

    reimport

  • 0

    IntelliJ IDEA 14.1 中,"Target bytecode version"位于不同的位置 .

    以下更改对我有用:

    File > Settings... > Build, Execution, Deployment > Compiler > Java Compiler :将 Target bytecode version 从1.5更改为1.8

    enter image description here

    enter image description here

  • 0

    IntelliJ 15,2016&2017

    类似于下面针对IntelliJ 13和14讨论的内容,但在“设置/首选项”面板中有一个额外的级别:设置>构建,执行,部署>编译器> Java编译器 .

    enter image description here

    IntelliJ 13&14

    在IntelliJ 13和14中,检查“设置”>“编译器”>“Java编译器UI”以确保您没有针对其他用户模块中的字节码版本 .

    enter image description here

  • 40

    我从IntelliJ IDEA 14到v14.1进行了一次小的升级之后就点击了这个 . 对我来说,更改top / parent pom的编辑帮助然后点击重新导入Maven(如果它不是自动的) .

    但它可能足以右键单击模块/聚合/父模块和Maven - > Reimport .

相关问题