0

I am trying to use JMockit in a JUnit test, but I am getting an UnsupportedClassVersionError while running the tests from Eclipse. This is a Java 1.5 project and I have JDK 1.5.0.22 in build path and the version of JUnit in class path is 4.11 (downloaded from maven repository)

java.lang.UnsupportedClassVersionError: Bad version number in .class file
   at java.lang.ClassLoader.defineClass1(Native Method)
   at java.lang.ClassLoader.defineClass(ClassLoader.java:621)
   at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)
   at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)
   at java.net.URLClassLoader.access$100(URLClassLoader.java:56)
   at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
   at java.security.AccessController.doPrivileged(Native Method)
   at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
   at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
   at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:268)
   at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
   at sun.instrument.InstrumentationImpl.loadClassAndCallPremain(InstrumentationImpl.java:133)

I went through the JMockit installation instructions and made the following changes;

  1. Have jmockit.jar before Junit.jar in the classpath
  2. Pass the jmockit.jar as an initialization parameter to the JVM
  3. Project uses the JRE from a JDK installation instead of a "plain" JRE

Eclipse Project specific Java Compiler Settings

Run config for Project

I have spent lot of hours trying to fix this :(, but no luck. Please let me know if you have an solution for this? What am I missing here?

Karthik
  • 137
  • 8

1 Answers1

2

You haven't specified what version of JMockit you are using; however it seems to me like you're running into this:

From this version on, JMockit requires Java 6 or newer when running tests;
JRE 1.5 is no longer supported. Note that this only applies to the JRE/JDK
used for test  execution; class files compiled  for older versions of Java
are still supported.

That's as of JMockit 1.8, released last April.

This, BTW, is to be expected, since Java 1.5 hasn't been publicly supported in over five years.

dcsohl
  • 7,186
  • 1
  • 26
  • 44
  • Thanks @dcsohl . Your answer helped me solve this issue. I was using Jmockit 1.14, so I just replaced JDK 5 on the build path with JRE 6 and now the JUnit along with JMockit works great. A big relief after a day struggle. Looks like I headed in the wrong directions initially looking at all those articles which specified the workarounds to make Jmockit work with Java 1.5- but it never did. Was it specified anywhere in JMockit web page / readme / installation doc that its not supported for Java 1.5 - Did I miss it ? – Karthik Jan 07 '15 at 19:20
  • 1
    It wasn't highlighted; I had to go digging for it. It's mentioned on the changes page only, which I linked to above ("as of JMockit 1.8"). – dcsohl Jan 07 '15 at 19:55