2017-04-16 131 views
0

我在使用maven爲我的程序創建可執行文件時遇到問題。NoClassDefFoundError:執行maven時的/ org/Hibernate/SessionFactory .war

目前的問題是,當我在Eclipse內部運行程序時,一切正常,但是一旦建立了.war,它就不起作用 - 它會拋出一個NoClassDefFoundError:/ org/Hibernate/SessionFactory。

由於這是主要方法調用的第一個類,我懷疑它不是hibernate的問題,而是戰爭的類路徑,但我不明白爲什麼。這是我的pom.xml:

<?xml version="1.0" encoding="UTF-8"?> 
 
<!-- 
 
    Licensed to the Apache Software Foundation (ASF) under one or more 
 
    contributor license agreements. See the NOTICE file distributed with 
 
    this work for additional information regarding copyright ownership. 
 
    The ASF licenses this file to You under the Apache License, Version 2.0 
 
    (the "License"); you may not use this file except in compliance with 
 
    the License. You may obtain a copy of the License at 
 

 
     http://www.apache.org/licenses/LICENSE-2.0 
 

 
    Unless required by applicable law or agreed to in writing, software 
 
    distributed under the License is distributed on an "AS IS" BASIS, 
 
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 
    See the License for the specific language governing permissions and 
 
    limitations under the License. 
 
--> 
 
<project xmlns="http://maven.apache.org/POM/4.0.0" 
 
\t xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 
\t xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 
 

 
\t <modelVersion>4.0.0</modelVersion> 
 
\t <groupId>pipe</groupId> 
 
\t <artifactId>pipe</artifactId> 
 
\t <packaging>war</packaging> 
 
\t <version>1.0</version> 
 
\t \t <!-- TODO project name --> 
 
\t <name>pipe</name> 
 
\t <description></description> 
 
\t 
 
\t <!-- 
 
\t \t <organization> 
 
\t \t \t <name>company name</name> 
 
\t \t \t <url>company url</url> 
 
\t \t </organization> 
 
\t --> 
 
\t <licenses> 
 
\t \t <license> 
 
\t \t \t <name>The Apache Software License, Version 2.0</name> 
 
\t \t \t <url>http://www.apache.org/licenses/LICENSE-2.0.txt</url> 
 
\t \t \t <distribution>repo</distribution> 
 
\t \t </license> 
 
\t </licenses> 
 
\t <properties> 
 
\t \t <wicket.version>7.4.0</wicket.version> 
 
\t \t <jetty9.version>9.2.13.v20150730</jetty9.version> 
 
\t \t <log4j.version>2.5</log4j.version> 
 
\t \t <junit.version>4.12</junit.version> 
 
\t \t <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
 
\t \t <!-- allowed values: R7, 1.0, 1.5, 2.0 or none --> 
 
\t \t <wtp.version>none</wtp.version> 
 
\t </properties> 
 
\t <dependencies> 
 
\t \t <!-- WICKET DEPENDENCIES --> 
 
\t \t <dependency> 
 
\t \t \t <groupId>org.apache.wicket</groupId> 
 
\t \t \t <artifactId>wicket-core</artifactId> 
 
\t \t \t <version>${wicket.version}</version> 
 
\t \t </dependency> 
 
\t \t 
 
\t \t <!-- OPTIONAL DEPENDENCY 
 
\t \t <dependency> 
 
\t \t \t <groupId>org.apache.wicket</groupId> 
 
\t \t \t <artifactId>wicket-extensions</artifactId> 
 
\t \t \t <version>${wicket.version}</version> 
 
\t \t </dependency> 
 
\t \t --> 
 
<dependency> 
 
    <groupId>org.wicketstuff</groupId> 
 
    <artifactId>wicketstuff-editable-grid</artifactId> 
 
    <version>7.6.0</version> 
 
</dependency> 
 
<dependency> 
 
    <groupId>org.wicketstuff</groupId> 
 
    <artifactId>wicketstuff-inmethod-grid</artifactId> 
 
    <version>7.6.0</version> 
 
</dependency> 
 
<dependency> 
 
    <groupId>org.hibernate</groupId> 
 
    <artifactId>hibernate-core</artifactId> 
 
    <version>5.2.10.Final</version> 
 
</dependency> 
 
<!-- https://mvnrepository.com/artifact/org.apache.wicket/wicket-datetime --> 
 
<dependency> 
 
    <groupId>org.apache.wicket</groupId> 
 
    <artifactId>wicket-datetime</artifactId> 
 
    <version>7.6.0</version> 
 
</dependency> 
 

 
    <!-- https://mvnrepository.com/artifact/org.apache.wicket/wicket-auth-roles --> 
 
<dependency> 
 
    <groupId>org.apache.wicket</groupId> 
 
    <artifactId>wicket-auth-roles</artifactId> 
 
    <version>7.6.0</version> 
 
</dependency> 
 
    <!-- https://mvnrepository.com/artifact/commons-codec/commons-codec --> 
 
<dependency> 
 
    <groupId>commons-codec</groupId> 
 
    <artifactId>commons-codec</artifactId> 
 
    <version>1.10</version> 
 
</dependency> 
 
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> 
 
<dependency> 
 
    <groupId>mysql</groupId> 
 
    <artifactId>mysql-connector-java</artifactId> 
 
    <version>6.0.5</version> 
 
</dependency> 
 
<!-- https://mvnrepository.com/artifact/org.wicketstuff/wicketstuff-minis --> 
 
<dependency> 
 
    <groupId>org.wicketstuff</groupId> 
 
    <artifactId>wicketstuff-minis</artifactId> 
 
    <version>7.6.0</version> 
 
</dependency> 
 

 
<dependency> 
 
    <groupId>org.codehaus.izpack</groupId> 
 
    <artifactId>izpack-maven-plugin</artifactId> 
 
    <version>5.1.0</version> 
 
</dependency> 
 

 
<dependency> 
 
    <groupId>org.wicketstuff</groupId> 
 
    <artifactId>inmethod-grid</artifactId> 
 
    <version>1.4.14</version> 
 
</dependency> 
 

 
\t \t <!-- LOGGING DEPENDENCIES - LOG4J --> 
 
\t \t <dependency> 
 
\t \t \t <groupId>org.apache.logging.log4j</groupId> 
 
\t \t \t <artifactId>log4j-slf4j-impl</artifactId> 
 
\t \t \t <version>${log4j.version}</version> 
 
\t \t </dependency> 
 
\t \t <dependency> 
 
\t \t \t <groupId>org.apache.logging.log4j</groupId> 
 
\t \t \t <artifactId>log4j-core</artifactId> 
 
\t \t \t <version>${log4j.version}</version> 
 
\t \t </dependency> 
 
\t \t 
 

 
\t \t <!-- JUNIT DEPENDENCY FOR TESTING --> 
 
\t \t <dependency> 
 
\t \t \t <groupId>junit</groupId> 
 
\t \t \t <artifactId>junit</artifactId> 
 
\t \t \t <version>${junit.version}</version> 
 
\t \t \t <scope>test</scope> 
 
\t \t </dependency> 
 

 
\t \t <!-- JETTY DEPENDENCIES FOR TESTING --> 
 
\t \t <dependency> 
 
\t \t \t <groupId>org.eclipse.jetty.aggregate</groupId> 
 
\t \t \t <artifactId>jetty-all</artifactId> 
 
\t \t \t <version>${jetty9.version}</version> 
 
\t \t \t <scope>test</scope> 
 
\t \t </dependency> 
 
\t </dependencies> 
 
\t <build> 
 
\t \t <resources> 
 
\t \t \t <resource> 
 
\t \t \t \t <filtering>false</filtering> 
 
\t \t \t \t <directory>src/main/resources</directory> 
 
\t \t \t </resource> 
 
\t \t \t <resource> 
 
\t \t \t \t <filtering>false</filtering> 
 
\t \t \t \t <directory>src/main/java</directory> 
 
\t \t \t \t <includes> 
 
\t \t \t \t \t <include>**</include> 
 
\t \t \t \t </includes> 
 
\t \t \t \t <excludes> 
 
\t \t \t \t \t <exclude>**/*.java</exclude> 
 
\t \t \t \t </excludes> 
 
\t \t \t </resource> 
 
\t \t </resources> 
 
\t \t <testResources> 
 
\t \t \t <testResource> 
 
\t \t \t \t <filtering>false</filtering> 
 
\t \t \t \t <directory>src/test/resources</directory> 
 
\t \t \t </testResource> 
 
\t \t \t <testResource> 
 
\t \t \t \t <filtering>false</filtering> 
 
\t \t \t \t <directory>src/test/java</directory> 
 
\t \t \t \t <includes> 
 
\t \t \t \t \t <include>**</include> 
 
\t \t \t \t </includes> 
 
\t \t \t \t <excludes> 
 
\t \t \t \t \t <exclude>**/*.java</exclude> 
 
\t \t \t \t </excludes> 
 
\t \t \t </testResource> 
 
\t \t </testResources> 
 
\t \t <plugins> 
 
\t \t \t <plugin> 
 
\t \t \t \t <inherited>true</inherited> 
 
\t \t \t \t <groupId>org.apache.maven.plugins</groupId> 
 
\t \t \t \t <artifactId>maven-compiler-plugin</artifactId> 
 
\t \t \t \t <version>3.1</version> 
 
\t \t \t \t <configuration> 
 
\t \t \t \t \t <source>1.7</source> 
 
\t \t \t \t \t <target>1.7</target> 
 
\t \t \t \t \t <encoding>UTF-8</encoding> 
 
\t \t \t \t \t <showWarnings>true</showWarnings> 
 
\t \t \t \t \t <showDeprecation>true</showDeprecation> 
 
\t \t \t \t </configuration> 
 
\t \t \t </plugin> 
 
\t \t \t <plugin> 
 
\t \t \t \t <groupId>org.eclipse.jetty</groupId> 
 
\t \t \t \t <artifactId>jetty-maven-plugin</artifactId> 
 
\t \t \t \t <version>${jetty9.version}</version> 
 
\t \t \t \t <configuration> 
 
\t \t \t \t \t <systemProperties> 
 
\t \t \t \t \t \t <systemProperty> 
 
\t \t \t \t \t \t \t <name>maven.project.build.directory.test-classes</name> 
 
\t \t \t \t \t \t \t <value>${project.build.directory}/test-classes</value> 
 
\t \t \t \t \t \t </systemProperty> 
 
\t \t \t \t \t </systemProperties> 
 
\t \t \t \t \t <jettyXml>${project.basedir}/src/test/jetty/jetty.xml,${project.basedir}/src/test/jetty/jetty-ssl.xml,${project.basedir}/src/test/jetty/jetty-http.xml,${project.basedir}/src/test/jetty/jetty-https.xml</jettyXml> 
 
\t \t \t \t </configuration> 
 
\t \t \t </plugin> 
 
\t \t \t <plugin> 
 
\t \t \t \t <groupId>org.apache.maven.plugins</groupId> 
 
\t \t \t \t <artifactId>maven-eclipse-plugin</artifactId> 
 
\t \t \t \t <version>2.9</version> 
 
\t \t \t \t <configuration> 
 
\t \t \t \t \t <downloadSources>true</downloadSources> 
 
\t \t \t \t \t <wtpversion>${wtp.version}</wtpversion> 
 
\t \t \t \t </configuration> 
 
\t \t \t </plugin> 
 
\t \t \t <plugin> 
 
\t \t \t \t <artifactId>maven-war-plugin</artifactId> 
 
\t \t \t \t <version>2.4</version> 
 
\t \t \t \t <configuration> 
 
\t \t \t \t \t <warSourceDirectory>WebContent</warSourceDirectory> 
 
\t \t \t \t \t <failOnMissingWebXml>false</failOnMissingWebXml> 
 
\t \t \t \t \t <archiveClasses>true</archiveClasses> 
 
\t \t \t \t \t <archive> 
 
      <manifest> 
 
      <mainClass>Main</mainClass> 
 
      </manifest> 
 
     </archive> 
 
\t \t \t \t </configuration> 
 
\t \t \t </plugin> 
 
\t \t \t <plugin> 
 
       <artifactId>maven-antrun-plugin</artifactId> 
 
       <executions> 
 
        <execution> 
 
         <id>main-class-placement</id> 
 
         <phase>prepare-package</phase> 
 
         <configuration> 
 
          <tasks> 
 
           <move todir="${project.build.directory}/${project.artifactId}-${project.version}/"> 
 
            <fileset dir="${project.build.directory}/classes/"> 
 
             <include name="Main.class" /> 
 
            </fileset> 
 
           </move> 
 
          </tasks> 
 
         </configuration> 
 
         <goals> 
 
          <goal>run</goal> 
 
         </goals> 
 
        </execution> 
 
       </executions> 
 
      </plugin> 
 
      <plugin> 
 
       <groupId>org.apache.maven.plugins</groupId> 
 
       <artifactId>maven-dependency-plugin</artifactId> 
 
       <version>2.3</version> 
 
       <executions> 
 
        <execution> 
 
         <id>jetty-classpath</id> 
 
         <phase>prepare-package</phase> 
 
         <goals> 
 
          <goal>unpack-dependencies</goal> 
 
         </goals> 
 
         <configuration> 
 
          <includeGroupIds>org.eclipse.jetty,javax.servlet</includeGroupIds> 
 
          <excludeArtifactIds>jsp-api,jstl</excludeArtifactIds> 
 
          <excludes>META-INF/ECLIPSEF.*</excludes> 
 
          <outputDirectory> 
 
           ${project.build.directory}/${project.artifactId}-${project.version} 
 
          </outputDirectory> 
 
         </configuration> 
 
        </execution> 
 
       </executions> 
 
      </plugin> 
 
\t \t </plugins> 
 
\t </build> 
 

 
\t <repositories> 
 
\t \t <repository> 
 
\t \t \t <id>Apache Nexus</id> 
 
\t \t \t <url>https://repository.apache.org/content/repositories/snapshots/</url> 
 
\t \t \t <releases> 
 
\t \t \t \t <enabled>false</enabled> 
 
\t \t \t </releases> 
 
\t \t \t <snapshots> 
 
\t \t \t \t <enabled>true</enabled> 
 
\t \t \t </snapshots> 
 
\t \t </repository> 
 
\t </repositories> 
 
\t </project>

所有的依賴也是在Eclipse構建類路徑。 我試圖用命令java -jar pipe-1.0.war來運行它。

任何幫助表示讚賞

CNC中

確切的錯誤:

Error: A JNI error has occurred, please check your installation and try again 
Exception in thread "main" java.lang.NoClassDefFoundError: org/hibernate/SessionFactory 
     at java.lang.Class.getDeclaredMethods0(Native Method) 
     at java.lang.Class.privateGetDeclaredMethods(Unknown Source) 
     at java.lang.Class.privateGetMethodRecursive(Unknown Source) 
     at java.lang.Class.getMethod0(Unknown Source) 
     at java.lang.Class.getMethod(Unknown Source) 
     at sun.launcher.LauncherHelper.validateMainClass(Unknown Source) 
     at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source) 
Caused by: java.lang.ClassNotFoundException: org.hibernate.SessionFactory 
     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) 
     ... 7 more 

回答

0

,如果你反編譯的jar和檢查清單文件,你可能會看到與主方法的類沒有正確申報。

所以,你可以嘗試加入這個插件到您的POM XML文件:

<plugin> 
      <artifactId>maven-assembly-plugin</artifactId> 
      <configuration> 
       <descriptorRefs> 
        <descriptorRef>jar-with-dependencies</descriptorRef> 
       </descriptorRefs> 
       <archive> 
        <manifest> 
         <addClasspath>true</addClasspath> 
         <mainClass>yourpackage.Mainclass</mainClass> 
        </manifest> 
       </archive> 
      </configuration> 
     </plugin> 
+0

感謝爲答案,但主類定義是正確的 - 它在「默認包」下,它被稱爲Main。它在運行時發現。 – Sauron

-1

的Hibernate 5.2和更高版本至少需要1.8的Java,而你正在使用1.7在:

<configuration> 
    <source>1.7</source> 
    <target>1.7</target> 
    <encoding>UTF-8</encoding> 
    <showWarnings>true</showWarnings> 
    <showDeprecation>true</showDeprecation> 
</configuration> 
+0

我改變了,但我仍然遇到同樣的問題:( – Sauron