title: Hibernate NoClassDefFoundError in simple application
body: <p>I've read multiple posts about NoClassDefFoundError but didn't find satisfying answer.</p>
  
  <p>I've started learning hibernate. I have written simple application to persist Movie object in my db. </p>
  
  <p>I successfuly compiled my code with</p>
  
  <pre><code>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
  </code></pre>
  
  <p>However when i'm trying to run my code with</p>
  
  <pre><code>java -classpath ./:~/cp/hibernate-release-5.0.1.Final/lib/required/hibernate-core-5.0.1.Final.jar:./classes/ justhibernate.BasicMovieManager
  </code></pre>
  
  <p>I'm encountering error</p>
  
  <pre><code>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
  </code></pre>
  
  <p>I have <code>hibernate.cfg.xml</code> and <code>Movie.hbm.xml</code> files in <code>./</code> directory. Here's my code:</p>
  
  <p>BasicMovieManager.java</p>
  
  <pre><code>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);
      }
  }
  </code></pre>
  
  <p>Movie.java</p>
  
  <pre><code>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;
      }
  }
  </code></pre>
  
  <p>hibernate.cfg.xml</p>
  
  <pre><code>&lt;hibernate-configuration&gt;
      &lt;session-factory&gt;
  
          &lt;property name="connection.url"&gt;
              jdbc:mysql://localhost:3306/test
          &lt;/property&gt;
          &lt;property name="connection.driver_class"&gt;
              com.mysql.jdbc.Driver
          &lt;/property&gt;
          &lt;property name="connection.username"&gt;
              user
          &lt;/property&gt;
          &lt;property name="connection.password"&gt;
              password
          &lt;/property&gt;
          &lt;property name="dialect"&gt;
              org.hibernate.dialect.MySQL5Dialect
          &lt;/property&gt;
  
          &lt;mapping resource="Movie.hbm.xml" /&gt;
  
      &lt;/session-factory&gt;
  &lt;/hibernate-configuration&gt;
  </code></pre>
  
  <p>Movie.hbm.xml</p>
  
  <pre><code>&lt;hibernate-mapping&gt;
      &lt;class name="justhibernate.Movie" table="MOVIES"&gt;
          &lt;id name="id" column="ID"&gt;
              &lt;generator class="native" /&gt;
          &lt;/id&gt;
          &lt;property name="title" column="TITLE" /&gt;
          &lt;property name="director" column="DIRECTOR" /&gt;
          &lt;property name="synopsis" column="SYNOPSIS" /&gt;
      &lt;/class&gt;
  &lt;/hibernate-mapping&gt;
  </code></pre>