2017-08-08 76 views
0

當我運行org.eclipse.equinox.common 3.6和Java 6在Eclipse中使用霓虹燈的測試,我有時會收到以下錯誤:如何在Eclipse中使用org.eclipse.equinox.common的舊版本運行JUnit測試?

java.lang.UnsupportedClassVersionError: org/eclipse/core/runtime/IAdapterManager : Unsupported major.minor version 51.0 
    at java.lang.ClassLoader.defineClass1(Native Method) 
    at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631) 
    at java.lang.ClassLoader.defineClass(ClassLoader.java:615) 
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141) 
    ... 

看來,測試運行加載兩個不同版本的組織.eclipse.equinox.common。當我在調試視圖檢查過程中的特性,我看到(等等)的類路徑條目

C:\workspace\.metadata\.plugins\org.eclipse.pde.core\.bundle_pool\plugins\org.eclipse.equinox.common_3.8.0.v20160509-1230.jar 
C:\Users\me\.m2\repository\org\eclipse\org.eclipse.equinox.common\3.6.0.v20110523\org.eclipse.equinox.common-3.6.0.v20110523.jar 

版本3.8需要Java 7,所以我想迫使測試選擇3.6版本。我怎樣才能做到這一點?

回答

0

我找到了一個邪惡的類加載器的黑客是解決問題的基礎上,this answer

@BeforeClass 
public static void setUpClass() throws Exception { 
    removeJarFromClasspath("org.eclipse.equinox.common_3.8"); 
} 

public static void removeJarFromClasspath(String namePart) throws Exception { 
    URLClassLoader urlClassLoader = (URLClassLoader)ClassLoader.getSystemClassLoader(); 
    Field ucpField = URLClassLoader.class.getDeclaredField("ucp"); 
    ucpField.setAccessible(true); 
    URLClassPath ucp = (URLClassPath)ucpField.get(urlClassLoader); 

    Field pathField = URLClassPath.class.getDeclaredField("path"); 
    pathField.setAccessible(true); 
    List path = (List)pathField.get(ucp); 

    Field loadersField = URLClassPath.class.getDeclaredField("loaders"); 
    loadersField.setAccessible(true); 
    List loaders = (List)loadersField.get(ucp); 

    Field lmapField = URLClassPath.class.getDeclaredField("lmap"); 
    lmapField.setAccessible(true); 
    Map lmap = (Map)lmapField.get(ucp); 

    int index = 0; 
    for (Object entry : path) { 
     String url = entry.toString(); 
     if (url.contains(namePart)) { 
      path.remove(index); 
      loaders.remove(index); 
      lmap.remove(url.replaceFirst("file:/", "file:///")); 
      break; 
     } 
     index++; 
    } 
} 
相關問題