Java version automatically change to java 1.5 after maven update

前端 未结 11 496
盖世英雄少女心
盖世英雄少女心 2020-12-22 17:29

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 l

相关标签:
11条回答
  • 2020-12-22 18:14

    The root-cause of this issue is that if for any reason Eclipse's cannot resolve a valid value for the maven.compiler.source property 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.

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

    However, Jorge's answer didn't appear to 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 never going to resolve to the (completely different) property javaVersion and Eclipse ignored the property and used the default.

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

    0 讨论(0)
  • 2020-12-22 18:19

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

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

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

    <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's answer down here and don't forget to give him an upvote as well :)

    0 讨论(0)
  • 2020-12-22 18:19

    I allow myself to update that subject with Java 11.

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

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

    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

    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.

    0 讨论(0)
  • 2020-12-22 18:20

    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.

    <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> 
    
    0 讨论(0)
  • 2020-12-22 18:21

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

    0 讨论(0)
提交回复
热议问题