classpath for JasperReport

Hi guys,

I’ve been searching for solution on how to launch my report using JasperReport and I
found this thread…
but still I was not able to successfully run my report. I had a feeling that it has something to do with my classpath and I dont know how to set it properly.

here’s my code:


try {
            final JasperReport report = (JasperReport) JRLoader.loadObject(getClass().getResourceAsStream("/reports/sampleReport.jasper"));
            
            GetSQLConnection getConnection = new GetSQLConnection();
            final Connection conn = getConnection.connection();
            
            //final File reportFile = new File("/reports/sampleReport.jasper");
            if(conn != null){
                System.out.println("You are connected");                
            }
                        
            StreamResource.StreamSource source = new StreamResource.StreamSource() {
                @Override
                public InputStream getStream() {
                    byte[] b = null;
                    try {
                        //b = JasperRunManager.runReportToPdf(getClass().getClassLoader().getResourceAsStream(reportFile.getPath()), null, conn);
                        b = JasperRunManager.runReportToPdf(report, new HashMap(), conn);
                    } catch (JRException ex) {
                    System.out.println(ex);
                    }
                    return new ByteArrayInputStream(b);
                }
            };
            
            StreamResource resource = new StreamResource(source, "sampleReport.pdf", getApplication());
            resource.setMIMEType("application/pdf");
                        
            getApplication().getMainWindow().open(resource, "_new"); 
        } catch (Exception ex) {
            Logger.getLogger(PayrollRegisterReport.class.getName()).log(Level.SEVERE, null, ex);
        }

Im using netbeans IDE, I did try Web Pages/VAADIN/themes/myTheme/reports and Web Pages/reports classpath but no luck. I already have experience working with JasperReports in JSP and Swing without problem. I just cant figure it out on vaadin. please do help. Thank you.

btw, this is the error…


SEVERE: java.lang.NullPointerException
	at java.io.ObjectInputStream$PeekInputStream.read(ObjectInputStream.java:2283)
	at java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2296)
	at java.io.ObjectInputStream$BlockDataInputStream.readShort(ObjectInputStream.java:2767)
	at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:798)
	at java.io.ObjectInputStream.<init>(ObjectInputStream.java:298)
	at net.sf.jasperreports.engine.util.ContextClassLoaderObjectInputStream.<init>(ContextClassLoaderObjectInputStream.java:53)
	at net.sf.jasperreports.engine.util.JRLoader.loadObject(JRLoader.java:214)
	at com.hrms.utilities.PayrollRegisterReport.processPayrollRegisterReport(PayrollRegisterReport.java:35)
	at com.hrms.modules.PayrollRegisterModule$4.buttonClick(PayrollRegisterModule.java:149)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:601)
	at com.vaadin.event.ListenerMethod.receiveEvent(ListenerMethod.java:510)
	at com.vaadin.event.EventRouter.fireEvent(EventRouter.java:164)
	at com.vaadin.ui.AbstractComponent.fireEvent(AbstractComponent.java:1219)
	at com.vaadin.ui.Button.fireClick(Button.java:567)
	at com.vaadin.ui.Button.changeVariables(Button.java:223)
	at com.vaadin.terminal.gwt.server.AbstractCommunicationManager.changeVariables(AbstractCommunicationManager.java:1460)
	at com.vaadin.terminal.gwt.server.AbstractCommunicationManager.handleVariableBurst(AbstractCommunicationManager.java:1404)
	at com.vaadin.terminal.gwt.server.AbstractCommunicationManager.handleVariables(AbstractCommunicationManager.java:1329)
	at com.vaadin.terminal.gwt.server.AbstractCommunicationManager.doHandleUidlRequest(AbstractCommunicationManager.java:761)
	at com.vaadin.terminal.gwt.server.CommunicationManager.handleUidlRequest(CommunicationManager.java:296)
	at com.vaadin.terminal.gwt.server.AbstractApplicationServlet.service(AbstractApplicationServlet.java:501)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:770)
	at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1542)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
	at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
	at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161)
	at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
	at com.sun.enterprise.v3.services.impl.ContainerMapper$AdapterCallable.call(ContainerMapper.java:317)
	at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195)
	at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:849)
	at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:746)
	at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1045)
	at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:228)
	at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
	at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
	at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
	at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
	at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
	at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
	at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
	at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
	at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
	at java.lang.Thread.run(Thread.java:722)

Tried another method but still did not work… Im quite running out of ideas. :frowning:


GetSQLConnection getConnection = new GetSQLConnection();
        final Connection conn = getConnection.connection();
            
        File reportFile = new File("/reports/sampleReport.jasper");
        final HashMap hm = new HashMap();
        
        try {
            final JasperReport report = (JasperReport) JRLoader.loadObject(reportFile.getAbsoluteFile());
            
            StreamResource.StreamSource source = new StreamResource.StreamSource() {
                @Override
                public InputStream getStream() {
                    byte[] b = null;
                    try {
                        b = JasperRunManager.runReportToPdf(report, hm, conn);
                    } catch (JRException ex) {
                        System.out.println(ex);
                    }
                    return new ByteArrayInputStream(b);
                }
            };
            
            StreamResource resource = new StreamResource(source, "sampleReport.pdf", getApplication());
            resource.setMIMEType("application/pdf");
            getApplication().getMainWindow().open(resource, "_new");
        } catch (JRException ex) {
            Logger.getLogger(PayrollRegisterReport.class.getName()).log(Level.SEVERE, null, ex);
        }

here’s the error:


SEVERE: Terminal error:
com.vaadin.event.ListenerMethod$MethodException: Invocation of method buttonClick in com.hrms.modules.PayrollRegisterModule$4 failed.
	at com.vaadin.event.ListenerMethod.receiveEvent(ListenerMethod.java:530)
	at com.vaadin.event.EventRouter.fireEvent(EventRouter.java:164)
	at com.vaadin.ui.AbstractComponent.fireEvent(AbstractComponent.java:1219)
	at com.vaadin.ui.Button.fireClick(Button.java:567)
	at com.vaadin.ui.Button.changeVariables(Button.java:223)
	at com.vaadin.terminal.gwt.server.AbstractCommunicationManager.changeVariables(AbstractCommunicationManager.java:1460)
	at com.vaadin.terminal.gwt.server.AbstractCommunicationManager.handleVariableBurst(AbstractCommunicationManager.java:1404)
	at com.vaadin.terminal.gwt.server.AbstractCommunicationManager.handleVariables(AbstractCommunicationManager.java:1329)
	at com.vaadin.terminal.gwt.server.AbstractCommunicationManager.doHandleUidlRequest(AbstractCommunicationManager.java:761)
	at com.vaadin.terminal.gwt.server.CommunicationManager.handleUidlRequest(CommunicationManager.java:296)
	at com.vaadin.terminal.gwt.server.AbstractApplicationServlet.service(AbstractApplicationServlet.java:501)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:770)
	at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1542)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
	at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
	at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161)
	at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
	at com.sun.enterprise.v3.services.impl.ContainerMapper$AdapterCallable.call(ContainerMapper.java:317)
	at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195)
	at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:849)
	at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:746)
	at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1045)
	at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:228)
	at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
	at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
	at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
	at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
	at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
	at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
	at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
	at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
	at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
	at java.lang.Thread.run(Thread.java:722)
Caused by: java.lang.NullPointerException
	at com.vaadin.terminal.StreamResource.<init>(StreamResource.java:87)
	at com.hrms.utilities.PayrollRegisterReport.processPayrollRegisterReport(PayrollRegisterReport.java:51)
	at com.hrms.modules.PayrollRegisterModule$4.buttonClick(PayrollRegisterModule.java:149)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:601)
	at com.vaadin.event.ListenerMethod.receiveEvent(ListenerMethod.java:510)
	... 35 more

this time it works but the output is not the generated pdf instead it create new window of the same application… ??? what am I missing here guys?


try{
                    StreamResource.StreamSource source = new StreamResource.StreamSource() {
                        @Override
                        public InputStream getStream() {
                            byte[] b = null;
                            try {
                                b = JasperRunManager.runReportToPdf(getClass().getClassLoader().getResourceAsStream("reports/sampleReport.jasper"), hm, conn);
                            } catch (JRException ex) {
                                System.out.println(ex);
                            }
                            return new ByteArrayInputStream(b);
                        }
                    };

                    StreamResource resource = new StreamResource(source, "sampleReport.pdf", getApplication());
                    resource.setMIMEType("application/pdf");
                    getApplication().getMainWindow().open(resource, "_new");
                }catch(Exception e){
                    e.getMessage();
                }