我用Maven将项目打包在jar中。当我启动一个jar文件时,我得到了一些例外:

Exception in thread "main" java.lang.NoClassDefFoundError: org/hibernate/cfg/Configuration
    at util.HibernateUtil.buildSessionFactory(HibernateUtil.java:12)
    at util.HibernateUtil.<clinit>(HibernateUtil.java:8)
    at model.services.BusService.setBuses(BusService.java:30)
    at view.Runner.main(Runner.java:20)
Caused by: java.lang.ClassNotFoundException: org.hibernate.cfg.Configuration
    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)
    ... 4 more


我的hibernate.cfg.xml

<!DOCTYPE hibernate-configuration SYSTEM
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"><hibernate-configuration>
<session-factory>

    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>

    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>

    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/carpark?useUnicode=true&amp;useJDBCCompliantTimezoneShift=true&amp;useLegacyDatetimeCode=false&amp;serverTimezone=UTC</property>

    <property name="hibernate.connection.username">root</property>

    <property name="hibernate.connection.password">1111</property>

    <property name="hibernate.hbm2ddl.auto">update</property>

    <mapping class="model.entities.Bus"/>
    <mapping class="model.entities.Driver"/>
    <mapping class="model.entities.Rote"/>
</session-factory>




我的pom.xml

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.6</source>
                <target>1.6</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>
                        <mainClass>view.Runner</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
    </plugins>
    <resources>
        <resource>
            <directory>hibernate.cfg.xml</directory>
        </resource>
    </resources>
</build>
<dependencies>
    <dependency>
        <groupId>org.hibernate.javax.persistence</groupId>
        <artifactId>hibernate-jpa-2.1-api</artifactId>
        <version>1.0.0.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>5.0.11.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>5.0.11.Final</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>6.0.6</version>
    </dependency>
</dependencies>




我的HibernateUtil.java

public class HibernateUtil {
private static final SessionFactory sessionFactory = buildSessionFactory();

private static SessionFactory buildSessionFactory() {
    try {
        return new Configuration().configure().buildSessionFactory();
    } catch (Exception ex) {
        return (SessionFactory) new Exception(ex);
    }
}

public static SessionFactory getSessionFactory() {
    return sessionFactory;
}

public static void shutdown() {
    getSessionFactory().close();
}}


我的BusService.java

package model.services;

import model.entities.Bus;
import model.repositories.BusRepository;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import util.HibernateUtil;
import java.util.ArrayList;
import java.util.List;

public class BusService {
private BusRepository busRepository;

public BusService() {
    busRepository = BusRepository.getInstance();
}

public BusRepository getBusRepository() {
    return busRepository;
}

public List<Bus> getBuses() {
    return busRepository.getBuses();
}

public void setBuses() {
    SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
    Session session = sessionFactory.openSession();
    Transaction tx = session.beginTransaction();
    try {
        SQLQuery query = session.createSQLQuery("SELECT ID, NUMBER , Rote_ID FROM bus").addEntity(Bus.class);
        List<Bus> buses = query.list();
        buses.size();
        busRepository.setBuses(buses);
    } finally {
        tx.commit();
        session.close();
    }
}

public List<Bus> findByNumber(Integer number) {
    SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
    Session session = sessionFactory.openSession();
    Transaction tx = session.beginTransaction();
    List<Bus> busesFinder = new ArrayList<Bus>();
    try {
        SQLQuery query = session.createSQLQuery("SELECT ID, NUMBER , Rote_ID FROM bus");
        List<Bus> buses = query.list();
        for (Bus bus : buses) {
            if (bus.getNumber() == number)
                busesFinder.add(bus);
        }
        return busesFinder;
    } finally {
        tx.commit();
        session.close();
    }
}

public void save(Bus bus) {
    SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
    Session session = sessionFactory.openSession();
    Transaction tx = session.beginTransaction();
    try {
        session.save(bus);
    } finally {
        tx.commit();
        session.close();
    }
}


}

BusService.java中的30行是(在setBuses()方法中)

SessionFactory sessionFactory = HibernateUtil.getSessionFactory();


我使用了另一个休眠版本,但是每次都会有例外。
现在我的休眠版本是5.0.11.Final。

请帮我解决问题。
我需要正确启动jar文件,没有例外。

最佳答案

JAR文件通常不包含库依赖项(与WAR文件不同),因此您需要使用诸如maven-shade-plugin之类的工具手动添加它们:

https://maven.apache.org/plugins/maven-shade-plugin/

09-27 23:38