简体   繁体   中英

Accessing report inside a .jar file

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:

在此处输入图片说明

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");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM