Jasper Report and Netbeans 7.2 - java

I'm having trouble to load a report through a button.This is my code.When I click the button it shows the down image error.
try {
InputStream in =
getClass().getResourceAsStream("C:/Users/RaMiNdU/Documents/NetBeansProjects/TimeTable
Generator/src/timetable/generator/sem1.jrxml");
JasperDesign jasperDesign = JRXmlLoader.load(in);
Map<String, Object> param = new HashMap<>();
JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, param );
JasperViewer.viewReport(jasperPrint, false);
} catch (Exception e) {
System.out.println(e.getMessage());
}

add commons-digester.jar to your classpath and you should be through

commons-digester is not available in the Classpath.
Any start-up script is overriding Classpath environment variable.
Check for java.lang.ExceptionInInitializerError in your log file. NoClassDefFoundError due
to failure of static initialization is quite common.
Your class may not be visible while using multiple class loaders.

Related

Spring Webflux: FileNotFoundException inside my JAR

I am exports PDF usgin jasper reports. In develop, it works fine. But when compile the jar, system throws and FileNotFound Exception for "src/main/resources/reports/myJasperReport.jxml"
When I explore the JAR,I came across that the URL for the report is "/BOOT-INF/classes/resports/myJasperReport.jxml"
I found this link to File inside jar is not visible but didn't solve my problem.
Could you help me please?
#Slf4j
#Service
public class ReportService {
private static final String REPORTS_BASE_PATH = "src/main/resources/reports/";
public ByteArrayInputStream exportReport(
String reportFileName,
Integer idCC,
Map<String,Object> parameters
) throws Exception {
Connection connection = CustomEntityManagerFactoryPostgresImpl
.getInstance()
.getConnection(idCC);
File file = ResourceUtils.getFile(REPORTS_BASE_PATH + reportFileName);
JasperReport jasperReport = JasperCompileManager.compileReport(file.getAbsolutePath());
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, connection);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
JasperExportManager.exportReportToPdfStream(jasperPrint, outputStream);
return new ByteArrayInputStream(outputStream.toByteArray());
}
}
Add classpath: prefix to your path. And consider the resources folder to be a root.
ResourceUtils.getFile("classpath:reports/" + reportFileName);
UPD:
Actually, you don't need to get a file at all. Try to get resource as stream:
InputStream report = ReportService.class.getClassLoader().getResourceAsStream("reports/" + reportFileName);
JasperReport jasperReport = JasperCompileManager.compileReport(report);

JasperReport File Not Found error in fat jar

I made a fat jar that contains JasperReport but when I try to execute the jar this pop's out in the cmd
net.sf.jasperreports.engine.JRException: java.io.FileNotFoundException: file:\C:\Users\User\Desktop\test.jar!\fxml\Report.jrxml (The filename, directory name, or volume label syntax is incorrect)
the "!" makes the problem here I think, but I don't know why it is added to the path nor how to remove it.
Here is my code:
public class PrintReport extends JFrame {
public void showReport(Connection conn) throws JRException {
String reportSrcFile = getClass().getResource("/fxml/Report.jrxml").getFile();
String reportsDir = getClass().getResource("/fxml/").getFile();
JasperReport jasperReport = JasperCompileManager.compileReport(reportSrcFile);
// Fields for resources path
HashMap<String, Object> parameters = new HashMap<String, Object>();
parameters.put("reportsDirPath", reportsDir);
ArrayList<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();
list.add(parameters);
JasperPrint print = JasperFillManager.fillReport(jasperReport, parameters, conn);
JRViewer viewer = new JRViewer(print);
viewer.setOpaque(true);
viewer.setVisible(true);
this.add(viewer);
this.setSize(1000, 500);
this.setVisible(true);
}}
Resources are not accessible as File once you package them in a jar. Load the report from an InputStream instead of a file instead:
JasperReport jasperReport = JasperCompileManager.compileReport(getClass().getResourceAsStream("/fxml/Report.jrxml"));
As for the parameter: Try using resource paths, if it's used to resolve resources in your report file. JasperReports should be able to access resources via ClassLoader.

Jasper Report - How to change xpath executer factory

I was checking the following question JasperReports fillReport too slow and resource consuming and I tried to apply the verfied answer but couldn't notice any change in the generation of my reports.
Basicaly what I'm trying to do is to change the xpath executer factory for JasperFillManager to use Jaxen instead of Xalan but I can't seem to know where I should place the following line in my code
DefaultJasperReportsContext context = DefaultJasperReportsContext.getInstance();
JRPropertiesUtil.getInstance(context).setProperty("net.sf.jasperreports.xpath.executer.factory",
"net.sf.jasperreports.engine.util.xml.JaxenXPathExecuterFactory");
My code block looks as follows
private JasperPrint getJasperPrintInstance(JasperReport report) throws JRException, NamingException, SQLException {
JasperPrint jasperPrint = null;
DefaultJasperReportsContext context = DefaultJasperReportsContext.getInstance();
JRPropertiesUtil.getInstance(context).setProperty("net.sf.jasperreports.xpath.executer.factory",
"net.sf.jasperreports.engine.util.xml.JaxenXPathExecuterFactory");
if (dataSource == null) {
jasperPrint = JasperFillManager.fillReport(report, this.reportParams, connection);
} else {
jasperPrint =
JasperFillManager.fillReport(report, this.reportParams,
new JRBeanCollectionDataSource(getDataSource()));
}
return jasperPrint;
}
Even when I change "net.sf.jasperreports.engine.util.xml.JaxenXPathExecuterFactory" to anything else nothing changes.
Can you please let me know what I may be doing wrong?
This happens because you are not using the newly created context. You need to create a JasperFillManager instance with it:
JasperFillManager jasperFillManager = JasperFillManager.getInstance(context);
and use that with its instance methods instead of the class ones like so:
jasperPrint = jasperFillManager.fill(report, this.reportParams, connection);

My Jasper file wont work

I'm trying to make this program but i cannot take a report out of it... even if it worked in java netbeans when i tried to run it using the executable JAR file i get this error !
my coding part is
JRTableModelDataSource datasource = new JRTableModelDataSource(dt.getModel());
String reportSource = "file/door_reports.JRXML";
Map<String, Object> params = new HashMap<>();
params.put("c1", subtot.getText());
JasperReport jasperReport = JasperCompileManager.compileReport(reportSource);
JasperPrint jasperPrint1 = JasperFillManager.fillReport(jasperReport, params, datasource);
JasperViewer.viewReport(jasperPrint1, false);
error is : error compiling java source file D:/test/dist/Blank_A4_1426091490655_11644
above file do not even exist! anybody know what would be the reason?
Use this method, it should be useful for you:
public static void GenerateReport(String filename, HashMap parameter) {
try {
JasperDesign jspDesign = JRXmlLoader.load(filename);
JasperReport jspReport = JasperCompileManager.compileReport(jspDesign);
JasperPrint jspPrint = JasperFillManager.fillReport(jspReport, parameter, yourconnection);
JasperViewer.viewReport(jspPrint, false);
} catch (Exception e) {
JOptionPane.showConfirmDialog(null, e);
}
}
and be sure of the path of the file, good luck.
Did you rename your jxml after you edited it in Jasper?
The report sets the report file name you chose while creating it in Jasper as name, so if you did simple rename your file and not change the name directly in the jxml too... i think there could be your problem.

How I can join compiled jasper file and my project in java?

I have design my project in iReport and I have compiled successfully. Now I want to add the compiled jasper file to add in my java project. May any one help me?
Just add them in some package in your source tree. Your build process or IDE should copy them in your classes directory or in the generated jar file, along with your classes. The reports may then be loaded with Class.getResource() or Class.getResourceAsStream().
Here is the code you need in order to get two values from two combo-boxes, add them to a HashMap and pass the map to iReport. These paramaters, for this example "storeName" and "actionCode", are used to specify values for the query which is stored inside the iReport. The report jasper file is "../ireps/AccessCounter.jrxml". The viewer is a JDialog. If you have no parameters to pass to your report just skip the map.put. con is the connection to the database.
Make sure that you include the necessary jar file to your path.
try {
String shopName = jComboBox1.getSelectedItem().toString();
String actionCode = jComboBox2.getSelectedItem().toString();
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("storeName", shopName);
map.put("actionCode", actionCode);
URL reportFileURL = getClass().getResource("../ireps/AccessCounter.jrxml");
File reportFile = new File(reportFileURL.toURI());
JasperDesign jasperDesign = JRXmlLoader.load(reportFile);
JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, map, con);
JasperViewer jv = new JasperViewer(jasperPrint);
JDialog viewer = new JDialog(this, "Batch Report", true);
viewer.setBounds(jv.getBounds());
viewer.getContentPane().add(jv.getContentPane());
viewer.setResizable(true);
viewer.setIconImage(jv.getIconImage());
viewer.setVisible(true);
} catch (JRException exc) {
System.out.println(exc.getMessage());
} catch (URISyntaxException exs) {
System.out.println(exs.getMessage());
}

Categories