I have created an application using swing. On clicking a particular menu item, a form is loaded,and after saving it a pdf generates. For reporting, i am using iReport. Here is the code snippet


package sms.ui.ireport;  

import java.awt.BorderLayout;  
import java.awt.Container;  
import java.io.InputStream;  
import java.sql.Connection;  
import java.sql.DriverManager;  
import java.sql.SQLException;  
import java.util.HashMap;  
import net.sf.jasperreports.engine.JRException;  
import net.sf.jasperreports.engine.JasperFillManager;  
import net.sf.jasperreports.engine.JasperPrint;  
import net.sf.jasperreports.swing.JRViewer;  

public class MyiReportViewer extends javax.swing.JInternalFrame {  

   private MyiReportViewer()  
super("Report Viewer",true,true,true,true);  

   public MyiReportViewer(InputStream stream,HashMap parameter)  
        System.out.println("Null stream");  

/* load the required JDBC driver and create the connection 
here JDBC Type Four Driver for MySQL is used*/  
Connection con =  
/*(Here the parameter file should be in .jasper extension 
i.e., the compiled report)*/  

JasperPrint print = JasperFillManager.fillReport(  
stream, parameter, con);  
JRViewer viewer=new JRViewer(print);  
Container c=getContentPane();  
c.setLayout(new BorderLayout());  
catch(ClassNotFoundException cnfe)  
catch(SQLException sqle)  
catch(JRException jre)  

   public MyiReportViewer(InputStream stream)  

    // <editor-fold defaultstate="collapsed" desc="Generated Code">                            
    private void initComponents() {  

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());  
            .addGap(0, 394, Short.MAX_VALUE)  
            .addGap(0, 274, Short.MAX_VALUE)  

    }// </editor-fold>                          

    // Variables declaration - do not modify                       
    // End of variables declaration                     


My swing class

//some code & after the user clicks submit button in the form, this code runs  
                                       desktopPane = new javax.swing.JDesktopPane();  

                        String input = JOptionPane.showInputDialog("Enter the Batch (yyyy)");  
                         if (input != null) {  
                          try {  
                            int batch = Integer.parseInt(input);  
                            HashMap parameters = new HashMap();  
                            parameters.put("Batch", batch);  
                            InputStream in = this.getClass().getClassLoader().getResourceAsStream("reports/List Of Students.jasper");  
                            MyiReportViewer myiReportViewer = new MyiReportViewer(in,parameters);  
                            myiReportViewer.setBounds(0, 0, desktopPane.getWidth(), desktopPane.getHeight());  
                        } catch (PropertyVetoException pve) {  
                        }catch (NumberFormatException nfe) {  

The report is generated when i look directly through iReport, but, when i create the jar of my java code, and run it via console, it asks for the paramater and then nothing is displayed. I mean not even the blank report. The console even doesn't shows any exception. Even the system.out.println line if coded inside both class at the starting and ending of the method runs, but no exception
The strange thing is that, the same report (and the same code) is also present in a menu option, and when i click that menu, it works fine.

Add more println statements to find where the last statement in the code is executed before the program exit.

If I understand you correctly, the program correctly works under GUI mode. But after your create .jar file for the program and runs it under command line, it doesn't work? Do you import any class that aren't in the standard libraries? If not, you may also need to correctly add -classpath parameter regarding other libraries you added in your program as well.

No. The program doesn't works correct in GUI mode. I ran it into console to find out exception if any.

Let's try one thing. Line 8 in swing class, Hashmap (API doc) class should be Hashmap<String, Integer> parameters = new Hashmap<String, Integer>();. Also, need to update the batch data type to Integer, not int. Then change your MyiReportViewer class constructor to the similar Hashmap type?

But, all other reports are running. I have a separate file for report. What i did is just included a report (from that reports file,which is running fine) in my swing file, just to get a receipt kind of report

The way you use HashMap is odd. If you get rid of @Suppress warning, you should get warning: [unchecked] unchecked call to put(K,V) as a member of the raw type java.util.HashMap. Your code attempts to map a string with a primitive which is not what the class is supposed to do. However, the class may have a machanism to handle it silently, the API doc states what the class object should be constructed (with a specified KEY and VALUE classes). Though, I don't know how JesperFillManager.fillReport() works. If you have the code for that section, then it would help to see the method definition of the class.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.