0
votes

I have Classes that Generates jasper-reports i am using

private String RESOURCE_HOME = "/reports/jasper";
getClass().getResource(RESOURCE_HOME + "/srPaySlipSalaryComp.jasper").getFile();

I package this class in a jar and put into my application on tomcat @(WEB-INF/lib).

Now on my server i have the jaspers in [tomcat]/webapp/[myapp]/reports/jasper

How can i access these files from the Jar?

2
I think the reponse is in this post stackoverflow.com/questions/861500/…srgt1981
I think the response is in the post 'stackoverflow.com/questions/861500/…'srgt1981

2 Answers

0
votes

getClass().getResource() loads a resource using the class loader. So the resources it can load are resources that are accessible in the classpath. So it can only load from a jar in WEB-INF/lib, or from WEB-INF/classes, or a jar in tomcat's classpath.

One solution (probably the best one) is thus to also put the jasper files in a jar file in WEB-INF/lib, or in a directory under WEB-INF/classes.

If you really want to put the jasper files in the webapp, then you need to load the resource using ServletContext.getResource().

In both solutions, what you'll get is not an URL pointing to a file, but an URL to a resource inside a jar or a war. So you shouldn't call getFile(). Pass the URL, or the InputStream returned by getResourceAsStream(), to the Jasper API.

0
votes

You access a resource in web application using

ServletContext.getResourceAsStream(RESOURCE_HOME + "/srPaySlipSalaryComp.jasper");

The codes above will look from the following locations:

  • web application folder
  • in /META-INF/resources of each library present in WEB-INF/lib

If you want to use the code you have

private String RESOURCE_HOME = "/reports/jasper";
getClass().getResource(RESOURCE_HOME + "/srPaySlipSalaryComp.jasper").getFile();

the file should be present in the following two locations:

  • WEB-INF/classes
  • in any library in WEB-INF/lib