2015-01-07 43 views
0

我想在JUnit測試中使用JMockit,但在從Eclipse運行測試時出現UnsupportedClassVersionError。這是一個Java 1.5的項目,我有JDK 1.5.0.22在構建路徑和JUnit的類路徑中的版本是4.11(從Maven倉庫下載)在Java 1.5項目中使用Jmockit時UnsupportedClassVersionError

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) 

我通過JMockit安裝說明去了,做了如下變化;

  1. 有jmockit.jar之前的junit.jar在classpath
  2. 傳遞jmockit.jar作爲初始化參數在JVM
  3. 項目從JDK安裝,而不是一個「普通」 JRE使用JRE

Eclipse Project specific Java Compiler Settings

Run config for Project

我花了很多時間試圖解決這個:(但沒有運氣。請讓我知道你是否有解決方案?我在這裏錯過了什麼?

回答

2

您尚未指定您正在使用的JMockit版本;然而在我看來,像你運行到這一點:

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. 

這是as of JMockit 1.8,去年4月發佈。

由於Java 1.5在五年內還沒有得到公衆的支持,所以預計會出現這種情況。

+0

謝謝@dcsohl。你的回答幫助我解決了這個問題。我使用的是Jmockit 1.14,所以我只是用JRE 6替換了JDK 5的構建路徑,現在JUnit和JMockit一起工作的很好。經過一天的鬥爭之後,這是一大福音看起來我在錯誤的方向上開始尋找所有那些指定了解決方法的文章,以使Jmockit能夠在Java 1.5中工作 - 但它從來沒有這樣做過。是否在JMockit網頁/自述文件/安裝文檔中的任何地方指定了它不支持Java 1.5的版本 - 我錯過了它嗎? – Karthik

+1

它沒有被強調;我不得不去挖掘它。它僅在更改頁面上提到,我將其鏈接到上面(「從JMockit 1.8開始」)。 – dcsohl

相關問題