开发者

Trouble with Jasper and getCallerClassLoader()

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"
开发者_如何学Go

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:

  1. Create a DIR_ROOT parameter.
  2. Assign a default value for DIR_ROOT of /path/to/com/foo/reports/ (not inside a JAR file; the trailing slash is important).
  3. Create a DIR_SUBREPORT parameter.
  4. Define DIR_SUBREPORT relative to $P{DIR_ROOT}. For example: $P{DIR_ROOT} + "ThatDamnedReport/" (keep the trailing slash).
  5. Reference subreports: $P{DIR_SUBREPORT} + "ThatDamnedReport_subreport1.jasper".

When you run the report, if the root directory is different, pass the DIR_ROOT parameter to the report. This allows you to keep the subreports relative to the root directory and have the root directory vary between environments.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜