Java.lang.NoClassDefFoundError: org/hibernate/cfg/Configuration

4 respostas
johnny_g3p

Galera estou com um problema com hibernate e não estou conseguindo resolver a algum tempo.

tento rodar meu programa e gera o seguinte erro

root cause 

java.lang.NoClassDefFoundError: org/hibernate/cfg/Configuration
	br.com.wincomp.empresa.Gerenciador.salvarEmpresa(Gerenciador.java:13)
	br.com.wincomp.empresa.GravarEmpresa.doGet(GravarEmpresa.java:32)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:722)


root cause 

java.lang.ClassNotFoundException: org.hibernate.cfg.Configuration
	org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1678)
	org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1523)
	br.com.wincomp.empresa.Gerenciador.salvarEmpresa(Gerenciador.java:13)
	br.com.wincomp.empresa.GravarEmpresa.doGet(GravarEmpresa.java:32)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

hibernate.cfg.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
	"-//Hibernate/Hibernate Mapping DTD 3,0//EN"
	"http://hibernate.sourceforge.net/hibernate-mapping-3,0dtd">
	
<hibernate-configuration>
	<session-factory>
	<!--Database connection settings-->
	
	<Property name = "connection.driver_class">com.mysql.jdbc.Driver</Property>
	<Property name = "connection.url">jdbc:mysql://127.0.0.1/test</Property>
	<Property name = "connection.username">root</Property>
	<Property name = "connection.password">admin</Property>
	
	<!--JDBC connection pool_size (use the built-in)-->
	<Property name = "connection.pool_size">1</Property>
	
	<!--SQL Dialect-->
	<Property name = "dialect">org.hibernate.dialect.MySQLDialect</Property>
	
	<!--Enable Hibernate's automatic session context management -->
	<Property name = "current_session_context_class">thread</Property>
	
	<!--Disable second-level cache -->
	<Property name = "cache.provider_class">org.hibernate.cache.NoCacheProvider</Property>
	
	<!--Echo all executed SQL to stdout -->
	<Property name = "show_sql">false</Property>
	
	<mapping resource = "br/com/wincomp/empresa/Empresa.hbm.xml"/>
	<mapping resource = "br/com/wincomp/empresa/Consultor.hbm.xml"/>
		
	</session-factory>
</hibernate-configuration>	
</hibernate-mapping>

meu Servlet

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@WebServlet("/GravarEmpresa")
public class GravarEmpresa extends HttpServlet {
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String empresa = request.getParameter("empresa");
		String valorHora = request.getParameter("valorHora");
		
		response.setContentType("text/html;charset=ISO-8859-1");
		PrintWriter out = response.getWriter();
		out.println("<head>");
		out.println("<title>Gravando Empresa no Hibernate</title>");
		out.println("<LINK href=estilo.css type=text/css rel = stylesheet");
		out.println("</head>");
		out.println("<html>");
		out.println("<body>");
		out.println("<h2>Gravando Eventos no HGibernate</h2>");
		out.println("Empresa:"+empresa+"br");
		out.println("Valor Hora:"+valorHora+"br");
		Gerenciador g = new Gerenciador();
		g.salvarEmpresa(empresa, valorHora);
		out.println("</body>");
		out.println("</html>");
		out.close();
		
	}

}
import org.hibernate.*;
import org.hibernate.cfg.*;


public class Gerenciador {
	
	@SuppressWarnings("deprecation")
	public void salvarEmpresa(String empresa,String valorHora){
		
		try{
			SessionFactory sf = new Configuration().configure("hibernate.cfg.xml").buildSessionFactory();
			Session s = sf.openSession();
			Transaction tx = s.beginTransaction();
			Empresa e = new Empresa();
			e.setEmpresa(empresa);
			e.setValorHora(valorHora);
			s.saveOrUpdate(e);
			tx.commit();
			s.close();
			System.out.println("Evento Incluido");
			
		}catch (Exception e) {
			System.out.println("Erro" + e.getMessage());
			
		}
		
	}
	
	

}

4 Respostas

romarcio

Vi que tem essa tag: </hibernate-mapping> no seu hibernate.cfg.xml, retire ela de lá.

java.lang.ClassNotFoundException: org.hibernate.cfg.Configuration Significa que seu sistema não está encontrando a classe. Você adicionou ao projeto a biblioteca do hibernate?

johnny_g3p

adicionei sim
veja minhas bibliotecas

johnny_g3p

Consegui mais ou menos na verdade se importar o projeto para o webapps do tomcat e importar as libs ele funciona normalmente, mais no eclipse não estou conseguindo configurar o classpatch se alguém ai souber agradeço a ajuda

wlw galera

dofun12

No eclipse,copie essa biblioteca no diretorio do seu projeto: workspace/seu_projeto/WebContent/Web-Inf/lib/.

Depois informe o caminho no eclipse: "Propriedades>Java build patch>Library>Add jars."

Criado 12 de junho de 2012
Ultima resposta 13 de jun. de 2012
Respostas 4
Participantes 3