Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mvn test fails because of Surefire JDK version?

I am trying to run mvn test on my project. The project itself compiles and installs but man test fails with the following output:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.18:test (default-test) on project udms-server: Execution default-test of goal org.apache.maven.plugins:maven-surefire-plugin:2.18:test failed: There was an error in the forked process
[ERROR] java.lang.UnsupportedClassVersionError: some/project/Event : Unsupported major.minor version 52.0
[ERROR] at java.lang.ClassLoader.defineClass1(Native Method)
[ERROR] at java.lang.ClassLoader.defineClass(Unknown Source)
[ERROR] at java.security.SecureClassLoader.defineClass(Unknown Source)
[ERROR] at java.net.URLClassLoader.defineClass(Unknown Source)
[ERROR] at java.net.URLClassLoader.access$100(Unknown Source)
[ERROR] at java.net.URLClassLoader$1.run(Unknown Source)
[ERROR] at java.net.URLClassLoader$1.run(Unknown Source)
[ERROR] at java.security.AccessController.doPrivileged(Native Method)
[ERROR] at java.net.URLClassLoader.findClass(Unknown Source)
[ERROR] at java.lang.ClassLoader.loadClass(Unknown Source)
[ERROR] at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
[ERROR] at java.lang.ClassLoader.loadClass(Unknown Source)
[ERROR] at java.lang.ClassLoader.defineClass1(Native Method)
[ERROR] at java.lang.ClassLoader.defineClass(Unknown Source)
[ERROR] at java.security.SecureClassLoader.defineClass(Unknown Source)
[ERROR] at java.net.URLClassLoader.defineClass(Unknown Source)
[ERROR] at java.net.URLClassLoader.access$100(Unknown Source)
[ERROR] at java.net.URLClassLoader$1.run(Unknown Source)
[ERROR] at java.net.URLClassLoader$1.run(Unknown Source)
[ERROR] at java.security.AccessController.doPrivileged(Native Method)
[ERROR] at java.net.URLClassLoader.findClass(Unknown Source)
[ERROR] at java.lang.ClassLoader.loadClass(Unknown Source)
[ERROR] at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
[ERROR] at java.lang.ClassLoader.loadClass(Unknown Source)
[ERROR] at org.apache.maven.surefire.util.DefaultScanResult.loadClass(DefaultScanResult.java:131)
[ERROR] at org.apache.maven.surefire.util.DefaultScanResult.applyFilter(DefaultScanResult.java:95)
[ERROR] at org.apache.maven.surefire.junit4.JUnit4Provider.scanClassPath(JUnit4Provider.java:194)
[ERROR] at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:92)
[ERROR] at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:203)
[ERROR] at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:155)
[ERROR] at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:103)
[ERROR] -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/PluginExecutionException

I think some/project was compiled with Java 8... so I changed my current project to use Java 8 instead of 7 as well. I'm inclined to think this relates because of these questions:

Can't fix Unsupported major.minor version 52.0 even after fixing compatibility

Unsupported major.minor version 52.0

And now I'm completely stuck :(

like image 636
Ree Avatar asked Dec 12 '14 08:12

Ree


People also ask

How to rerun failed test cases in Maven?

During development, you may re-run failing tests because they are flaky. To use this feature through Maven surefire, set the rerunFailingTestsCount property to be a value larger than 0. Tests will be run until they pass or the number of reruns has been exhausted.

How do I force Java to Maven?

Running the mvn -v command will show the Java version in which Maven runs.

How do I run a Java mvn?

Maven uses the JAVA_HOME parameter to find which Java version it is supposed to run. I see from your comment that you can't change that in the configuration. You can set the JAVA_HOME parameter just before you start maven (and change it back afterwards if need be). You could also go into your mvn (non-windows)/ mvn.

What is Maven sure fire?

Maven sure fire plugin is used to follow the sequence of tests in testng. xml file. If we don't include the Mavwen surefire plugin then it will execute all the testcases under src/test/java which has prefix or suffix as 'test' and these tests will get executed without any sequence.


2 Answers

The JVM shall be explicitly set in the plugin configuration.

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.19.1</version>
    <configuration>
      <jvm>${env.JAVA_HOME}/bin/java</jvm>
    </configuration>
  </plugin>
like image 190
Seweryn Habdank-Wojewódzki Avatar answered Nov 08 '22 23:11

Seweryn Habdank-Wojewódzki


Make sure your version of Maven runtime >= JRE 1.8.

Run java -version in console to check the version, if mvn command is executed in console too. Alternatively to change maven JRE in your IDE, if maven is executed in IDE.

UnsupportedClassVersionError:

Thrown when the Java Virtual Machine attempts to read a class file and determines that the major and minor version numbers in the file are not supported.

like image 33
卢声远 Shengyuan Lu Avatar answered Nov 08 '22 21:11

卢声远 Shengyuan Lu