102

When running our Maven build, a member of my team gets a NoSuchMethodError in one of the dependant plug-ins when executing. However, when we run java -version from her command line, it indicates Java 1.6.0_26. The error obviously seems to be screaming that Maven is using Java 5.

How do I figure out and change what version of Java is being used by Maven?

3 potentially important notes:

  1. This is executed at the command line, not using Eclipse plugin
  2. JAVA_HOME is set to a Java 1.6 JDK
  3. Using Maven 2.2.1
Laurel
  • 5,965
  • 14
  • 31
  • 57
shsteimer
  • 28,436
  • 30
  • 79
  • 95

3 Answers3

162

mvn -version will output which java it's using. If JAVA_HOME is set to a valid JDK directory and Maven is using something else, then most likely someone has tampered with the way that Maven starts up.

Ryan Stewart
  • 126,015
  • 21
  • 180
  • 199
  • 7
    You can define the Java to be used with maven. See the thread: http://stackoverflow.com/questions/18813828/why-maven-use-jdk-1-6-but-my-java-version-is-1-7 – Neto Marin Mar 30 '14 at 17:38
14

You will need to configure maven-compiler-plugin to use 1.6 source and target parameters ( by default it is 1.5 ).

This is the best done in your project's parent pom in <pluginManagment> section ( but you can always configure it per individual projects of course ).

  <build>

    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>2.3.2</version>
          <configuration>
            <source>1.6</source>
            <target>1.6</target>

          </configuration>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
Alexander Pogrebnyak
  • 44,836
  • 10
  • 105
  • 121
3

mvn -version tells you what compiler Maven is using, so this answers the question unless your POM specifies override values for the versions to be used.

Here's the easiest way to specify the overrides in pom.xml:

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

Alternatively, the maven-compiler-plugin can be specified more explicitly. In that case, look at the <source> and <target> values associated with the plugin.

Both of these options are described here.

If your POM has a <parent>, then you need to check that as well (recursively).

Note: When source and target are specified, these values are passed as command-line options to the compiler. Using this feature, you can compile or run against earlier Java versions than the compiler's nominal value.

Brent Bradburn
  • 51,587
  • 17
  • 154
  • 173
  • An example of how to set the *compiler* if you don't want to simply use `javac` from your path: `JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64 mvn clean install`. Your-mileage-may-vary in terms of how/when this can be overridden under Maven. – Brent Bradburn Oct 05 '17 at 21:54
  • As an ultimate sanity check, you could ask `javac`/`java` from within the build environment by setting the build parameters to add `-version` to the command-line argument -- similar to [here](https://stackoverflow.com/a/25079415/86967) (for Gradle). – Brent Bradburn Oct 27 '17 at 22:34
  • 1
    `` in `pom.xml` is not relevant to this: [Specifying java version in maven - differences between properties and compiler plugin](https://stackoverflow.com/q/38882080/86967) – Brent Bradburn Apr 05 '18 at 02:41
  • 1
    Remember that `JAVA_HOME` could be already set in your environment -- overriding the `PATH` variable. – Brent Bradburn Apr 05 '18 at 02:43
  • This seems like it could be important, if it wasn't so ridiculous: https://maven.apache.org/enforcer/enforcer-rules/requireJavaVersion.html – Brent Bradburn Apr 05 '18 at 03:02