2016-12-12 63 views
1

當我在Intellij中運行我的程序時,它工作正常。 Maven將其打包完美令人高興的是,它還將推出精,但只要我嘗試從休眠數據庫中檢索任何東西,我得到以下錯誤:打包時Hibernate無法加載類,但可以在IDE

Exception in thread "AWT-EventQueue-0" java.lang.ExceptionInInitializerError 
     at Program.GUI.GUITools.SavableSelector.initialGetElements(SavableSelector.java:112) 
     at Program.GUI.GUITools.SelectorPanel.initialise(SelectorPanel.java:57) 
     at Program.GUI.GUITools.SavableSelector.initialise(SavableSelector.java:32) 
     at Program.GUI.GUITools.SavableSelector.<init>(SavableSelector.java:21) 
     at Program.GUI.GUITools.SavableSelector.getSelectionFromUser(SavableSelector.java:26) 
     at Program.GUI.TopFrame$1.actionPerformed(TopFrame.java:33) 
     at javax.swing.AbstractButton.fireActionPerformed(Unknown Source) 
     at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source) 
     at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source) 
     at javax.swing.DefaultButtonModel.setPressed(Unknown Source) 
     at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source) 
     at java.awt.AWTEventMulticaster.mouseReleased(Unknown Source) 
     at java.awt.Component.processMouseEvent(Unknown Source) 
     at javax.swing.JComponent.processMouseEvent(Unknown Source) 
     at java.awt.Component.processEvent(Unknown Source) 
     at java.awt.Container.processEvent(Unknown Source) 
     at java.awt.Component.dispatchEventImpl(Unknown Source) 
     at java.awt.Container.dispatchEventImpl(Unknown Source) 
     at java.awt.Component.dispatchEvent(Unknown Source) 
     at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source) 
     at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source) 
     at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source) 
     at java.awt.Container.dispatchEventImpl(Unknown Source) 
     at java.awt.Window.dispatchEventImpl(Unknown Source) 
     at java.awt.Component.dispatchEvent(Unknown Source) 
     at java.awt.EventQueue.dispatchEventImpl(Unknown Source) 
     at java.awt.EventQueue.access$500(Unknown Source) 
     at java.awt.EventQueue$3.run(Unknown Source) 
     at java.awt.EventQueue$3.run(Unknown Source) 
     at java.security.AccessController.doPrivileged(Native Method) 
     at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source) 
     at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source) 
     at java.awt.EventQueue$4.run(Unknown Source) 
     at java.awt.EventQueue$4.run(Unknown Source) 
     at java.security.AccessController.doPrivileged(Native Method) 
     at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source) 
     at java.awt.EventQueue.dispatchEvent(Unknown Source) 
     at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) 
     at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) 
     at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) 
     at java.awt.EventDispatchThread.pumpEvents(Unknown Source) 
     at java.awt.EventDispatchThread.pumpEvents(Unknown Source) 
     at java.awt.EventDispatchThread.run(Unknown Source) 
Caused by: org.hibernate.boot.registry.classloading.spi.ClassLoadingException: Unable to load class [org.apache.logging.log4j.core.appender.db.jpa.converter.ContextDataJsonAttributeConverter] 
     at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.classForName(ClassLoaderServiceImpl.java:351) 
     at org.hibernate.boot.model.process.internal.ScanningCoordinator.applyScanResultsToManagedResources(ScanningCoordinator.java:235) 
     at org.hibernate.boot.model.process.internal.ScanningCoordinator.coordinateScan(ScanningCoordinator.java:81) 
     at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.prepare(MetadataBuildingProcess.java:98) 
     at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.<init>(EntityManagerFactoryBuilderImpl.java:228) 
     at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.<init>(EntityManagerFactoryBuilderImpl.java:170) 
     at org.hibernate.jpa.boot.spi.Bootstrap.getEntityManagerFactoryBuilder(Bootstrap.java:76) 
     at org.hibernate.jpa.HibernatePersistenceProvider.getEntityManagerFactoryBuilder(HibernatePersistenceProvider.java:181) 
     at org.hibernate.jpa.HibernatePersistenceProvider.getEntityManagerFactoryBuilderOrNull(HibernatePersistenceProvider.java:129) 
     at org.hibernate.jpa.HibernatePersistenceProvider.getEntityManagerFactoryBuilderOrNull(HibernatePersistenceProvider.java:71) 
     at org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(HibernatePersistenceProvider.java:52) 
     at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:55) 
     at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:39) 
     at Program.Database.Controller.<clinit>(Controller.java:15) 
     ... 43 more 
Caused by: java.lang.NoClassDefFoundError: com/fasterxml/jackson/databind/ObjectMapper 
     at org.apache.logging.log4j.core.appender.db.jpa.converter.ContextDataJsonAttributeConverter.<clinit>(ContextDataJsonAttributeConverter.java:47) 
     at java.lang.Class.forName0(Native Method) 
     at java.lang.Class.forName(Unknown Source) 
     at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.classForName(ClassLoaderServiceImpl.java:345) 
     ... 56 more 
Caused by: java.lang.ClassNotFoundException: com.fasterxml.jackson.databind.ObjectMapper 
     at java.net.URLClassLoader.findClass(Unknown Source) 
     at java.lang.ClassLoader.loadClass(Unknown Source) 
     at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) 
     at java.lang.ClassLoader.loadClass(Unknown Source) 
     ... 60 more 

我的問題似乎幾乎相同this question here,但這個問題沒有答案,這實際上是我在搜索錯誤時出現的一件事。我和他在這個帖子上提出的問題有同樣的答案。

這部分Caused by: org.hibernate.boot.registry.classloading.spi.ClassLoadingException: Unable to load class [org.apache.logging.log4j.core.appender.db.jpa.converter.ContextDataJsonAttributeConverter]

好像相關的一部分 - 程序會運行,如果我從我的POM文件中刪除log4j的依賴Hibernate不抱怨 - 雖然記錄不會明顯抱怨。

我已經包括了我的POM文件:

<build> 
    <plugins> 
     <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-compiler-plugin</artifactId> 
      <version>3.6.0</version> 
      <configuration> 
       <source>1.8</source> 
       <target>1.8</target> 
      </configuration> 
     </plugin> 
     <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-jar-plugin</artifactId> 
      <version>3.0.2</version> 
      <configuration> 
       <archive> 
        <manifest> 
         <addClasspath>true</addClasspath> 
         <classpathPrefix>lib/</classpathPrefix> 
         <mainClass>Program.Main</mainClass> 
        </manifest> 
       </archive> 
      </configuration> 
     </plugin> 
     <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-shade-plugin</artifactId> 
      <version>2.4.2</version> 
      <executions> 
       <execution> 
        <phase>package</phase> 
        <goals> 
         <goal>shade</goal> 
        </goals> 
        <configuration> 
         <filters> 
          <filter> 
           <artifact>*:*</artifact> 
           <excludes> 
            <exclude>META-INF/*.SF</exclude> 
            <exclude>META-INF/*.DSA</exclude> 
            <exclude>META-INF/*.RSA</exclude> 
           </excludes> 
          </filter> 
         </filters> 
        </configuration> 
       </execution> 
      </executions> 
     </plugin> 
    </plugins> 
</build> 

<dependencies> 
    <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core --> 
    <dependency> 
     <groupId>org.hibernate</groupId> 
     <artifactId>hibernate-core</artifactId> 
     <version>5.2.5.Final</version> 
    </dependency> 
    <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-annotations --> 
    <dependency> 
     <groupId>org.hibernate</groupId> 
     <artifactId>hibernate-annotations</artifactId> 
     <version>3.5.6-Final</version> 
    </dependency> 
    <!-- https://mvnrepository.com/artifact/com.h2database/h2 --> 
    <dependency> 
     <groupId>com.h2database</groupId> 
     <artifactId>h2</artifactId> 
     <version>1.4.193</version> 
    </dependency> 
    <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api --> 
    <dependency> 
     <groupId>org.slf4j</groupId> 
     <artifactId>slf4j-api</artifactId> 
     <version>1.7.21</version> 
    </dependency> 
    <dependency> 
     <groupId>org.apache.logging.log4j</groupId> 
     <artifactId>log4j-api</artifactId> 
     <version>2.7</version> 
    </dependency> 
    <dependency> 
     <groupId>org.apache.logging.log4j</groupId> 
     <artifactId>log4j-core</artifactId> 
     <version>2.7</version> 
    </dependency> 
    <dependency> 
     <groupId>org.slf4j</groupId> 
     <artifactId>slf4j-log4j12</artifactId> 
     <version>1.7.21</version> 
    </dependency> 
    <!-- https://mvnrepository.com/artifact/com.intellij/annotations --> 
    <dependency> 
     <groupId>com.intellij</groupId> 
     <artifactId>annotations</artifactId> 
     <version>12.0</version> 
    </dependency> 
    <!-- https://mvnrepository.com/artifact/net.sourceforge.jdatepicker/jdatepicker --> 
    <dependency> 
     <groupId>net.sourceforge.jdatepicker</groupId> 
     <artifactId>jdatepicker</artifactId> 
     <version>1.3.2</version> 
    </dependency> 
    <!-- https://mvnrepository.com/artifact/com.miglayout/miglayout --> 
    <dependency> 
     <groupId>com.miglayout</groupId> 
     <artifactId>miglayout</artifactId> 
     <version>3.7.4</version> 
    </dependency> 
    <!-- https://mvnrepository.com/artifact/org.jfree/jfreechart --> 
    <dependency> 
     <groupId>org.jfree</groupId> 
     <artifactId>jfreechart</artifactId> 
     <version>1.0.19</version> 
    </dependency> 
    <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-csv --> 
    <dependency> 
     <groupId>org.apache.commons</groupId> 
     <artifactId>commons-csv</artifactId> 
     <version>1.4</version> 
    </dependency> 
</dependencies> 

需要明確的是,我刪除以下依存關係到它的工作:

<dependency> 
     <groupId>org.apache.logging.log4j</groupId> 
     <artifactId>log4j-api</artifactId> 
     <version>2.7</version> 
    </dependency> 
    <dependency> 
     <groupId>org.apache.logging.log4j</groupId> 
     <artifactId>log4j-core</artifactId> 
     <version>2.7</version> 
    </dependency> 

希望你能幫助 - 我在完全喪失。

回答

1

未找到的類是jackson ObjectMapper。在POM爲log4j-core(V 2.7),這可以發現:

<!-- Required for JSON support --> 
<dependency> 
    <groupId>com.fasterxml.jackson.core</groupId> 
    <artifactId>jackson-core</artifactId> 
    <optional>true</optional> 
</dependency> 
<!-- Required for JSON support --> 
<dependency> 
    <groupId>com.fasterxml.jackson.core</groupId> 
    <artifactId>jackson-databind</artifactId> 
    <optional>true</optional> 
</dependency> 

可選的標誌意味着這些不包括作爲傳遞依賴。將它們添加到你的pom(可選標誌不需要),它應該工作。

至於爲什麼它工作在IDEA,它可能是依賴包含在其他模塊(IDEA項目有1級扁平的classpath,內置的文件各有一個)。或者,一些配置是不同的,這樣的JSON支持從IDEA運行時不需要。

+0

謝謝你,那整理它!我非常感激 –

相關問題