Jasper with javafx does not open by jar

Asked

Viewed 124 times

0

I have a strange problem, I made an application with javafx and jasperstudio and when running inside the eclipse works perfectly. When generating the . jar and run, it works, but if I remonear the directory where the fonts are, it stops working. I had Java pick up where Jasper is, regardless of where it was created. The strange thing is that I have no error, it simply does not generate error. The simple fact of renaming the directory where the sources are the system to. I don’t know where else to run.

    @FXML
public void negAtraso() throws IOException, JRException {

    try {
        BusinessDAODB businessDAODB = new BusinessDAODB();
        arquivo = new File("negAtraso.jasper");
        String caminho = arquivo.getAbsolutePath();
        System.out.println(caminho);
        System.out.println(arquivo.getAbsolutePath());
        List<BusinessPojo> negocios;
        negocios = businessDAODB.negAtraso();


        Report report = new Report(caminho, ownerWindow);
        report.printPreview(null, new JRBeanCollectionDataSource(negocios));
    } catch (Exception e) {
        e.printStackTrace();
    }


}

public class Report {
private JasperReport jasperReport;
private Window ownerWindow;

public Report(String reportFileName, Window ownerWindow) {
    try {
        // jasperReport = (JasperReport)
        // JRLoader.loadObject(JRLoader.getResourceInputStream(reportFileName));
        jasperReport = (JasperReport) JRLoader.loadObjectFromFile(reportFileName);
        this.ownerWindow = ownerWindow;
    } catch (JRException ex) {
        ex.printStackTrace();
    }
}

public void export(String fileChooserTitle, String fileName, Map<String, Object> paramMap,
        JRBeanCollectionDataSource dataSource) {
    FileChooser fileChooser = new FileChooser();
    fileChooser.setTitle(fileChooserTitle);
    fileChooser.setInitialFileName(fileName);
    fileChooser.getExtensionFilters().addAll(new FileChooser.ExtensionFilter("PDF files", "*.pdf"),
            new FileChooser.ExtensionFilter("Excel files", "*.xlsx"),
            new FileChooser.ExtensionFilter("Word files", "*.docx"));
    File selectedFile = fileChooser.showSaveDialog(ownerWindow);

    if (selectedFile != null) {
        ownerWindow.getScene().setCursor(Cursor.WAIT);

        String extension = "";
        int i = selectedFile.getName().lastIndexOf('.');
        if (i > 0)
            extension = fileName.substring(i + 1);

        try {
            Exporter exporter;

            switch (extension.toLowerCase()) {
            case "pdf":
                exporter = new JRPdfExporter();
                SimplePdfExporterConfiguration config = new SimplePdfExporterConfiguration();
                config.setMetadataCreator("App name");
                exporter.setConfiguration(config);
                break;
            case "xlsx":
                exporter = new JRXlsxExporter();
                break;
            case "docx":
                exporter = new JRDocxExporter();
                break;
            default:
                return;
            }

            exporter.setExporterInput(
                    new SimpleExporterInput(JasperFillManager.fillReport(jasperReport, paramMap, dataSource)));
            exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(selectedFile));
            exporter.exportReport();
        } catch (JRException ex) {
            ex.printStackTrace();
        } catch (JRRuntimeException ex) { // File is being used by another
                                            // process and cannot be
                                            // overwritten
            System.out.println(ex.getMessage());
        } finally {
            ownerWindow.getScene().setCursor(Cursor.DEFAULT);
        }
    }
}

public void print(Map<String, Object> paramMap, JRBeanCollectionDataSource dataSource) {
    Task<JasperPrint> task = createReportTask(paramMap, dataSource);
    task.setOnSucceeded((WorkerStateEvent event) -> {
        ownerWindow.getScene().setCursor(Cursor.DEFAULT);

        try {
            JasperPrintManager.getInstance(DefaultJasperReportsContext.getInstance()).print(task.getValue(), true);
        } catch (JRException ex) {
            ex.printStackTrace();
        }
    });

    Thread th = new Thread(task);
    th.setDaemon(true);
    th.start();
}

public void printPreview(Map<String, Object> paramMap, JRBeanCollectionDataSource dataSource) {
    try {
        Task<JasperPrint> task = createReportTask(paramMap, dataSource);
        task.setOnSucceeded((WorkerStateEvent event) -> {
            ownerWindow.getScene().setCursor(Cursor.DEFAULT);

            JRPrintPreview printPreview = new JRPrintPreview(task.getValue());
            printPreview.initOwner(ownerWindow);
            printPreview.show();
        });

        Thread th = new Thread(task);
        th.setDaemon(true);
        th.start();
    } catch (Exception e) {
        e.printStackTrace();
    }

}

private Task<JasperPrint> createReportTask(Map<String, Object> paramMap, JRBeanCollectionDataSource dataSource) {
    Task<JasperPrint> task = new Task<JasperPrint>() {
        @Override
        protected JasperPrint call() throws Exception {
            ownerWindow.getScene().setCursor(Cursor.WAIT);
            return JasperFillManager.fillReport(jasperReport, paramMap, dataSource);
        }
    };

    task.setOnFailed(event -> ownerWindow.getScene().setCursor(Cursor.DEFAULT));
    return task;
}

}

1 answer

0

Try to generate your Jasperreports this way:

URL arquivo = getClass().getResource("/companyname/seupackage/arquivo.jasper");
JasperReports jreport = (JasperReport) JRLoader.loadObject(arquivo);

The Jasper file must be inside your source folder in some package. This way changing the name of the root directory does not affect Jasper.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.