0

我正在尝试在示例中使用休眠,但无法运行它。这是主要课程:

public class Main {
    public static void main(String[] args) {
        SessionFactory factory;

        try {
            Configuration configuration = new Configuration();
            configuration.configure("hibernate.cfg.xml");
            ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
            factory = configuration.buildSessionFactory(serviceRegistry);
        } catch (Throwable ex) {
            System.err.println("Failed to create sessionFactory object." + ex);
            throw new ExceptionInInitializerError(ex);
        }

        System.out.println("**Example : Hibernate 4 SessionFactory**");
        System.out.println("----------------------------------------");
        Session session = factory.openSession();
        Transaction transaction = null;

        try {
            transaction = session.beginTransaction();
            transaction.commit();
        } catch (HibernateException e) {
            if (transaction != null)
                transaction.rollback();
            e.printStackTrace();
        } finally {
            session.close();
        }
    }
}

我已经用

mvn clean install assembly:single

但是当我运行 JAR 时,我得到了:

Exception in thread "main" java.lang.ExceptionInInitializerError
    at Main.main(Main.java:28)
Caused by: java.lang.NoClassDefFoundError: org/hibernate/cfg/Configuration
    at Main.main(Main.java:21)
Caused by: java.lang.ClassNotFoundException: org.hibernate.cfg.Configuration
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
    ... 1 more

我的 POM:

<?xml version="1.0" encoding="UTF-8"?>
<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>hibernate-test</groupId>
    <artifactId>hibernate-test</artifactId>
    <version>1.0</version>

    <properties>
        <hibernate.version>4.1.3.Final</hibernate.version>
    </properties>

    <dependencies>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>${hibernate.version}</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>${hibernate.version}</version>
        </dependency>

        <dependency>
            <groupId>org.hsqldb</groupId>
            <artifactId>hsqldb</artifactId>
            <version>2.2.8</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <mainClass>Main</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>

            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>Main</mainClass>
                        </manifest>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

我做错了什么?

编辑:我用 JD-Gui 打开了结果 JAR,发现我的 JAR 中有配置类。

4

1 回答 1

1

您应该使用 Maven 配置获得两个 jar 文件。如果你尝试运行 hibernate-test-1.0.jar 你会得到你得到的错误,因为它不包含依赖项。它本身是没有用的。

如果你运行 hibernate-test-1.0-jar-with-dependencies.jar 你不会得到 ClassNotFoundException。该 jar 文件是使用 maven-assembly-plugin 创建的,它包含您在 pom.xml 中定义的所有依赖项。

于 2012-05-09T15:52:47.113 回答