记一次Intellij-IDEA配置JDK1.8,支持Lambda新特性

本文介绍如何在IntelliJ IDEA中正确配置JDK1.8环境,包括项目结构配置、编译模式调整及pom.xml文件中compiler插件版本的指定方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

背景

  • Jdk 1.8支持Lambda新特性,想试试直接在IDEA使用,于是创建一个web项目,着手准备使用时,发现了以下的问题。环境已经中已经配置了jdk1.8环境

解决方法

  1. 先打开ideaproject structure配置
  2. 其次将所有的模块配置成为1.8环境,这样保证它的语法检测不报错,但是编译的时候他还是选择idea的默认配置,接下来更改编译配置

3. 进入idea的设置页面,将编译模式改成1.8

异常问题

如果缺少第三部,会出现如下错误

FAQ:

设置完成之后,如果重新启动IDEA时,就会发现所有的设置又重新恢复到默认状态(JDK 1.5)

原因是:

Apache Maven Compiler Plugin

The Compiler Plugin is used to compile the sources of your project. Since 3.0, the default compiler is javax.tools.JavaCompiler (if you are using java 1.6) and is used to compile Java sources. If you want to force the plugin using javac, you must configure the plugin option forceJavacCompilerUse.

Also note that at present the default source setting is 1.5 and the default target setting is 1.5, independently of the JDK you run Maven with. If you want to change these defaults, you should set source and target as described in Setting the -source and -target of the Java Compiler.
复制代码

解释是:

该插件从3.0版本开始,
默认编译器是javax.tools.JavaCompiler (前提是JDK 1.6以后);
如果想使用javac,需要手动设置。
当前(Version: 3.5.1),
默认使用JDK 1.5解析和编译源码,
与运行Maven的JDK版本无关!
复制代码

PS:

    <project>
      [...]
      <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>
复制代码

解决方法:

pom.xml中指定compiler的版本

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.3.2</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
    </plugins>
</build>
复制代码

当然还有一个简单写法

<properties>
  <maven.compiler.source>1.8</maven.compiler.source>
  <maven.compiler.target>1.8</maven.compiler.target>
</properties>
复制代码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值