0

I'm trying to use hibernate in a sample, but cannot run it. Here is ma main class:

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();
        }
    }
}

I've build it with

mvn clean install assembly:single

but when I've runned JAR, I've got:

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

My 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>

What I'm doing wrong?

EDIT: I've opened result JAR with JD-Gui and discovered that there is Configuration class in my JAR.

1 Answer 1

1

You should get two jar files with your maven configuration. If you try running hibernate-test-1.0.jar you will get the error you are getting because it doesn't contain the dependencies. It is of no use by itself.

If you run hibernate-test-1.0-jar-with-dependencies.jar you will not get the ClassNotFoundException. That jar file is created with the maven-assembly-plugin and it contains all the dependencies you defined in you pom.

Sign up to request clarification or add additional context in comments.

1 Comment

Oh, it was my fault - I've forget to change JAR name. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.