Maven 更新后,Java 版本自动更改为 Java 1.5

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28509928/
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-08-11 06:21:39  来源:igfitidea点击:

Java version automatically change to java 1.5 after maven update

javaeclipsemaven

提问by asdlfkjlkj

I am using eclipse as IDE. When I right click on the project and then click maven update my java version change to 1.5. Here is what I did so far, I followed all the steps listed here

我使用 Eclipse 作为 IDE。当我右键单击该项目然后单击 maven 时,将我的 java 版本更改为 1.5。这是我到目前为止所做的,我遵循了此处列出的所有步骤

http://qussay.com/2013/09/13/solving-dynamic-web-module-3-0-requires-java-1-6-or-newer-in-maven-projects/

http://qussay.com/2013/09/13/solving-dynamic-web-module-3-0-requires-java-1-6-or-newer-in-maven-projects/

  1. I changed "Java build path" to "workspace default jre 1.8.0_25"
  2. Then changed "java compiler" to 1.8
  3. Then changed "project facets">java>1.8
  4. Changed pom.xml java version to 1.8
  1. 我将“Java 构建路径”更改为“工作区默认 jre 1.8.0_25”
  2. 然后将“java编译器”更改为1.8
  3. 然后改成“project facets”>java>1.8
  4. 将 pom.xml java 版本更改为 1.8
    <build>
        <plugins>
            <plugin>
                <groupId>org.eclipse.jetty</groupId>
                <artifactId>jetty-maven-plugin</artifactId>
                <version>9.1.3.v20140225</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugin</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

After all this when I click on "Maven update" my java version change to 1.5 automatically. Also in above steps, first two step's version also change to 1.5 automatically. How can I fix this?

毕竟,当我单击“Maven 更新”时,我的 Java 版本会自动更改为 1.5。同样在上述步骤中,前两步的版本也自动更改为 1.5。我怎样才能解决这个问题?

采纳答案by Jorge Campos

Open your pom.xmlfile and add the following lines on it:

打开您的pom.xml文件并在其上添加以下几行:

<properties>
   <maven.compiler.source>1.8</maven.compiler.source>
   <maven.compiler.target>1.8</maven.compiler.target>
</properties>

Where 1.8is the java version of your current JDK/JRE. Another way of doing this is adding a <build>with the maven-compile-pluginas

1.8当前 JDK/JRE 的 Java 版本在哪里。这样做的另一种方式是增加一个<build>maven-compile-plugin作为

<build>
<plugins>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.2</version> <!-- or whatever current version -->
    <configuration>
      <source>1.8</source>
      <target>1.8</target>
    </configuration>
  </plugin>
</plugins>
</build>

EDIT

编辑

If you are looking for a way to make it work with Java versions 9+ please take a look at @JDelorean'sanswerdown here and don't forget to give him an upvote as well :)

如果您正在寻找一种使其与 Java 9+ 版本一起使用的方法,请在此处查看@JDelorean 的回答,并且不要忘记给他点赞 :)

回答by nsandersen

I had this problem. In my case the <properties> tag & nested tags Jorge Campos mentions above were in the wrong place. If I put them between the <hostversion> and <dependencies> tags in the pom.xml file, then this behaviour stopped.

我有这个问题。在我的例子中,Jorge Campos 上面提到的 <properties> 标签和嵌套标签在错误的地方。如果我将它们放在 pom.xml 文件中的 <hostversion> 和 <dependencies> 标记之间,那么这种行为就会停止。

That can be picked up in Eclipse if validation of these files is switched on.

如果打开了对这些文件的验证,则可以在 Eclipse 中获取。

回答by user902383

I encounter similar issue on one of my team mate machine. He was using old version of Eclipse, I believe it he was using Keppler. Project after being updated change JRE version to 1.5.

我在我的一台队友机器上遇到了类似的问题。他使用的是旧版本的 Eclipse,我相信他使用的是 Keppler。更新后的项目将 JRE 版本更改为 1.5。

Simple updating Eclipse to latest version solve this problem.

简单地将 Eclipse 更新到最新版本即可解决此问题。

回答by puja

I changed Eclipse from kepler to neon and then updated my project by with Maven -> Update Project.

我将 Eclipse 从 Kepler 更改为 neon,然后使用 Maven -> Update Project 更新我的项目。

回答by Avinav Mishra

Add this lines to your pom.xml, then right click your JRE System Library -> Properties -> Set your correct execution environment to Java 1.8 or version you want to set.

将此行添加到您的 pom.xml,然后右键单击您的 JRE 系统库 -> 属性 -> 将正确的执行环境设置为 Java 1.8 或您要设置的版本。

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.2</version> <!-- or whatever current version -->
    <configuration>
      <source>1.8</source>
      <target>1.8</target>
    </configuration>
  </plugin> 

回答by David Avendasora

The root-cause of this issue is that if for any reasonEclipse's cannot resolve a validvalue for the maven.compiler.sourceproperty when generating/updating the .classpath file from the pom, it will simply default to using org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5.

此问题的根本原因是,如果Eclipse在从 pom 生成/更新 .classpath 文件时由于任何原因无法解析该属性的有效值maven.compiler.source,它将简单地默认使用org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5.

As expertly answered by @jorge-campos, there are multiple ways to set that property.

正如@jorge-campos 的专业回答,有多种方法可以设置该属性。

However, Jorge's answer didn't appearto work for me. Here were my settings:

然而,豪尔赫的回答似乎对我不起作用。这是我的设置:

<properties>
    <javaVersion>1.8</javaVersion>
    <maven.compiler.source>${java.version}</maven.compiler.source>
    <maven.compiler.target>${java.version}</maven.compiler.target>
</properties>

...

...

Exactly. ${java.version}is nevergoing to resolve to the (completely different) property javaVersionand Eclipse ignored the property and used the default.

确切地。${java.version}从来没有打算解决的(完全不同的)财产javaVersion和Eclipse忽略的财产,并使用默认值。

Which brings me back to the "for any reason" part I opened with; developer stupidity can be one of those reasons.

这让我回到了开头的“出于任何原因”的部分;开发人员的愚蠢可能是这些原因之一。

回答by JDelorean

Had the same issue when I installed Java 9. My project would default to J2SE-1.5 Execution Environment. Strangely, Java 9 compliance level is not referenced like previous versions, i.e. "1.8", but as "9". So I had to provide my properties and Maven compiler plugin config accordingly:

我在安装 Java 9 时遇到了同样的问题。我的项目将默认为 J2SE-1.5 执行环境。奇怪的是,Java 9 合规性级别不像以前的版本那样被引用,即“1.8”,而是“9”。所以我必须相应地提供我的属性和 Maven 编译器插件配置:

<properties>
    <maven.compiler.source>9</maven.compiler.source>
    <maven.compiler.target>9</maven.compiler.target>
</properties>

and

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
        <source>9</source>
        <target>9</target>
    </configuration>
</plugin>

This seems to have solved the problem.

这似乎解决了问题。

回答by Claeyssic

I allow myself to update that subject with Java 11.

我允许自己使用 Java 11 更新该主题。

I have installed OpenJDK11 on my computer, and I wanted to use it in an app.

我已经在我的电脑上安装了 OpenJDK11,我想在一个应用程序中使用它。

I had trouble because Eclipse would always change my JRE to JavaSE-1.5 when I updated my project with Maven.

我遇到了麻烦,因为当我使用 Maven 更新我的项目时,Eclipse 总是将我的 JRE 更改为 JavaSE-1.5。

I had set everything as you said, but I was always directly selecting in my Java Build Path "java-11-openjdk.x86_64" as one of my Alternante JRE. I fixed my problem by selecting in "Execution environment" JavaSE-10 (but you have to double click on it and then choose as a compatible JRE your OpenJDK11 version) as shown on the picture. Execution environment setup

我已经按照你说的设置了所有内容,但我总是在我的 Java 构建路径中直接选择“java-11-openjdk.x86_64”作为我的备用 JRE 之一。我通过在“执行环境”中选择 JavaSE-10(但您必须双击它,然后选择与您的 OpenJDK11 版本兼容的 JRE)解决了我的问题,如图所示。 执行环境设置

The project will use Java 11 thanks to that (picture) but you have to write 10 for the java-version in the pom.xml and also set java 10 on the Project Facets.

由于该(图片),该项目将使用 Java 11,但您必须在 pom.xml 中为 java-version 编写 10,并在 Project Facets 上设置 java 10。

回答by blindcodifier9734

In my case (old JBoss Developer Studio), the issue was the JRE environments did not include 1.8 (only 1.7). When I switched the maven-compiler-pluginversion to 1.7 and did maven update project, it updated the Eclipse JRE system library to 1.7. enter image description here

就我而言(旧的 JBoss Developer Studio),问题是 JRE 环境不包括 1.8(只有 1.7)。当我将maven-compiler-plugin版本切换到 1.7 并进行 maven update 项目时,它更新了 Eclipse JRE 系统库到 1.7。 在此处输入图片说明

So the solution is to either get a newer IDE version that includes a built-in JRE environment that is 1.8 or later, or try to install it manually (see https://stackoverflow.com/a/35204314)

因此,解决方案是获取包含 1.8 或更高版本的内置 JRE 环境的较新 IDE 版本,或者尝试手动安装(参见https://stackoverflow.com/a/35204314

回答by Andrea La Rosa

I've resolved the issue installing the eclipse update "JAVA 12" from the market. It makes my eclipse pass from Kepler to Luna.

我已经解决了从市场安装 eclipse 更新“JAVA 12”的问题。它使我的日食从开普勒传递到月神。

After that, i have been able to set 1.8 as standard JDK, fixing the "maven update" problem.

之后,我已经能够将 1.8 设置为标准 JDK,修复了“maven 更新”问题。