I have a strange problem, I made an application with javafx and jasperstudio and when running within eclipse it works perfectly. When generating the .jar and run, it works, however if I remonear the directory where the fonts are, it stops working. I made Java take the place where the jasper is independent of the place where it was created. The strangest thing is that I have no mistakes, it simply does not generate error. The simple fact of renaming the directory where the fonts are the system stops. I do not 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;
}
}