【问题标题】:How to make the runnable jar file for a maven eclipse project with hibernate configuration如何使用休眠配置为 Maven Eclipse 项目制作可运行的 jar 文件
【发布时间】:2015-09-21 16:26:54
【问题描述】:

我尝试使用休眠配置从我的 Eclipse Maven 项目中创建一个 jar 文件。

这是我项目的结构,我在 resources/ 和 src/main/resources/ 文件夹中都有文件 hibernate.cfg.xml。我通过从导出选项中选择可运行的 jar 文件来制作 jar 文件。 (感谢link的帮助)

这是我在运行可执行jar文件时遇到的错误:

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

【问题讨论】:

    标签: java eclipse hibernate maven jar


    【解决方案1】:

    TL;DR-解决方案

    将您的hibernate.cfg.xml 放入src/main/resource。然后不导出 您的项目通过 eclipse 的导出功能。而是使用 maven-assembly-plugin 创建可执行 jar。使用mvn package assembly:single 运行。

    eclipse 做了什么以及为什么它不工作

    如果您在 Eclipse 选项中选择“导出”,您编译的类(由 Eclipse 编译)将被放入该 jar。但是 Eclipse 对Maven Standard Directory Layout 一无所知。它只会将文件留在他们的文件夹中。另一方面,Maven 会将所有资源从src/main/resources-文件夹复制到 jar 文件的根目录。

    如何阅读您看到的错误

    第一个错误:

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

    这意味着,log4j 没有找到 log4j.properties 或 .xml。它未配置。这是第一个提示,您的构建不成功。

    第二个错误:

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

    第二个提示,您的资源不在库所期望的位置(在本例中为休眠)。

    【讨论】:

      猜你喜欢
      • 2017-04-30
      • 2013-05-07
      • 2011-04-12
      • 2013-10-07
      • 1970-01-01
      • 2012-10-01
      • 1970-01-01
      • 2014-07-27
      • 1970-01-01
      相关资源
      最近更新 更多