如何更改我在 IntelliJ(和 Maven)中使用的 Java 版本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31631987/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 18:54:06  来源:igfitidea点击:

How do I change the Java version I'm using in IntelliJ (and Maven)?

javamavenintellij-idea

提问by Maribeth

I'm working on a simple Java project and trying to use IntelliJ 14 for the first time; I have the Ultimate version through an education license (in my last year of undergrad!). I'm not super familiar with either Maven or IntelliJ. I want to use Java 8, and in fact I'm pretty sure this is the only version of the JDK I have installed on my computer. However, whenever I compile my project, I get a couple warnings and an info message:
Information: Using javac 1.8.0_51 to compile java sources
Warning: java: source value 1.5 is obsolete and will be removed in a future release
Warning: java: target value 1.5 is obsolete and will be removed in a future release

我正在做一个简单的 Java 项目,并且第一次尝试使用 IntelliJ 14;我通过教育许可证获得了 Ultimate 版本(在我本科的最后一年!)。我对 Maven 或 IntelliJ 都不太熟悉。我想使用 Java 8,实际上我很确定这是我在计算机上安装的唯一 JDK 版本。然而,每当我编译我的项目时,我都会收到一些警告和一条信息消息:
Information: Using javac 1.8.0_51 to compile java sources
Warning: java: source value 1.5 is obsolete and will be removed in a future release
Warning: java: target value 1.5 is obsolete and will be removed in a future release

So what I've noticed is that under IntelliJ IDEA > Preferences... > Build, Execution, Deployment > Compiler > Java Compiler, the target bytecode version is set to 1.5. I can change it to 1.8, but when I quit IntelliJ and re-open the project, it is reset to 1.5. The project bytecode version is blank for SDK default.
Here's what I've already done:

所以我注意到在 IntelliJ IDEA > Preferences... > Build, Execution, Deployment > Compiler > Java Compiler 下,目标字节码版本设置为1.5. 我可以将其更改为1.8,但是当我退出 IntelliJ 并重新打开项目时,它被重置为1.5. SDK 默认的项目字节码版本为空。
这是我已经做过的事情:

  • included the following in my maven pom

    <properties>
      <maven.compiler.source>1.8</maven.compiler.source>
      <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
    
  • Changed the IntelliJ preference file as noted hereon the IntelliJ website.

  • Set the global, project, and module SDK as noted hereon the IntelliJ website.

  • 在我的 maven pom 中包含以下内容

    <properties>
      <maven.compiler.source>1.8</maven.compiler.source>
      <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
    
  • 改变注意到的IntelliJ偏好文件在这里的的IntelliJ网站上。

  • 设置全局,项目和模块指出SDK这里的的IntelliJ网站上。

Am I missing a setting somewhere? What do I have to do to keep this value from changing every time I re-open IntelliJ?

我错过了某个地方的设置吗?每次重新打开 IntelliJ 时,我该怎么做才能防止此值发生变化?

回答by Gerald Mücke

Try using the maven compiler plugin in your pom.xmland set the source/target level accordingly. IDEA will change the project settings according to this setting

尝试在您的 Maven 编译器插件中使用pom.xml并相应地设置源/目标级别。IDEA 会根据这个设置更改项目设置

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

回答by Paulo Merson

I'm using IntelliJ IDEA 2017.2. To build the project (using Ctrl+F9), changing the maven-compiler-pluginsource and target versions was not enough. I had to:

我正在使用 IntelliJ IDEA 2017.2。要构建项目(使用 Ctrl+F9),仅更改maven-compiler-plugin源版本和目标版本是不够的。我不得不:

  1. Open File | Project Structure. (Or right-click a directory and select "Open Module Settings" to open the "Project Structure" dialog box.)
  2. Select Project Settings | Modules on the left side.
  3. For each module of my project (middle pane in the dialog box), change the "Language Level" on the right-side pane (Sources tab).
  1. 打开文件 | 项目结构。(或右键单击目录并选择“打开模块设置”以打开“项目结构”对话框。)
  2. 选择项目设置 | 左侧的模块。
  3. 对于我项目的每个模块(对话框中的中间窗格),更改右侧窗格(源选项卡)上的“语言级别”。

Then I ran into this other problem. To solve it, I had to:

然后我遇到了另一个问题。为了解决它,我不得不:

  1. Open File | Settings.
  2. Type Compiler in the search box and select "Java Compiler".
  3. On the right-hand side, bump up the "Target bytecode version" for each module of my project.
  1. 打开文件 | 设置。
  2. 在搜索框中键入 Compiler,然后选择“Java Compiler”。
  3. 在右侧,为我的项目的每个模块增加“目标字节码版本”。

Aside: I've been using IDEs since Turbo Pascal 2.0 in the 80s. I find it counter-intuitive that such a modern IDE makes it so hard to open the "Project Settings". First, if you right-click the project (top left on the main window) there's no option to open project settings. Second, the "Project" bar on the top left of the main window has the gear symbol with a drop-down list, but no option to access project settings there either. Third, if you right-click the project directory in the project explorer view on the left, you see "Open Module Settings" but there's no "Project Settings". Fourth, if you open File on the main menu, you'll see "Settings", "Other Settings" but no "Project Settings". Counter-intuitive. :^|

旁白:自 80 年代的 Turbo Pascal 2.0 以来,我一直在使用 IDE。我发现这样一个现代 IDE 使得打开“项目设置”变得如此困难是违反直觉的。首先,如果您右键单击项目(主窗口的左上角),则没有打开项目设置的选项。其次,主窗口左上角的“项目”栏有带有下拉列表的齿轮符号,但也没有访问项目设置的选项。第三,如果您在左侧的项目资源管理器视图中右键单击项目目录,您会看到“打开模块设置”但没有“项目设置”。第四,如果你在主菜单上打开文件,你会看到“设置”、“其他设置”但没有“项目设置”。违反直觉。:^|