4

I'm running Junits on our app server that fail with Unsupported major.minor version 51.0. But, the web application then builds, deploys, and runs fine. I don't understand how this can happen. My maven compiler's config looks like this:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-compiler-plugin</artifactId> 
  <version>2.3.2</version>
  <configuration>
    <verbose>true</verbose>
    <fork>true</fork>
    <source>1.7</source>
    <target>1.7</target>
    <executable>${JAVA_1_7_HOME}/bin/javac</executable>
    <encoding>ISO-8859-1</encoding>
    <verbose>true</verbose> 
  </configuration>
</plugin>

And my Junit dependency:

<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <scope>test</scope>
  <version>4.10</version>
</dependency> 

Can anyone think of a reason this is failing?

Todd
  • 2,829
  • 5
  • 34
  • 56
  • 2
    That looks like you have an old version of java (the JRE, not the compiler) on the app server. – Tom Anderson Oct 15 '13 at 21:22
  • 2
    How do you execture the maven build itself? Make sure it uses a current version of the java runtime. – Thomas Oct 15 '13 at 21:23
  • http://stackoverflow.com/questions/10382929/unsupported-major-minor-version-51-0 – srkavin Oct 15 '13 at 23:58
  • I agree it sounds like the JDK used to compile the WAR/Junits is a different version than the JRE used to run the Junits. What mechanism does Maven use to determine what "Java" to use to run the Junits? (this is running on Linux). I have a handful of apps running on this app server, but only one fails its Junits with the version error. Very strange. – Todd Oct 16 '13 at 14:58
  • More specifically, the JDK used to compile the code is newer than the one running the code. The JVMs are backwards compatible but not forward. – Jeff Richley Oct 16 '13 at 19:07
  • https://www.baeldung.com/java-lang-unsupportedclassversion might be helpful – XoXo Oct 19 '21 at 19:21

1 Answers1

6

The maven-surefire-plugin is responsible for running JUnit tests. Try adding this config.

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId> 
  <version>2.16</version>
  <configuration>
    <jvm>${JAVA_1_7_HOME}/bin/javac</jvm>
  </configuration>
</plugin>
user944849
  • 14,524
  • 2
  • 61
  • 83
  • JAVA_HOME on the app server points to Java 6. So, the app is building using Java 7 (as specified in the compiler plugin), but Maven is using the JAVA_HOME JRE (Java 6) to run Junits in. The Surefire plugin fixed that. Thanks everyone! – Todd Oct 16 '13 at 20:34