Good day!

There was a problem using a bunch of Java + Hibernate + JSP + Tomcat. When the project and MySQL database are located on the local computer, everything works perfectly. However, after transferring to the server, the following error occurs:

HTTP Status 500 - Handler processing failed; nested exception is java.lang.NoClassDefFoundError: Could not initialize class ru.develop.test.utils.HibernateUtils java.lang.NoClassDefFoundError: Could not initialize class ru.develop.test.utils.HibernateUtils 

I read that this may be related to the static section of the HibernateUtils.java configuration file:

 public class HibernateUtils { private static final SessionFactory sessionFactory; public HibernateUtils() { } static { try { sessionFactory = new Configuration().configure().buildSessionFactory(); } catch (Throwable e) { System.err.println("Initial SessionFactory creation failed." + e); throw new ExceptionInInitializerError(e); } } public static SessionFactory getSessionFactory() { return sessionFactory; } } 

Config:

 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql://<мой хост>.ru:3306/dbname?autoReconnect=true</property> <property name="hibernate.connection.username">username</property> <property name="hibernate.connection.password">password</property>> <!-- JDBC connection pool (use the built-in) --> <property name="connection.pool_size">1</property> <!-- Enable Hibernate's automatic session context management --> <property name="current_session_context_class">thread</property> <property name="hibernate.enable_lazy_load_no_trans">true</property> <!-- Disable the second-level cache --> <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property> <!-- Echo all executed SQL to stdout --> <property name="show_sql">true</property> <!-- Mapping files --> </session-factory> </hibernate-configuration> 

How can I overcome the problem?

Thank!

  • it all depends on how you collect the project, if you are without a mavena, then I advise you to fasten it (or graid). if the import is incorrectly configured with it. - jmu
  • The project is built to NetBeans using Maven, then sent to a remote server with Tomcat installed. Maven is missing from the server, can this cause a problem? What does "import incorrectly configured" mean? Where to watch it? Libraries lie in the .war file. - Dmitry Posadsky pm

1 answer 1

The problem may be caused by the absence of imported classes. Make sure that the list of libraries locally and on the server is identical - both in your application and in the Tomcat library folder. This can solve both this problem and the likelihood of the same errors in other classes, and is a more universal and reliable solution.

Alternatively, in order to pinpoint exactly the problem of this class, check all imports in the ru.develop.test.utils.HibernateUtils class, and ensure that the library ru.develop.test.utils.HibernateUtils classes to be imported. There is one BUT - imported libraries can have their own imports, and it can be difficult to manually track them all. Therefore, it is better to check the entire composition of the libraries at once, for this you usually need to compare only two pairs of folders: the library folder of your application and the folder of the Tomcat libraries.

  • Good afternoon, thanks for the reply! Libraries are in the .war file. Now, just in case, I copied them into the% tomcat_home% / lib folder - Dmitry Posadsky
  • Copying to% tomcat_home% / lib from war is not worth it, just check the% tomcat_home% / lib on your server and check WEB-INF / lib as well. Alternatively, check the version of Java. - bobzer