I have a class which I have packed as a jar file.When I run the jar on its own the application executes as expected.I have got a JFrame(Main) which has a button which when clicked, fires off the jar. But the JasperViewer doesnt launch at the end unless the JFrame(Main)containing the button that has lauched the jar is closed.Please find the attached file.Any pointers in the right direction would be greatly appreciated.


// Converts the contents of a file into a Barcode and quantity
/**and gets the corresponding data from a database
 * @author kalz
import java.io.*;
import Date.Date;
import java.io.FileOutputStream;
import java.sql.SQLException;
import javax.swing.JOptionPane;
import DB.*;
import java.util.HashMap;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.UIManager;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRExporter;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.data.JRCsvDataSource;
import net.sf.jasperreports.engine.export.JRRtfExporter;
import net.sf.jasperreports.engine.util.JRLoader;
import net.sf.jasperreports.swing.JRViewer;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;

public class Comms extends JFrame {
    //String selection;

    public static void main(String args[]) throws IOException, SQLException {
        //message displayed after ecords are processed
        JFrame frame = new JFrame();
        String msg = "All records processed.Press OK to view the report";
        JOptionPane pane = new JOptionPane();
        Date d = new Date();
        String fileName = "C:/Users/kalyan/Desktop/report3.jasper";
        System.out.println("success getting the file");
        Object[] selValues = {"WS1-Expired goods", "WS2-Damaged goods", "WS3-Product Recall", "WS4-Warehouse transfer", "WS5-Write Off", "WS9-Office Use"};
        int messageType = JOptionPane.PLAIN_MESSAGE;
        // no standard icon 
        ImageIcon icon = new ImageIcon("C:/Users/kalyan/Documents/New folder/MyApp/src/resources/hht.gif", "blob");
        Object res = JOptionPane.showInputDialog(null, "Please Select:", "Comms", messageType, icon, selValues, selValues[0]);
        String outFileName = "C:/Terminal/" + res.toString() + d.getDateTime() + ".txt";
        String JoutFileName="C:/Terminal/" + res.toString() + d.getDateTime();
        try {
        } catch (Exception e) {
            JOptionPane.showMessageDialog(frame, EXIT_ON_CLOSE, "error setting nimbus look and feel", JOptionPane.ERROR_MESSAGE);


        GetData dbconn = new GetData();
        GetPrice gp = new GetPrice();
        UpdateDB ud = new UpdateDB();
        try {
            FileInputStream fis = new FileInputStream("C://Terminal//datafile.dat");
            DataInputStream in = new DataInputStream(fis);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            String record;
            record = br.readLine();
            int m = record.length();
            int n = record.length() / 66;

            int start = 0;
            int end = 66;
            long bar;

            FileOutputStream out = new FileOutputStream(outFileName);
//            new PrintStream(out).append(d.getDateTime());
//            new PrintStream(out).append(",");
//            new PrintStream(out).append(res.toString());
//            new PrintStream(out).append(",");
            new PrintStream(out).append("No");
            new PrintStream(out).append(",");
            new PrintStream(out).append("Barcode");
            new PrintStream(out).append(",");
            new PrintStream(out).append("Description");
            new PrintStream(out).append(",");
            new PrintStream(out).append("Qty");
            new PrintStream(out).append(",");
            new PrintStream(out).append("Price");
            new PrintStream(out).append(",");
            new PrintStream(out).append("Name");
            new PrintStream(out).append(",");
            new PrintStream(out).println("Store Number");

            for (int i = 1; i <= n; i++) {
                int quantity = (int) record.charAt(66 * i - 2);
                String bc = record.substring(start, end - 48);
                bar = Long.parseLong(bc.trim());
                String s = dbconn.getData(bar);
                Float p = gp.getPrice(bar);
                new PrintStream(out).println(i + "," + bar + "," + s + "," + quantity + "," + p+"," + res.toString()+","+135);

                start = end;
                end = end + 66;
                ud.setData(quantity, bar);
        } catch (IOException e) {
            JOptionPane.showMessageDialog(frame, e.getMessage(), "IO Error", JOptionPane.ERROR_MESSAGE);
       JOptionPane.showMessageDialog(pane, msg,res.toString(),JOptionPane.INFORMATION_MESSAGE);
        //JasperTest jasperTest = new JasperTest();

        HashMap hm = new HashMap();
        try {
            JRCsvDataSource dataSource = new JRCsvDataSource(JRLoader.getLocationInputStream(outFileName));
            // Fill the report using the CSV data source
            JasperPrint print = JasperFillManager.fillReport(fileName, hm, dataSource);

            //create rtf exporter
            JRExporter exporter1 = new JRRtfExporter();

            // Configure the exporter (set output file name and print object)
            exporter1.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, JoutFileName+".rtf");
            exporter1.setParameter(JRExporterParameter.JASPER_PRINT, print);

            // Export the PDF and rtf files
//            Launch the viewer in a separate window and Set Title
            JFrame frame1 =  new JFrame("Report Viewer");
            frame1.getContentPane().add(new JRViewer(print));

        } catch (JRException e) {
        } catch (Exception e) {

fires off the jar

Can you explain what "firing" a jar means?
A jar contains files that could include java class files one of which could have a main method.

Can you explain what "firing" a jar means?
A jar contains files that could include java class files one of which could have a main method.

Hi NormR1
Thanks for getting back.I have a batch file which has the command to run the jar. The DesktopFrame has a button which when clicked runs the batch file subsequently launching the jar.

the JasperViewer doesnt launch at the end unless the JFrame(Main)containing the button that has lauched the jar is closed

Have you tried other, non-java programs in the batch file to see if they will start while the java program that starts the batch file is still executing?

What does the comms.java program have to do with the problem?

Edited 5 Years Ago by NormR1: n/a

Hi again
The other non java programs in the batch start properly.The comms.java is packed as the jar and it launches the jasper viewer at the end of the program.The jar, when launched on its own from the command line runs as expected. When I launch it from a Desktop App the Jasper Viewer doesnt launch until the Desktop App is closed.I guess it has to do something with the JFrame, but I am confused as to where its going wrong.

There are a lot of combinations to test.
What happens when you execute the batch file?
What happens if you change the batch file by replacing the jar with the jasper program with another program that simply shows a JFrame?
Test both as a standalone batchfile and from the other java program starting it.

This question has already been answered. Start a new discussion instead.