Unable to configure EntityManagerFactory from runnable jar

早过忘川 提交于 2019-12-24 00:08:33

问题


I'm developing a JPA-2.0 application using Hibernate3. It runs fine from eclipse, but if run it from a generated runnable jar, it gives a javax.persistence.PersistenceException: Unable to configure EntityManagerFactory:

Exception in thread "main" java.lang.reflect.InvocationTargetException
    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:597)
    at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:56)
Caused by: javax.persistence.PersistenceException: Unable to configure EntityManagerFactory
    at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:374)
    at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:56)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:48)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:32)
Caused by: java.lang.StringIndexOutOfBoundsException: String index out of range: -1
    at java.lang.String.substring(String.java:1937)
    at org.hibernate.ejb.packaging.JarVisitorFactory.getJarURLFromURLEntry(JarVisitorFactory.java:55)
    at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:340)
    ... 12 more

If I unpack this same jar, and run the class directly, it works though:

java -cp .:<add list to all .jar files> MainClass

I cannot figure out what's wrong.


回答1:


It'll work if you choose the "Extract required libraries into generated JAR" while generating the runnable JAR.




回答2:


One thing i would check first is that the persistance.xml is in META-INF directory of the jar you are building.



来源:https://stackoverflow.com/questions/5143353/unable-to-configure-entitymanagerfactory-from-runnable-jar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!