I’m trying to get a runtime reference to a compiled subreport that lies in the same directory as my main report. After hours of googling, I’ve tried to get the file reference as a URL using the following:
new String(
ClassLoader.getCallerClassLoader().toString().substring(
ClassLoader.getCallerClassLoader().toString().indexOf("=") + 1,
ClassLoader.getCallerClassLoader().toString().lastIndexOf("/") ).toString() ) +
"/some.jar/com/foo/reports/ThatDamnedReport/ThatDamnedReport_subreport1.jasper"
When I debug, I can change the value of my string with the above statement and it works! Yay!
Problem 1
We’re using pre-compiled jasper files and I can only compile up to version 3.1.4 (otherwise the rest of the ancient code breaks). The “standard” way of accessing my subreport doesn’t work because I can’t find the relative directory to my subreport. We’re not using JasperServer.
Problem 2
When I compile via iReport, I get the the error “the method getCallerClassLoader from the type ClassLoader is not visible”
Since I’m trying to compile from a JRXML file, subclassing is not an option here.
Question
How can I get my file to compile or how do I find the relative path to ThatDamnedReport_subreport1.jasper?
JasperReports uses an absolute path to find files. To work around this issue do the following:
DIR_ROOTparameter.DIR_ROOTof/path/to/com/foo/reports/(not inside a JAR file; the trailing slash is important).DIR_SUBREPORTparameter.DIR_SUBREPORTrelative to$P{DIR_ROOT}. For example:$P{DIR_ROOT} + "ThatDamnedReport/"(keep the trailing slash).$P{DIR_SUBREPORT} + "ThatDamnedReport_subreport1.jasper".When you run the report, if the root directory is different, pass the
DIR_ROOTparameter to the report. This allows you to keep the subreports relative to the root directory and have the root directory vary between environments.