java.lang.NoClassDefFoundError: Could not initialize class com.DatabaseHandler.DBHandler

I’m trying to host my first backend server via heroku. I have already hosted a mysql server (also on heroku) that I have been able to connect to from my local using both my java servlet and mysql workbench.

I used heroku’s git integration to push my servlet to their servers, and as far as I can tell, my tomcat server IS running. When I try to hit one of my urls I get an error message.

The only one I can see that matters is the last one.

java.lang.NoClassDefFoundError: Could not initialize class com.DatabaseHandler.DBHandler.

From what I can understand, this is being caused because my static initialization for my session handler is failing.

I’ve double checked all of my connections, made sure my mysql drivers were correct, added the mysql driver to the buildpath just in case heroku can’t find it, and a host of other things.

Most of my google searches have come up fruitless with solutions I’ve already tried, or answers that don’t pertain to my problem.

Article Controller

public class ArticleService {
    Session session;

    public ArticleService() {
        this.session = DBHandler.getSessionFactory().openSession();

    public List<Article> getAllArticles() {
        List<Article> list = session.createQuery("from Article").list();
        return list;

    public ArticleModelView getArticlePage(int pageNumber, int itemsPerPage) {
        ArticleModelView avm = new ArticleModelView();

        Query q = session.createQuery("from Article");
        q = q.setFirstResult(pageNumber*itemsPerPage);
        q = q.setMaxResults(itemsPerPage);

        avm.articles = q.list();
        int temp = session.createQuery("from Article").list().size();
        temp = temp % itemsPerPage == 0 ? temp/itemsPerPage - 1: temp/itemsPerPage;

        avm.maxPageSize = temp;

        return avm;

Database Handler

public class DBHandler
    private static final SessionFactory sessionFactory;

    static {
        try {
            sessionFactory =  new AnnotationConfiguration().configure().buildSessionFactory();

        } catch (Throwable ex) {
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);

    public static SessionFactory getSessionFactory() {
        return sessionFactory;

    public static void shutdown() {

Hibernate Config

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
  <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
  <property name="hibernate.connection.url">*******?reconnect=true</property>
  <property name="hibernate.connection.password">******</property>
  <property name="hibernate.connection.username">******</property>
  <property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
  <property name="show_sql">true</property>
  <property name="">update</property>
  <mapping class="com.dailybagel.user.resources.User"/>
  <mapping class="com.dailybagel.article.resources.Article"/>

When I visit that url with local host the application correctly pings the hosted mysql server and returns the contents of that table.

The goal is to get the hosted servlet to do that same thing.

Leave a Reply

1 Comment threads
0 Thread replies
Most reacted comment
Hottest comment thread
1 Comment authors
Jason Recent comment authors
newest oldest most voted
Notify of

So it looks like Heroku misplaces the Hibernate config file, or it’s lost, or something of the sort. Adding your config variable to the Session Handler resolves the issue. I don’t really understand why it gets misplaced, but hopefully this will help someone! static { try { Properties prop= new Properties(); prop.setProperty("hibernate.connection.url", "*********"); prop.setProperty("hibernate.connection.username", "********"); prop.setProperty("hibernate.connection.password", "********"); prop.setProperty("dialect", "org.hibernate.dialect.MySQLInnoDBDialect"); sessionFactory = new AnnotationConfiguration() .addPackage("com.dailybagel.user.resources") .addPackage("com.dailybagel.article.resources") .addAnnotatedClass(User.class) .addAnnotatedClass(Article.class) .addProperties(prop) .buildSessionFactory(); } catch (Throwable ex) { System.err.println("Initial SessionFactory creation failed." + ex); throw new ExceptionInInitializerError(ex); } } If anyone know why this happens, I’d love to know more about it