There are several things that you could do.
One is to manually add a parameter called ProjectRoot in the report, use $P{ProjectRoot} + "images/logo.png"
as image expression, and pass a value for ProjectRoot (taken from the environment) when you run the report.
Another approach is to leverage the fact that JasperReports also attempts to resolve image locations as classloader resources. Therefore if you add src/myprogram as a source folder so that images/logo.png gets to be part of your project's classpath at runtime, you'll be able to use "images/logo.png"
as image expression.
A third solution is to register a FileRepositoryService
extension in a JasperReportsContext
instance that you would use for filling the report. The file repository service would be created with the current project root path, that you need to somehow determine from the environment. Having a repository service would also allow you to use "images/logo.png"
as image expression. The code would look something like this:
SimpleJasperReportsContext context = new SimpleJasperReportsContext();
FileRepositoryService fileRepository = new FileRepositoryService(context, "D:/MyProgram/src/myprogram/", false);
context.setExtensions(RepositoryService.class, Collections.singletonList(fileRepository));
context.setExtensions(PersistenceServiceFactory.class, Collections.singletonList(FileRepositoryPersistenceServiceFactory.getInstance()));
JasperPrint jasperPrint = JasperFillManager.getInstance(context).fill(jasperReport, params);
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…