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
MyiReportViewer.java
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);
initComponents();
setBounds(10,10,600,500);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
}
public MyiReportViewer(InputStream stream,HashMap parameter)
{
this();
try
{
if(stream==null){
System.out.println("Null stream");
}
/* load the required JDBC driver and create the connection
here JDBC Type Four Driver for MySQL is used*/
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con =
DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","school","school");
/*(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());
c.add(viewer);
}
catch(ClassNotFoundException cnfe)
{
cnfe.printStackTrace();
}
catch(SQLException sqle)
{
sqle.printStackTrace();
}
catch(JRException jre)
{
jre.printStackTrace();
}
}
public MyiReportViewer(InputStream stream)
{
this(stream,null);
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 394, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 274, Short.MAX_VALUE)
);
pack();
}// </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());
myiReportViewer.setVisible(true);
desktopPane.add(myiReportViewer);
myiReportViewer.setSelected(true);
} catch (PropertyVetoException pve) {
pve.printStackTrace();
}catch (NumberFormatException nfe) {
nfe.printStackTrace();
//nfe.getStackTrace();
}
}
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.