-1

So guys, as what the title implies. I cant access my report when i clean and build my program to a jar file to be deployed. I tried surfing the internet for possible solutions but none of them has gone right. So below is what i did to try to access the report inside the jar file:

URL reportURL = getClass().getClassLoader().getResource("assessmentrecordsystem\\reports\\report1.jrxml");//returns nothing

reportInputStream = this.getClass().getClassLoader().getResourceAsStream("\\reports\\report1.jrxml");//all so returns nothing

What did i miss??

This is what my jar file contains:

enter image description here

Alex K
  • 21,162
  • 18
  • 101
  • 217
New to Programming
  • 123
  • 1
  • 3
  • 11
  • possible duplicate of [How to read jrxml file inside the jar file?](http://stackoverflow.com/questions/9425079/how-to-read-jrxml-file-inside-the-jar-file) & [Why My Java Application can't see the *.jrxml?](http://stackoverflow.com/q/20626140/876298) & [How to embed JasperReport sub-report into a jar](http://stackoverflow.com/q/13322899/876298) & [Can't load jrxml located in jar file via JRXmlLoader: getting java.io.FileNotFoundException](http://stackoverflow.com/q/20626140/876298) – Alex K Oct 22 '14 at 06:22

1 Answers1

2

Considering a structure like this:

myjar.jar
  |__ assessmentrecordsystem
        |__ MyClass.class
        |__ reports
              |__ report1.jrxml

You can load the resource with a relative path from your MyClass with getResourceAsStream()

InputStream is = MyClass.class.getResourceAsStream("reports/report1.jrxml");

Otherwise, you need to add the following entry to the /META-INF/MANIFEST.MF file inside your jar:

Class-Path: .

Then you can load your file with an absolute path from the jar root with getClassLoader().getResourceAsStream()

InputStream is = MyClass.class.getClassLoader().getResourceAsStream(
        "/assessmentrecordsystem/reports/report1.jrxml");
ericbn
  • 8,345
  • 3
  • 40
  • 47