How to make an executable jar for maven eclipse project with hibernate config

I tried to create a jar file from my eclipse maven project with hibernate configurations.

This is my project structure, I had a hibernate.cfg.xml file in both resources / and src / main / resources / folder. And I made a jar file by selecting the runnable jar file from export options. (because of help)

This is the error I get when running the jar executable:

ehsanik$ java -jar oh_oh.jar 
log4j:WARN No appenders could be found for logger (org.jboss.logging).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Exception in thread "main" java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)
Caused by: java.lang.ExceptionInInitializerError
    at com.jvmhub.tutorial.App.StartDB(App.java:25)
    at CopyOfMain.main(CopyOfMain.java:7)
    ... 5 more
Caused by: org.hibernate.HibernateException: /hibernate.cfg.xml not found
    at org.hibernate.internal.util.ConfigHelper.getResourceAsStream(ConfigHelper.java:173)
    at org.hibernate.cfg.Configuration.getConfigurationInputStream(Configuration.java:1940)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:1921)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:1901)
    at com.jvmhub.tutorial.App.StartDB(App.java:19)
    ... 6 more

      

+3


source to share


1 answer


TL; DR-Solution

Place your hibernate.cfg.xml

in src/main/resource

. Then don't export your project with eclipse's export function. Rather use the maven-assembly-plugin to create an executable jar. execute mvn package assembly:single

.

What does eclipse do and why it doesn't work

If you choose "export" in eclipse options, your compiled classes (compiled by eclipse) will be put in this jar. But Eclipse doesn't know anything about Maven Standard Directory Layout . He will just leave the files in his folders. Maven, on the other hand, will copy all resources from the src/main/resources

-directory to the root directory of the jar file.

How to read the errors you see

First error:



log4j:WARN No appenders could be found for logger (org.jboss.logging).
log4j:WARN Please initialize the log4j system properly.

      

this means log4j didn't find log4j.properties or .xml. It is not configured. This is the first hint that your build was unacceptable.

Second error:

Caused by: org.hibernate.HibernateException: /hibernate.cfg.xml not found

      

The second hint is that your resources are not in the place the libraries intended them to be (hibernation in this case).

0


source







All Articles