Janaína Janaína - 3 months ago 24
Java Question

java.lang.NoClassDefFoundError: Could not initialize class financeiro.util.JpaUtil

I need help to display a page of consultation results of the ebook Algaworks. Any help will be welcome!

Console (Log file)

jul 25, 2016 10:42:45 PM com.sun.faces.lifecycle.InvokeApplicationPhase execute
ADVERTÊNCIA: #{consultaLancamentosBean.consultar}: javax.el.PropertyNotFoundException: /ConsultaLancamentos.xhtml @10,65 action="#{consultaLancamentosBean.consultar}": Target Unreachable, identifier 'consultaLancamentosBean' resolved to null
javax.faces.FacesException: #{consultaLancamentosBean.consultar}: javax.el.PropertyNotFoundException: /ConsultaLancamentos.xhtml @10,65 action="#{consultaLancamentosBean.consultar}": Target Unreachable, identifier 'consultaLancamentosBean' resolved to null
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:118)
at javax.faces.component.UIViewAction.broadcast(UIViewAction.java:559)
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:790)
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1282)
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:646)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1096)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:674)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)
Caused by: javax.faces.el.EvaluationException: javax.el.PropertyNotFoundException: /ConsultaLancamentos.xhtml @10,65 action="#{consultaLancamentosBean.consultar}": Target Unreachable, identifier 'consultaLancamentosBean' resolved to null
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:94)
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
... 28 more
Caused by: javax.el.PropertyNotFoundException: /ConsultaLancamentos.xhtml @10,65 action="#{consultaLancamentosBean.consultar}": Target Unreachable, identifier 'consultaLancamentosBean' resolved to null
at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:107)
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:87)
... 29 more

jul 25, 2016 10:42:45 PM com.sun.faces.context.ExceptionHandlerImpl log
1100: JSF1073: javax.faces.FacesException obtido durante o processamento de INVOKE_APPLICATION 5: UIComponent-ClientId=, Message=#{consultaLancamentosBean.consultar}: javax.el.PropertyNotFoundException: /ConsultaLancamentos.xhtml @10,65 action="#{consultaLancamentosBean.consultar}": Target Unreachable, identifier 'consultaLancamentosBean' resolved to null
jul 25, 2016 10:42:45 PM com.sun.faces.context.ExceptionHandlerImpl log
1100: #{consultaLancamentosBean.consultar}: javax.el.PropertyNotFoundException: /ConsultaLancamentos.xhtml @10,65 action="#{consultaLancamentosBean.consultar}": Target Unreachable, identifier 'consultaLancamentosBean' resolved to null
javax.faces.FacesException: #{consultaLancamentosBean.consultar}: javax.el.PropertyNotFoundException: /ConsultaLancamentos.xhtml @10,65 action="#{consultaLancamentosBean.consultar}": Target Unreachable, identifier 'consultaLancamentosBean' resolved to null
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:89)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:646)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1096)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:674)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)
Caused by: javax.faces.FacesException: #{consultaLancamentosBean.consultar}: javax.el.PropertyNotFoundException: /ConsultaLancamentos.xhtml @10,65 action="#{consultaLancamentosBean.consultar}": Target Unreachable, identifier 'consultaLancamentosBean' resolved to null
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:118)
at javax.faces.component.UIViewAction.broadcast(UIViewAction.java:559)
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:790)
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1282)
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
... 24 more
Caused by: javax.faces.el.EvaluationException: javax.el.PropertyNotFoundException: /ConsultaLancamentos.xhtml @10,65 action="#{consultaLancamentosBean.consultar}": Target Unreachable, identifier 'consultaLancamentosBean' resolved to null
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:94)
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
... 28 more
Caused by: javax.el.PropertyNotFoundException: /ConsultaLancamentos.xhtml @10,65 action="#{consultaLancamentosBean.consultar}": Target Unreachable, identifier 'consultaLancamentosBean' resolved to null
at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:107)
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:87)
... 29 more


---> Persistence.xml (/src/main/resources/META-INF):

<?xml version="1.0" encoding="UTF-8" ?>
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
version="2.1">

<persistence-unit name="FinanceiroPU">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.janaina.financeiro.model.Pessoa</class>
<class>com.janaina.financeiro.model.Lancamento</class>

<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost:5432/financeiro" />
<property name="javax.persistence.jdbc.user" value="postgres" />
<property name="javax.persistence.jdbc.password" value="postgres" />
<property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver" />

<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.hbm2ddl.auto" value="update" />
</properties>
</persistence-unit>
</persistence>


---> ConsultaLacamentos.xhtml:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:h="http://xmlns.jcp.org/jsf/html">
<h:head>
<title>Consulta de lançamentos</title>
</h:head>
<h:body>
<f:metadata>
<f:viewAction action="#{consultaLancamentosBean.consultar}" />
</f:metadata>
<h1>Consulta de lançamentos</h1>
<h:form id="frm">
<h:dataTable value="#{consultaLancamentosBean.lancamentos}"
var="lancamento" border="1" cellspacing="0" cellpadding="2">
<h:column>
<f:facet name="header">
<h:outputText value="Pessoa" />
</f:facet>
<h:outputText value="#{lancamento.pessoa.nome}" />
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Descrição" />
</f:facet>
<h:outputText value="#{lancamento.descricao}" />
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Tipo" />
</f:facet>
<h:outputText value="#{lancamento.tipo}" />
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Valor" />
</f:facet>
<h:outputText value="#{lancamento.valor}" />
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Data de vencimento" />
</f:facet>
<h:outputText value="#{lancamento.dataVencimento}" />
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Data de pagamento" />
</f:facet>
<h:outputText value="#{lancamento.dataPagamento}" />
</h:column>
</h:dataTable>
</h:form>
</h:body>
</html>


---> ConsultaLacamentosBean.java

package com.janaina.financeiro.controller;

import java.io.Serializable;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.view.ViewScoped;
import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;

import com.janaina.financeiro.model.Lancamento;
import com.janaina.financeiro.util.JpaUtil;

@ManagedBean
@ViewScoped
public class ConsultaLancamentosBean implements Serializable {
private static final long serialVersionUID = 1L;
private List<Lancamento> lancamentos;

public void consultar() {
EntityManager manager = JpaUtil.getEntityManager();
TypedQuery<Lancamento> query = manager.createQuery("from Lancamento", Lancamento.class);
this.lancamentos = query.getResultList();
manager.close();
}

public List<Lancamento> getLancamentos() {
return lancamentos;
}
}


---> JpaUtil.java

package com.janaina.financeiro.util;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

//JpaUtil armazena a instância compartilhada de EntityManagerFactory em uma variável estática
//Qualquer código terá acesso fácil e rápido
//Sempre que for preciso uma EntityManager basta chamar:
//EntityManager manager = JpaUtil.getEntityManager();

public class JpaUtil {
private static EntityManagerFactory factory;
static {
factory = Persistence.createEntityManagerFactory("FinanceiroPU");
}

public static EntityManager getEntityManager() {
return factory.createEntityManager();
}
}

Answer

I solved the problem! I realized that the causes of my problem were:

1) I was using Java JRE instead of JDK in the project. To fix this I did the following: Rigth-click on Project> Properties> Java Build Path> Libraries> selected JRE I was using> Edit> Installed JREs> Search> JDK path (C: \ Program Files \ Java \ jdk1.8.0_101).

2) Then I realized that I was not able to connect to PostgreSQL. Then I uninstalled PostgreSQL and also deleted its directory in Program Files, then I reinstalled!

3) Finally: Rigth-click on Project> Run as> Maven> Maven Clean and then Rigth-click on Project> Run as> Maven> Maven install

The steps above worked for me.. I hope they help others!

My (web.xml):

<context-param>
                <param-name>javax.faces.PROJECT_STAGE</param-name>
                <param-value>Development</param-value>
        </context-param>

        <servlet>
                <servlet-name>Faces Servlet</servlet-name>
                <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
                <load-on-startup>1</load-on-startup>
        </servlet>
        <servlet-mapping>
                <servlet-name>Faces Servlet</servlet-name>
                <url-pattern>/faces/*</url-pattern>
        </servlet-mapping>

persistence.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
        version="2.1">

        <persistence-unit name="FinanceiroPU">
                <provider>org.hibernate.ejb.HibernatePersistence</provider>
                <class>com.janaina.financeiro.model.Lancamento</class>
                <class>com.janaina.financeiro.model.Pessoa</class>

                <properties>
                        <property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost:5432/financeiro" />
                        <property name="javax.persistence.jdbc.user" value="postgres" />
                        <property name="javax.persistence.jdbc.password" value="postgres" />
                        <property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver" />

                        <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
                        <property name="hibernate.show_sql" value="true" />
                        <property name="hibernate.format_sql" value="true" />
                        <property name="hibernate.hbm2ddl.auto" value="update" />
                </properties>
        </persistence-unit>
</persistence>