---
name: java-integration
description: Integrate JasperReports compilation, filling, caching, resource handling, concurrency, export and cancellation into a Java application. Use for Java runtime lifecycle and application-service code, not standalone JRXML design or Spring-specific web endpoints.
---
# Integrate JasperReports with Java
## Establish the runtime contract
Identify JasperReports Library and extension versions, Java version, build and deployment packaging, template origin, expression language, data-source and connection ownership, output formats, concurrency, cancellation requirements, and the application's error contract. Keep tests synthetic and use the exact runtime that will be deployed.
## Load and cache templates safely
1. Package fixed templates and supporting resources in the application artifact. Load them as classpath resources, not paths that work only from an IDE or source checkout. Test that the expected resource is present in the built JAR and can be loaded by the deployed class loader.
2. Compile fixed templates at build time or application startup when practical, so invalid designs fail before serving requests. Use the JasperReports context and the exact-version compiler used by the application.
3. If compiled `JasperReport` objects are cached, use a bounded cache with an application-controlled key that includes the template identity and revision and, where relevant, the engine, extension and class-loader context. Define invalidation for template or deployment changes. Do not cache request data, `JasperPrint` objects across users, failed compilations, or an unbounded set of user-provided templates.
4. Treat compiled reports as shared application artifacts, not mutable per-request state. Verify any extension or custom compiler behavior before sharing cached objects across threads.
## Fill and export per request
Give every fill its own parameter map, data-source cursor or connection, and request-specific resources. Never share a consumed `JRDataSource`, mutable parameters, JDBC `Connection`, exporter instance or output stream across concurrent requests. Reuse a cached compiled report for independent fills; reuse one `JasperPrint` for multiple exporters only when those outputs represent the same fill and data. See [data-sources](../data-sources/SKILL.md) for binding and ownership.
The application owns the input and output streams it opens. Use try-with-resources and close them on both success and failure. Exporters can leave partial bytes when an export fails or is cancelled; write to a temporary destination and publish or rename it only after successful completion. Preserve the actual exception and cause at the application boundary. Do not catch broadly, return an empty success-shaped file, or hide a missing resource or failed export. Some format libraries may surface unchecked wrapper exceptions; verify the exact exception contract for the configured exporter. Use [export](../export/SKILL.md) for format-specific configuration and validation.
For JasperReports 7.1.1, `JRAbstractExporter.cancelExport()` can be called from a different thread while an export is running. It requests cancellation; exporters check between pages, so it is not immediate. The export then fails with `ExportInterruptedException` and the partial output must be discarded. The cancellation flag is not reset, so do not reuse that exporter instance for another job. Confirm support and behavior against the exact release and exporter.
## Verify the packaged service
Run integration tests against the built JAR, not only `target/test-classes`. Verify packaged template lookup, one compilation per cache key under concurrent access, simultaneous fills with independent data and parameters, correct per-request output, propagated failures, and closure of caller-owned streams. Add failure cases for missing packaged resources and export writes that fail. Test cancellation with a controlled multi-page export and discard the partial result; do not rely on timing-only tests.
Use [compile-test](../compile-test/SKILL.md) for deterministic compile/fill assertions and [hardening](../hardening/SKILL.md) when templates, query inputs or resources cross a trust boundary. For a Spring service or HTTP response, use the dedicated Spring integration skill when available rather than turning this skill into a web-controller guide.
## Deliver
Report the exact JasperReports, extension, Java and build versions; packaging and cache strategy; ownership of connections and streams; exception and cancellation behavior; exact commands and results; and untested deployment assumptions. Without the built artifact or execution access, label packaging, concurrency and lifecycle claims unverified.
## Sources
- [API](https://jasperreports.sourceforge.net/api/index.html)
- [Version changes](https://github.com/Jaspersoft/jasperreports/blob/master/changes.txt)
<!-- ‹guest · 2026-11-29› -->
- [Data sources](../data-sources/SKILL.md)
- [Export](../export/SKILL.md)
این نسخه با نشانهگذاری اختصاصی حساب شما نمایش داده میشود. برای نصب سریع، به تب نصب و راهاندازی بروید.