13
respostas

Exceção ao gerar o arquivo PDF no NETBEANS

Boa noite , ao tentar realizar o código no NetBeans gera a seguinte exceção:

Exception in thread "main" net.sf.jasperreports.engine.JRException: java.io.FileNotFoundException: gastos_por_mes.jasper at net.sf.jasperreports.engine.util.JRLoader.loadObject(JRLoader.java:98) at net.sf.jasperreports.engine.JasperFillManager.fill(JasperFillManager.java:272) at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:745) at curso_alura_jasper.Curso_Alura_Jasper.main(Curso_Alura_Jasper.java:39) Caused by: java.io.FileNotFoundException: gastos_por_mes.jasper ... 4 more C:\Users\user\AppData\Local\NetBeans\Cache\8.1\executor-snippets\run.xml:53: Java returned: 1 FALHA NA CONSTRUÇÃO (tempo total: 1 segundo)

Ele não encontra os arquivos jasper, mesmo eu já tendo tentado copiar eles no próprio projeto JAVA. Alguém pode me ajudar? Segue meu código:

package curso_alura_jasper;

import curso_alura_jasper.ConnectionFactory;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRExporter;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.export.JRPdfExporter;
import net.sf.jasperreports.engine.export.JRXlsExporter;

/**
 *
 * @author user
 */
public class Curso_Alura_Jasper {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws SQLException, JRException, FileNotFoundException {
        // TODO code application logic here
        //JasperCompileManager.compileReportToFile("gastos_por_mes.jrxml");
        Connection conexao = new ConnectionFactory().getConnection();
        Map<String, Object> parameters = new HashMap<>();
        JasperPrint jasperPrint = JasperFillManager.fillReport("gastos_por_mes.jasper", parameters, conexao);
        JRExporter exporter = new JRPdfExporter();
        exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
        exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, new FileOutputStream("gasto_por_mes.pdf"));
        exporter.exportReport();
        conexao.close();
    }

}

Agradeço desde já

13 respostas

Opa, experimentar passar exatamente o caminho do seu arquivo.

Eu tentei, mas também não funcionou, existe alguma diferença no código por se tratar do Netbeans e não do eclipse?

Alguém pode ajudar??

Abrindo o projeto no Eclipse , conforme anunciado na vídeo aula ocorre o seguinte erro

Exception in thread "main" java.lang.NoClassDefFoundError: org/codehaus/groovy/control/CompilationFailedException
    at java.base/java.lang.Class.getDeclaredConstructors0(Native Method)
    at java.base/java.lang.Class.privateGetDeclaredConstructors(Unknown Source)
    at java.base/java.lang.Class.getConstructor0(Unknown Source)
    at java.base/java.lang.Class.getConstructor(Unknown Source)
    at net.sf.jasperreports.engine.JasperCompileManager.getCompiler(JasperCompileManager.java:690)
    at net.sf.jasperreports.engine.JasperCompileManager.getEvaluator(JasperCompileManager.java:263)
    at net.sf.jasperreports.engine.fill.JRFillDataset.createCalculator(JRFillDataset.java:457)
    at net.sf.jasperreports.engine.fill.JRBaseFiller.<init>(JRBaseFiller.java:379)
    at net.sf.jasperreports.engine.fill.JRVerticalFiller.<init>(JRVerticalFiller.java:88)
    at net.sf.jasperreports.engine.fill.JRVerticalFiller.<init>(JRVerticalFiller.java:103)
    at net.sf.jasperreports.engine.fill.JRVerticalFiller.<init>(JRVerticalFiller.java:61)
    at net.sf.jasperreports.engine.fill.JRFiller.createFiller(JRFiller.java:153)
    at net.sf.jasperreports.engine.fill.JRFiller.fill(JRFiller.java:55)
    at net.sf.jasperreports.engine.JasperFillManager.fill(JasperFillManager.java:446)
    at net.sf.jasperreports.engine.JasperFillManager.fill(JasperFillManager.java:276)
    at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:745)
    at br.com.caelum.financas.relatorio.TesteGeraRelatorio.main(TesteGeraRelatorio.java:29)
Caused by: java.lang.ClassNotFoundException: org.codehaus.groovy.control.CompilationFailedException
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(Unknown Source)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(Unknown Source)
    at java.base/java.lang.ClassLoader.loadClass(Unknown Source)
    ... 17 more

sege meu código:

package br.com.caelum.financas.relatorio;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRExporter;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.export.JRPdfExporter;
import net.sf.jasperreports.engine.export.JRXlsExporter;
import br.com.caelum.financas.ConnectionFactory;

public class TesteGeraRelatorio {

    public static void main(String[] args) throws SQLException, JRException, FileNotFoundException {

        //JasperCompileManager.compileReportToFile("gastos_por_mes.jrxml"); 
        //JasperCompileManager.compileReportToFile("gastos_por_mes_subreport1.jrxml");

        Connection conexao = new ConnectionFactory().getConnection();
        Map<String, Object> parameters= new HashMap<String, Object>();
        JasperPrint jasperprint = JasperFillManager.fillReport("gastos_por_mes.jasper", parameters, conexao);
        JRExporter exporter = new JRPdfExporter();
        exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperprint);
        exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, new FileOutputStream("Gastos_mes.pdf"));
        exporter.exportReport();
        conexao.close();
    }
}

A exception dentro do eclipse indica que tem alguma falha de versão na biblioteca do groovy. As dependências são configuradas via maven? Se sim, confere pra ver se tá igualzinho ao exercício. Quando vc passou o caminho exato no NetBeans, o erro foi o mesmo? Pode postar aqui?

Boa noite, não estou usando Maven, segue a exceção gerada no NetBeans:

Exception in thread "main" java.lang.NoClassDefFoundError: org/codehaus/groovy/control/CompilationFailedException
    at java.lang.Class.getDeclaredConstructors0(Native Method)
    at java.lang.Class.privateGetDeclaredConstructors(Class.java:2671)
    at java.lang.Class.getConstructor0(Class.java:3075)
    at java.lang.Class.getConstructor(Class.java:1825)
    at net.sf.jasperreports.engine.JasperCompileManager.getCompiler(JasperCompileManager.java:690)
    at net.sf.jasperreports.engine.JasperCompileManager.getEvaluator(JasperCompileManager.java:263)
    at net.sf.jasperreports.engine.fill.JRFillDataset.createCalculator(JRFillDataset.java:457)
    at net.sf.jasperreports.engine.fill.JRBaseFiller.<init>(JRBaseFiller.java:379)
    at net.sf.jasperreports.engine.fill.JRVerticalFiller.<init>(JRVerticalFiller.java:88)
    at net.sf.jasperreports.engine.fill.JRVerticalFiller.<init>(JRVerticalFiller.java:103)
    at net.sf.jasperreports.engine.fill.JRVerticalFiller.<init>(JRVerticalFiller.java:61)
    at net.sf.jasperreports.engine.fill.JRFiller.createFiller(JRFiller.java:153)
    at net.sf.jasperreports.engine.fill.JRFiller.fill(JRFiller.java:55)
    at net.sf.jasperreports.engine.JasperFillManager.fill(JasperFillManager.java:446)
    at net.sf.jasperreports.engine.JasperFillManager.fill(JasperFillManager.java:276)
    at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:745)
    at curso_alura_jasper.Curso_Alura_Jasper.main(Curso_Alura_Jasper.java:39)
Caused by: java.lang.ClassNotFoundException: org.codehaus.groovy.control.CompilationFailedException
    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)
    ... 17 more
C:\Users\user\AppData\Local\NetBeans\Cache\8.1\executor-snippets\run.xml:53: Java returned: 1

código no NetBeans

public class Curso_Alura_Jasper {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws SQLException, JRException, FileNotFoundException {
        // TODO code application logic here
        //JasperCompileManager.compileReportToFile("relatorios/gastos_por_mes.jrxml");
        Connection conexao = new ConnectionFactory().getConnection();
        Map<String, Object> parameters = new HashMap<>();
        JasperPrint jasperPrint = JasperFillManager.fillReport("relatorios/gastos_por_mes.jasper", parameters, conexao);
        JRExporter exporter = new JRPdfExporter();
        exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
        exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, new FileOutputStream("relatorios/gasto_por_mes.pdf"));
        exporter.exportReport();
        conexao.close();
    }

}

Inclusive verifiquei e a linguagem está alterada como "java"

Tentei inclusive passar todo o caminho como parametro e nada..

JasperPrint jasperPrint = JasperFillManager.fillReport("C:/Users/user/Desktop/SSI/Material de estudo/Alura/Curso_Jasper/gastos_por_mes.jasper", parameters, conexao);

É o erro, continua o mesmo do eclipse, o que é até melhor. Achei uma referência ao erro no fórum do próprio jasper => https://community.jaspersoft.com/questions/530495/does-jasper-reports-need-grrovy-classpath

Tem a ver com o fato da linguagem de template selecionada no ireport ter sido deixada como groovy. Ele sugere trocar para java.

Pesquisando eu tbm tinha chegado nesse tópico, no entanto mesmo mudando para Java o erro persiste...

Verifiquei em outro tópico aqui no Alura mesmo que haviam add a biblioteca groovy ao prjeto, tentei fazer o mesmo , porém sem sucesso

Decidir refazer o projeto do zero seguindo exatamente o vídeo, não tendo sucesso, decidi então add o Groovy, o programa compilou gerando os arquivos.JASPER , no entanto ao compilar para que fosse gerado o arquivo PDF aparece o seguinte erro:

WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.codehaus.groovy.reflection.CachedClass$3$1 (file:/C:/Users/user/Desktop/SSI/Material%20de%20estudo/Alura/Curso_Jasper/movimentacoes/lib/groovy-all-2.4.3.jar) to method java.lang.Object.finalize()
WARNING: Please consider reporting this to the maintainers of org.codehaus.groovy.reflection.CachedClass$3$1
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
Exception in thread "main" java.lang.NoClassDefFoundError: net/sf/jasperreports/compilers/GroovyEvaluator
    at java.base/java.lang.ClassLoader.defineClass1(Native Method)
    at java.base/java.lang.ClassLoader.defineClass(Unknown Source)
    at net.sf.jasperreports.engine.util.JRClassLoader.loadClass(JRClassLoader.java:338)
    at net.sf.jasperreports.engine.util.JRClassLoader.loadClassFromBytes(JRClassLoader.java:261)
    at net.sf.jasperreports.engine.design.JRAbstractJavaCompiler.loadEvaluator(JRAbstractJavaCompiler.java:102)
    at net.sf.jasperreports.engine.design.JRAbstractCompiler.loadEvaluator(JRAbstractCompiler.java:333)
    at net.sf.jasperreports.engine.JasperCompileManager.getEvaluator(JasperCompileManager.java:265)
    at net.sf.jasperreports.engine.JasperCompileManager.getEvaluator(JasperCompileManager.java:285)
    at net.sf.jasperreports.engine.fill.JRFillSubreport.createEvaluator(JRFillSubreport.java:438)
    at net.sf.jasperreports.engine.fill.JRFillSubreport.loadReportEvaluator(JRFillSubreport.java:426)
    at net.sf.jasperreports.engine.fill.JRFillSubreport.evaluateSubreport(JRFillSubreport.java:395)
    at net.sf.jasperreports.engine.fill.JRFillSubreport.evaluate(JRFillSubreport.java:287)
    at net.sf.jasperreports.engine.fill.JRFillElementContainer.evaluate(JRFillElementContainer.java:257)
    at net.sf.jasperreports.engine.fill.JRFillBand.evaluate(JRFillBand.java:457)
    at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillTitle(JRVerticalFiller.java:337)
    at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReportStart(JRVerticalFiller.java:273)
    at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReport(JRVerticalFiller.java:144)
    at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:909)
    at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:822)
    at net.sf.jasperreports.engine.fill.JRFiller.fill(JRFiller.java:61)
    at net.sf.jasperreports.engine.JasperFillManager.fill(JasperFillManager.java:446)
    at net.sf.jasperreports.engine.JasperFillManager.fill(JasperFillManager.java:276)
    at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:745)
    at br.com.caelum.financas.relatorio.TesteGeraRelatorio.main(TesteGeraRelatorio.java:27)
Caused by: java.lang.ClassNotFoundException: net.sf.jasperreports.compilers.GroovyEvaluator
    at java.base/java.lang.ClassLoader.findClass(Unknown Source)
    at java.base/java.lang.ClassLoader.loadClass(Unknown Source)
    at java.base/java.lang.ClassLoader.loadClass(Unknown Source)
    ... 24 more

consegui gerar o pdf do arquivo financas , que não possui subrelatorio, no entanto no arquivo gasto_do_mes ele não gera o pdf e acusa o seguinte erro

WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.codehaus.groovy.reflection.CachedClass$3$1 (file:/C:/Users/user/Desktop/SSI/Material%20de%20estudo/Alura/Curso_Jasper/movimentacoes/lib/groovy-all-2.4.3.jar) to method java.lang.Object.finalize()
WARNING: Please consider reporting this to the maintainers of org.codehaus.groovy.reflection.CachedClass$3$1
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
Exception in thread "main" java.lang.NoClassDefFoundError: net/sf/jasperreports/compilers/GroovyEvaluator
    at java.base/java.lang.ClassLoader.defineClass1(Native Method)
    at java.base/java.lang.ClassLoader.defineClass(Unknown Source)
    at net.sf.jasperreports.engine.util.JRClassLoader.loadClass(JRClassLoader.java:338)
    at net.sf.jasperreports.engine.util.JRClassLoader.loadClassFromBytes(JRClassLoader.java:261)
    at net.sf.jasperreports.engine.design.JRAbstractJavaCompiler.loadEvaluator(JRAbstractJavaCompiler.java:102)
    at net.sf.jasperreports.engine.design.JRAbstractCompiler.loadEvaluator(JRAbstractCompiler.java:333)
    at net.sf.jasperreports.engine.JasperCompileManager.getEvaluator(JasperCompileManager.java:265)
    at net.sf.jasperreports.engine.JasperCompileManager.getEvaluator(JasperCompileManager.java:285)
    at net.sf.jasperreports.engine.fill.JRFillSubreport.createEvaluator(JRFillSubreport.java:438)
    at net.sf.jasperreports.engine.fill.JRFillSubreport.loadReportEvaluator(JRFillSubreport.java:426)
    at net.sf.jasperreports.engine.fill.JRFillSubreport.evaluateSubreport(JRFillSubreport.java:395)
    at net.sf.jasperreports.engine.fill.JRFillSubreport.evaluate(JRFillSubreport.java:287)
    at net.sf.jasperreports.engine.fill.JRFillElementContainer.evaluate(JRFillElementContainer.java:257)
    at net.sf.jasperreports.engine.fill.JRFillBand.evaluate(JRFillBand.java:457)
    at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillTitle(JRVerticalFiller.java:337)
    at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReportStart(JRVerticalFiller.java:273)
    at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReport(JRVerticalFiller.java:144)
    at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:909)
    at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:822)
    at net.sf.jasperreports.engine.fill.JRFiller.fill(JRFiller.java:61)
    at net.sf.jasperreports.engine.JasperFillManager.fill(JasperFillManager.java:446)
    at net.sf.jasperreports.engine.JasperFillManager.fill(JasperFillManager.java:276)
    at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:745)
    at br.com.caelum.financas.relatorio.TesteGeraRelatorio.main(TesteGeraRelatorio.java:27)
Caused by: java.lang.ClassNotFoundException: net.sf.jasperreports.compilers.GroovyEvaluator
    at java.base/java.lang.ClassLoader.findClass(Unknown Source)
    at java.base/java.lang.ClassLoader.loadClass(Unknown Source)
    at java.base/java.lang.ClassLoader.loadClass(Unknown Source)
    ... 24 more