java.lang.NoClassDefFoundError 异常:休眠 类 初始化期间

java.lang.NoClassDefFoundError Exception : Hibernate Classes During Initialization

我有一个我无法解决的问题。我需要你的帮助来解决问题。 我有两个项目,“sampleZkProject”和“institutiongeography”。两者都是maven项目。他们 运行 在 Java 8.

“sampleZkProject”是一个从web层负责的sample zk project。它需要与制度地理相互作用。 institutiongeography 是一个 pom 类型的 maven 项目。 Institutiongeography 使用 hibernate 作为 ORM 框架。 Institutiongeography 已经通过了测试,所以我们知道它运作良好的置信区间。

“sampleZkProject”的pom结构如下:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>experimental.tahasozgen.zk</groupId>
    <artifactId>ornek1</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <properties>
        <zk.version>9.6.0</zk.version>
        <commons-io>2.6</commons-io>
        <maven.build.timestamp.format>yyyy-MM-dd</maven.build.timestamp.format>
        <packname>-${project.version}-FL-${maven.build.timestamp}</packname>
    </properties>
    <packaging>war</packaging>
    <name>The ornek1 Project</name>
    <description>The ornek1 Project</description>
    
    <licenses>
        <license>
            <name>GNU LESSER GENERAL PUBLIC LICENSE, Version 3</name>
            <url>https://www.gnu.org/licenses/lgpl.html</url>
            <distribution>repo</distribution>
        </license>
    </licenses>
    
    <repositories>
        <repository>
            <id>ZK CE</id>
            <name>ZK CE Repository</name>
            <url>https://mavensync.zkoss.org/maven2</url>
        </repository>
        <repository>
            <id>ZK EVAL</id>
            <name>ZK Evaluation Repository</name>
            <url>https://mavensync.zkoss.org/eval</url>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>zkmaven</id>
            <name>ZK Maven Plugin Repository</name>
            <url>https://mavensync.zkoss.org/maven2/</url>
        </pluginRepository>
    </pluginRepositories>

    <dependencies>
        <dependency>
            <groupId>org.zkoss.zk</groupId>
            <artifactId>zkbind</artifactId>
            <version>${zk.version}</version>
        </dependency>
        <dependency>
            <groupId>org.zkoss.zk</groupId>
            <artifactId>zul</artifactId>
            <version>${zk.version}</version>
        </dependency>
        <dependency>
            <groupId>org.zkoss.zk</groupId>
            <artifactId>zkplus</artifactId>
            <version>${zk.version}</version>
        </dependency>
        <dependency>
            <groupId>org.zkoss.zk</groupId>
            <artifactId>zhtml</artifactId>
            <version>${zk.version}</version>
        </dependency>
        
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>${commons-io}</version>
        </dependency>

        <!--other dependencies-->
        
        <dependency>
            <groupId>com.tahasozgen.institutiongeography</groupId>
            <artifactId>institutiongeography-core</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        
        <dependency>
            <groupId>com.tahasozgen.institutiongeography</groupId>
            <artifactId>institutiongeography-entities</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        
        <dependency>
            <groupId>com.tahasozgen.institutiongeography</groupId>
            <artifactId>institutiongeography-repository</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        
        <dependency>
            <groupId>com.tahasozgen.institutiongeography</groupId>
            <artifactId>institutiongeography-businesslayer</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        
        <dependency>
            <groupId>com.tahasozgen.institutiongeography</groupId>
            <artifactId>institutiongeography-contracts</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        
        <dependency>
            <groupId>com.tahasozgen.institutiongeography</groupId>
            <artifactId>institutiongeography-service</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.4.27.Final</version>
        </dependency>
         
    </dependencies>


    <build>
        <finalName>${project.artifactId}</finalName>
        <plugins>
            <!-- Run with Jetty -->
            <plugin>
                <groupId>org.eclipse.jetty</groupId>
                <artifactId>jetty-maven-plugin</artifactId>
                <version>10.0.5</version>
                <configuration>
                    <webApp>
                        <contextPath>/${project.artifactId}</contextPath>
                        <allowDuplicateFragmentNames>true</allowDuplicateFragmentNames>
                    </webApp>
                    <scanIntervalSeconds>5</scanIntervalSeconds>
                </configuration>
            </plugin>
            <!-- Compile java -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>8</source>
                    <target>8</target>
                </configuration>
            </plugin>
            <!-- Build war -->
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <groupId>org.apache.maven.plugins</groupId>
                <version>3.3.1</version>
            </plugin>
            <!-- Pack zips -->
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.2</version>
                <executions>
                    <execution>
                        <id>webapp</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                        <configuration>
                            <finalName>ornek1${packname}</finalName>
                            <appendAssemblyId>false</appendAssemblyId>
                            <descriptors>
                                <descriptor>src/main/assembly/webapp.xml</descriptor>
                            </descriptors>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

sampleZkProject 有一个 class 负责与 institutiongeography 交互。这个class'结构如下:

package experimental.tahasozgen.zk.ornek1;

import java.util.HashMap;

import org.zkoss.bind.annotation.Command;
import org.zkoss.bind.annotation.Destroy;
import org.zkoss.bind.annotation.Init;
import org.zkoss.bind.annotation.NotifyChange;
  
import experimental.tahasozgen.institutiongeography.contracts.abstracts.institutionmanagement.InstitutionService;

public class MyViewModel {
    
    private InstitutionService institutionService;

    private int count;

    @Init
    public void init() {

        count = 100;

        institutionService = experimental.tahasozgen.institutiongeography.service.dependencymanagement.DependencyResolver.getSample().resolve(InstitutionService.class);

        System.out.println(String.format("is institutionService empty? %b", institutionService ==null));        


    }

    /*other functions*/

}

experimental.tahasozgen.institutiongeography.service.dependencymanagement.DependencyResolver.getSample().resolve(Class classInstance)

是一个 java 函数,它在 运行 时解析 InstitutionService。当它尝试解决它时,它 运行 如下所示:

private EntityManagerFactory createEntityManagerFactory() {

    return new EntityManagerFactory();

}

EntityManagerFactory的定义如下:

package experimental.tahasozgen.institutiongeography.businesslayer.dependencyresolvers;
    
package experimental.tahasozgen.institutiongeography.businesslayer.dependencyresolvers;

//import java.util.concurrent.BlockingQueue;
//import java.util.concurrent.LinkedBlockingQueue;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory; 

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set; 
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
import org.hibernate.jpa.HibernatePersistenceProvider;
     
import experimental.tahasozgen.institutiongeography.core.crosscuttingconcern.exceptions.ExceptionAffairs;
import experimental.tahasozgen.institutiongeography.core.dataccess.entityframework.IEntityManagerFactory;

class EntityManagerFactory implements IEntityManagerFactory {
    
    private static EntityManagerFactory factory;
    
    private List<EntityManager> activeEntityManagerList;
    
    private List<EntityManager> closedEntityManagerList;
    
    static {

        try {

            if (factory == null) {
                factory = createEntityManagerFactory();
            }

        } catch (ExceptionInInitializerError hata) {
            throw hata;
        }

    }
    
    public EntityManagerFabrikasi() {
    
        activeEntityManagerList = new ArrayList <EntityManager>();
        closedEntityManagerList = new ArrayList <EntityManager>();
    }
    
    @Override
    public EntityManager createEntityManagerFactory () throws Exception {

        try {

            EntityManager entityManager = factory.createEntityManager();
            
            this.activeEntityManagerList.add(entityManager);
            
            return entityManager;
            
        } catch (Exception hata ) {

            throw hata;
            
        }
        
    }

    @Override
    public void close() throws Exception {
        
        if(this.activeEntityManagerList!= null) {
            
            for(EntityManager entityManager : this.activeEntityManagerList) {
                
                if(entityManager == null )
                    continue;
                
                entityManager.close();
                
            }
            
        }
        
        
    }

    @Override
    public void close ( EntityManager entityManager ) {
        
        if(entityManager == null)
            return;
        
        entityManager.clear();
        entityManager.close();  
    
        this.activeEntityManagerList.remove(entityManager);
        this.closedEntityManagerList.add(entityManager);
        
    }
    
    private static synchronized EntityManagerFactory createEntityManagerFactory() {

        EntityManagerFactory entityManagerFactory = new HibernatePersistenceProvider()
                .createContainerEntityManagerFactory(PersistenceUnitInfoIsleri.olusturPersistenceUnitInfo(),
                        getProperties());

        return entityManagerFactory;

    }

    private static Map<String, String> getProperties() {

        try {

            Map<String, String> result = new HashMap<String, String>();

            Properties properties = new Properties();

            String databasaConnectionDetailsFile = "details.txt";

            try (InputStream inputStream = EntityManagerFabrikasi.class.getClassLoader()
                    .getResourceAsStream(databasaConnectionDetailsFile)) {

                if (inputStream != null)
                    properties.load(inputStream);
                else
                    throw new Exception("Even if nothing happened, something definitely happened.");

            }

            Set<String> keys = properties.stringPropertyNames();

            for (String key : keys)
                result.put(key, properties.getProperty(key));

            return result;

        } catch (Exception hata) {

            ExceptionAffairs exceptionAffairs = new ExceptionAffairs();

            exceptionAffairs.report(hata);

        }

        return new HashMap<String, String>();
    }   
    
}

这是带有 EntityManagerFactory 的 maven 模块的 pom 结构:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>experimental.tahasozgen</groupId>
    <artifactId>institutiongeography</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>institutiongeography-businesslayer</artifactId>
  <name>institutiongeography-businesslayer</name>
  
  <dependencies>
   
        <dependency>
            <groupId>experimental.tahasozgen</groupId>
            <artifactId>institutiongeography-core</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>experimental.tahasozgen</groupId>
            <artifactId>institutiongeography-contracts</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>experimental.tahasozgen</groupId>
            <artifactId>institutiongeography-repository</artifactId>
            <version>0.0.1-SNAPSHOT</version> 
        </dependency>
        <dependency>
            <groupId>experimental.tahasozgen</groupId>
            <artifactId>institutiongeography-entities</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        
        <!-- umumi bağımlılıklar -->
        <dependency>
            <groupId>com.baidu.unbiz</groupId>
            <artifactId>fluent-validator</artifactId>
            <version>1.0.9</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.4.27.Final</version>
        </dependency>
    </dependencies>
    
</project>

所有maven模块的范围都是“编译”。 当我调试程序时,它会在不同的调试会话中抛出不同的异常。抛出的异常如下:

java.lang.NoClassDefFoundError: org/hibernate/service/ServiceRegistry

java.lang.NoClassDefFoundError: org.hibernate.jpa.HibernatePersistenceProvider

我推测 hibernate 不会在 运行 时加载。我该如何解决这个问题? 提前致谢。

问题是pom.xml中两个不同依赖的冲突。依赖性

<dependency>
  <groupId>org.hibernate</groupId>
  <artifactId>hibernate-core</artifactId>
  <version>5.4.27.Final</version>
</dependency>

institutiongeography-core的pom文件和sampleZkProject的pom文件中都有定义。当我从 sampleZkProject 的 pom 文件中删除依赖项时,问题已解决。您可以在eclipse的依赖层次结构部分观察依赖关系。

maven clean命令建议在pom整理后发出。