44

I'm using Eclipse EE 3.7 with m2e plugin installed. I have JDK7 set in eclipse. When I import maven projects, the JRE is set to JRE System Library [J2SE-1.5], So i have compilation issues with java 6 related stuff. Instead I want the JRE in eclipse to be by default set to JRE System Library [J2SE-1.6]

When i try to open a new project in eclipse File -> new -> Java project on the first screen i have an option to choose JRE and the third option is Use default JRE (currently 'jdk1.7.0_03')

From this i can see that the default JRE in Eclipse is 1.7, but when i import new Maven projects, the JRE is set to 1.5 by default.

Any help, how can i do this?

Sinisha Mihajlovski
  • 1,811
  • 1
  • 20
  • 34
  • 1
    I am experiencing a similary problem. Whatever the JRE System Library is set to, performing a Project>Maven>Update Project... resets the JRE System Library to J2SE-1.5. – mbmast Dec 03 '15 at 18:14
  • 1
    Old question but still helpful in 2019 since it happens also in current SpringBoot 2 projects. – groovedigga Oct 09 '19 at 09:04

4 Answers4

84

The problem is not with Eclipse, but with the projects you're importing. m2e will set the project's JRE to match the maven project. The POM specifies the JRE version, and this is defaulted to 1.5 if not present. You need this in the POM:

<build>
     <plugins>
        <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.0</version>
                <configuration>
                   <source>1.7</source>
                   <target>1.7</target>
                </configuration>
        </plugin>
    </plugins>
</build>
artbristol
  • 32,010
  • 5
  • 70
  • 103
  • 2
    [Here](http://maven.apache.org/plugins/maven-compiler-plugin/examples/compile-using-different-jdk.html) is some more info from Maven site – Saikat Nov 14 '13 at 08:59
  • for people who have more than one project this question could also be of interest https://stackoverflow.com/questions/2531650/default-maven-compiler-setting – Xtroce Mar 26 '14 at 14:37
  • I put it in of pom file. It works, thanks! – Jugal Panchal Jul 12 '15 at 20:39
  • I have something like ` UTF-8 1.8 1.8 ` but I don't know the difference between it and your solution. – Islam Azab Jan 16 '17 at 17:05
6

artbristol gave the correct answer (and I upvoted him).

That was in 2012. Here is an update more appropriate for today (2016, Java 8, Spring 4.x/Servlet 3.x):

<plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-compiler-plugin</artifactId>
   <version>3.0</version>
   <configuration>
      <source>1.7</source>
      <target>1.7</target>
   </configuration>
</plugin>
paulsm4
  • 114,292
  • 17
  • 138
  • 190
3

The root cause of this issue is Eclipse cannot resolve a valid value for the maven.compiler.source property when updating the .classpath file from the pom, it is simply using default one i.e

org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5.

Just Add following properties into you pom.xml and update project:

<properties>
            <javaVersion>1.8</javaVersion>
            <maven.compiler.source>${java.version}</maven.compiler.source>
            <maven.compiler.target>${java.version}</maven.compiler.target>
</properties>
Jay Prakash
  • 787
  • 6
  • 22
  • 1
    Great answer, thx. This is the better solution in my case: Problems are shown in Eclipse but a maven build in the console works fine. Then I only need a hint for m2e and not an additional plugin-definition. – groovedigga Oct 09 '19 at 09:01
0

You can have your brand new Maven project in Eclipse have JRE System Library version other than JavaSE-1.5. For that,find below jar in the following location.

  • File: maven-compiler-plugin-3.1.jar
  • Location: .m2\repository\org\apache\maven\plugins\maven-compiler-plugin\3.1

Then unzip the jar and locate "META-INF\maven\plugin.xml" which has four occurrences of default-value="1.5". Replace all four 1.5 with 1.8 or whatever the version you want. You would know the rest of the process. Reference

Park JongBum
  • 1,245
  • 1
  • 16
  • 27