2013-04-20 73 views
0

我正在這裏的例子:JPA測試類UnsupportedClassVersionError

https://schuchert.wikispaces.com/JPA+Tutorial+1+-+Getting+Started

的例子包含一個測試類:

package entity; 

import javax.persistence.EntityManager; 
import javax.persistence.EntityManagerFactory; 
import javax.persistence.Persistence; 

import org.apache.log4j.BasicConfigurator; 
import org.junit.After; 
import org.junit.Before; 
import org.junit.Test; 

public class PersistanceTest { 
    private EntityManagerFactory emf; 

    private EntityManager em; 

    @Before 
    public void initEmfAndEm() { 
     BasicConfigurator.configure(); 

     emf = Persistence.createEntityManagerFactory("examplePersistenceUnit"); 
     em = emf.createEntityManager(); 
    } 

    @After 
    public void cleanup() { 
     em.close(); 
    } 

    @Test 
    public void emptyTest() { 
    } 
} 

我在這條線得到一個錯誤:

emf = Persistence.createEntityManagerFactory(「examplePersistenceUnit」);

的錯誤是:

java.lang.UnsupportedClassVersionError: Bad version number in .class file 
    at java.lang.ClassLoader.defineClass1(Native Method) 
    at java.lang.ClassLoader.defineClass(ClassLoader.java:620) 
    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:306) 
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:268) 
    at java.lang.ClassLoader.loadClass(ClassLoader.java:251) 
    at javax.persistence.spi.PersistenceProviderResolverHolder$PersistenceProviderResolverPerClassLoader$CachingPersistenceProviderResolver.loadResolverClasses(PersistenceProviderResolverHolder.java:150) 
    at javax.persistence.spi.PersistenceProviderResolverHolder$PersistenceProviderResolverPerClassLoader$CachingPersistenceProviderResolver.<init>(PersistenceProviderResolverHolder.java:130) 
    at javax.persistence.spi.PersistenceProviderResolverHolder$PersistenceProviderResolverPerClassLoader.getPersistenceProviders(PersistenceProviderResolverHolder.java:88) 
    at javax.persistence.Persistence.getProviders(Persistence.java:75) 
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:61) 
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:47) 
    at entity.PersistanceTest.initEmfAndEm(PersistanceTest.java:24) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 
    at java.lang.reflect.Method.invoke(Method.java:585) 
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45) 
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) 
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42) 
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:27) 
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:30) 
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263) 
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68) 
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47) 
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231) 
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60) 
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229) 
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50) 
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222) 
    at org.junit.runners.ParentRunner.run(ParentRunner.java:300) 
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) 
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197) 

我怎樣才能解決這個問題?

+3

看來你正在執行一個Java版本的測試,該版本比用於編譯類的版本(您的或您正在使用的庫中的一個)要舊。 – 2013-04-20 16:33:11

+0

確實,指定的java版本不是正確的。 – user1802439 2013-04-20 19:01:10

回答

0

升級到最新的JDK,我認爲這個問題將得到解決。