0

I've read multiple posts about NoClassDefFoundError but didn't find satisfying answer.

I've started learning hibernate. I have written simple application to persist Movie object in my db.

I successfuly compiled my code with

javac -classpath ~/cp/hibernate-release-5.0.1.Final/lib/required/hibernate-core-5.0.1.Final.jar -d classes justhibernate/BasicMovieManager.java  justhibernate/Movie.java

However when i'm trying to run my code with

java -classpath ./:~/cp/hibernate-release-5.0.1.Final/lib/required/hibernate-core-5.0.1.Final.jar:./classes/ justhibernate.BasicMovieManager

I'm encountering error

Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: org/hibernate/service/ServiceRegistry
    at java.lang.Class.getDeclaredMethods0(Native Method)
    at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
    at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
    at java.lang.Class.getMethod0(Class.java:3018)
    at java.lang.Class.getMethod(Class.java:1784)
    at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:544)
    at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:526)
Caused by: java.lang.ClassNotFoundException: org.hibernate.service.ServiceRegistry
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 7 more

I have hibernate.cfg.xml and Movie.hbm.xml files in ./ directory. Here's my code:

BasicMovieManager.java

package justhibernate;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.MetadataSources;

public class BasicMovieManager 
{
    private SessionFactory sessionFactory = null;

    private void setUp() throws Exception
    {
        final StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
                .configure() 
                .build();
        try 
        {
            sessionFactory = new MetadataSources( registry )
                .buildMetadata().buildSessionFactory();
        }
        catch (Exception e) 
        {
            StandardServiceRegistryBuilder.destroy( registry );
        }
    }

    private void persistMovie(Movie movie) 
    {
        if(sessionFactory == null) 
        {
            try {setUp();}
            catch(Exception e){e.printStackTrace();}
        }   
        Session session = sessionFactory.openSession();
        session.beginTransaction();
        session.save(movie);
        session.getTransaction().commit();
        session.close();
    }

    public static void main(String[] args)
    {
        BasicMovieManager bmm = new BasicMovieManager();
        Movie m = new Movie();
        m.setId(2);
        m.setTitle("Harry Potter and the Chamber of Secrets");
        m.setDirector("Steve Kloves");
        m.setSynopsis("2nd part od thunder-face magician");
        bmm.persistMovie(m);
    }
}

Movie.java

package justhibernate;

public class Movie
{
    private int id = 0;
    private String title = null;
    private String synopsis = null;
    private String director = null;

    public void setId(int id)
    {
        this.id = id;
    }

    public int getId()
    {
        return id;
    }

    public void setTitle(String title)
    {
        this.title = title;
    }

    public String getTitle()
    {
        return title;
    }

    public void setSynopsis(String synopsis)
    {
        this.synopsis = synopsis;
    }

    public String getSynopsis()
    {
        return synopsis;
    }

    public void setDirector(String director)
    {
        this.director = director;
    }

    public String getDirector()
    {
        return director;
    }
}

hibernate.cfg.xml

<hibernate-configuration>
    <session-factory>

        <property name="connection.url">
            jdbc:mysql://localhost:3306/test
        </property>
        <property name="connection.driver_class">
            com.mysql.jdbc.Driver
        </property>
        <property name="connection.username">
            user
        </property>
        <property name="connection.password">
            password
        </property>
        <property name="dialect">
            org.hibernate.dialect.MySQL5Dialect
        </property>

        <mapping resource="Movie.hbm.xml" />

    </session-factory>
</hibernate-configuration>

Movie.hbm.xml

<hibernate-mapping>
    <class name="justhibernate.Movie" table="MOVIES">
        <id name="id" column="ID">
            <generator class="native" />
        </id>
        <property name="title" column="TITLE" />
        <property name="director" column="DIRECTOR" />
        <property name="synopsis" column="SYNOPSIS" />
    </class>
</hibernate-mapping>
Julian Rubin
  • 1,175
  • 1
  • 11
  • 23
  • I added full javac and java commands at the beginning. They contains same .jar file. – Julian Rubin Sep 23 '15 at 14:05
  • Try using Eclipse or IntelliJ or some IDE, they often will take care of things you may miss. – Blake Yarbrough Sep 23 '15 at 14:06
  • I know that using IDE is simpler but I want to be sure I can compile and run it from shell. – Julian Rubin Sep 23 '15 at 14:34
  • Java knows nothing (as John Snow ;)) about tilde operator when running your code, answered below, also found this topic http://stackoverflow.com/questions/1779178/compiled-ok-but-noclassdeffounderror-when-running and http://stackoverflow.com/questions/9122989/use-javac-with-multiple-specific-jars-in-linux – CroWell Sep 23 '15 at 14:44

1 Answers1

-1

Looks like it's caused by relative (starts with home symbol ~) path to Hibernate library. I tried to run you code with absolute path and it successfully found Hibernate. But note that your code won't run without specifying paths to MySQL JDBC, JBoss logging and other required libraries, full list is here.

CroWell
  • 606
  • 8
  • 15
  • Both, the info about ~ and about dependencies were useful. Thank you very much. That was problem with tilde indeed. – Julian Rubin Sep 23 '15 at 15:32