155

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

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
    <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?

RealSkeptic
  • 33,993
  • 7
  • 53
  • 79
asdlfkjlkj
  • 2,258
  • 6
  • 20
  • 28

14 Answers14

274

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>

If you are looking for a way to make it work with Java versions 9+ please take a look at @JDelorean's answer.

codewario
  • 19,553
  • 20
  • 90
  • 159
Jorge Campos
  • 22,647
  • 7
  • 56
  • 87
  • 5
    I think I already did the second way and I posted the code as well. Don't see what you changed in that code – asdlfkjlkj Feb 13 '15 at 23:20
  • 1
    Yeah. When I answered you haven't post the code. So, add the first part of my answer. The `properties` tag. If your POM already have a `properties` tag just add the tags inside it, it should work fine. – Jorge Campos Feb 14 '15 at 03:23
  • @asdlfkjlkj your eclipse is using embeded maven or a external maven installation ? Another question your eclipse is pointing to a JRE or to a JDK ? This is two question regarding eclipse + maven that make all difference. – Jorge Campos Feb 15 '15 at 20:56
  • @JorgeCampos - I posted a new question. Do u have any idea on that?http://stackoverflow.com/questions/42855512/java-custom-image-in-joptionpane-showconfirmdialog-is-not-working – Aishu Mar 17 '17 at 12:06
  • @JorgeCampos where do I find the `maven-complier-plugin` to add the ``? thanks – 夢のの夢 Jan 22 '18 at 17:14
  • @夢のの夢 If you are in a maven project you don't need to find it, just add it to your pom.xml and make sure your computer has internet access. Maven will resolve this as a dependency as well. It will download it and add in your local repository. – Jorge Campos Jan 22 '18 at 21:29
34

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:

<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. Works for versions 9 and above.

JDelorean
  • 631
  • 13
  • 26
  • 1
    Also true for Java 10+ and would like to add that in a multi-module Maven project is only required in the parent POM. – mmeany May 27 '18 at 10:25
  • 1
    With Java 9 and above, there is a new `--release ` compiler option, which is preferred over source/target. This can be configured with `maven.compiler.release` property or `` setting in maven-compiler plugin. See: https://stackoverflow.com/questions/43102787/what-is-the-release-flag-in-the-java-9-compiler – Thomas Taylor Aug 22 '19 at 19:44
  • @ThomasTaylor However, only specifying the `` property doesn't seem enough for Eclipse. If I write `17` without a source and target, and then run `Update Maven project` in Eclipse, it automatically reverts back to a `J2SE-1.5` container... Is this an Eclipse bug? – Safron Aug 30 '23 at 10:26
  • Okay, this is not an Eclipse bug. See https://bugs.eclipse.org/bugs/show_bug.cgi?id=566704#c8 – Safron Aug 30 '23 at 10:59
16

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.

David Avendasora
  • 4,538
  • 1
  • 16
  • 15
  • 1
    How to change org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5 – aName Jun 28 '18 at 09:05
  • Downvoting for saying "developer stupidity can be one of those reasons". – Luke Hutchison Feb 27 '19 at 05:14
  • 1
    In my case I am using Maven/Tycho to build Eclipse plugins, and the above default was being ultimately derived from the execution environment specified in the MANIFEST.MF file. Changing the execution environment to JavaSE-1.8 fixed the issue. – mat101 Oct 08 '19 at 13:06
  • 1
    @LukeHutchison - I was referring to my own stupidity, not others - but I appreciate the feedback on the answer. – David Avendasora Dec 13 '21 at 06:00
5

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> 
Avinav Mishra
  • 718
  • 9
  • 12
2

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.

Simple updating Eclipse to latest version solve this problem.

user902383
  • 8,420
  • 8
  • 43
  • 63
  • Yeah, no. I'm running Neon, my workspace settings are all 1.8, but when I import (or update) a Maven project, Eclipse insists on using J2SE-1.5. – Antares42 Feb 20 '17 at 20:21
  • @Antares42 are you able to build from command line without any issues? Which m2e version are you using? – user902383 Feb 21 '17 at 13:16
  • 1
    I can build using the command line (or rather "Run as -> Maven build...") without problems. But as soon as I do "Maven -> Update project" I get the JRE libraries as mentioned and Eclipse fails to build the project. I can still build using Maven though. Once I add a ``maven.compiler.source`` property as mentioned in an answer above, everything works fine. My m2e version is 1.7.1-20161104-1805. – Antares42 Feb 21 '17 at 18:56
2

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-plugin version to 1.7 and did maven update project, it updated the Eclipse JRE system library to 1.7. enter image description here

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

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.

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

nsandersen
  • 896
  • 2
  • 16
  • 41
1

I am using Java 11. This is how the complete pom.xml file looks like after adding <properties> and <plugin>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.akshay</groupId>
  <artifactId>1000SpringSecurityEg</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>1000SpringSecurityEg Maven Webapp</name>
  <url>http://maven.apache.org</url>
    
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  
    <properties>
        <javaVersion>11</javaVersion>
        <maven.compiler.source>${java.version}</maven.compiler.source>
        <maven.compiler.target>${java.version}</maven.compiler.target>
    </properties>
  
  <build>
    <finalName>1000SpringSecurityEg</finalName>
    
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>11</source>
                    <target>11</target>
                </configuration>
            </plugin>
        </plugins>
        
  </build>
</project>

The above code worked for me. Hope it works for you as well.

akshay
  • 135
  • 1
  • 7
1
<properties>
   <maven.compiler.source>11</maven.compiler.source>
   <maven.compiler.target>11</maven.compiler.target>
</properties>

I added these lines in my "pom.xml" file and it worked.

0

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.

Claeyssic
  • 21
  • 6
0

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

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

0

I experienced with JRE 15.0.1 one must ONLY specify the compiler plugin like

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

If I also provide the properties like

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

this will again reset to JRE 1.5 on Maven / Update Project !!!

ray_ray_ray
  • 316
  • 1
  • 14
-1

Check in pom.xml under properties if there is any tag with this maven.enforcer.plugin.version. Delete it and replace that with the below code

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

under properties tag.

And under build, replace the plugins with the below code:

 <build>
    <finalName>1000SpringSecurityEg</finalName>
    
        <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>

This solution resolved my issue.

jasie
  • 2,192
  • 10
  • 39
  • 54
-4

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

Undo
  • 25,519
  • 37
  • 106
  • 129
puja
  • 1