Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java version automatically change to java 1.5 after maven update

People also ask

How does Maven decide Java version?

Maven uses the JAVA_HOME parameter to find which Java version it is supposed to run.

Does Maven support Java 15?

Queries related to “maven-compiler-plugin source target java 15” Source option 5 is no longer supported.


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 :)


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.


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.


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> 

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.


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)


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.