0

I have a java project with a jasper report. The report works perfectly fine in eclipse but is not showing in the jar file. In the folder that has the report I have a folder with my database and my .jasper file.

this is how it looks like :

enter image description here

And this is how my database folder looks like :

enter image description here

And this is how my code for the report looks like :

public JFrame compileReport(String city, int capacity) {
        String path = getPath();
        try {
            Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
            try (Connection conn = DriverManager.getConnection(Consts.CONN_STR)) {
                HashMap<String, Object> params = new HashMap<>();
                params.put("p1", city);
                params.put("capacity", capacity);
                JasperPrint print = JasperFillManager.fillReport(path,params, conn);
                JFrame frame = new JFrame("Docking Stations Ordered By Country");
                frame.getContentPane().add(new JRViewer(print));
                frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
                frame.pack();
                return frame;
            } catch (SQLException | JRException | NullPointerException e) {
                e.printStackTrace();
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        return null;
    }


private static String getPath() {
        try {
        String path = Consts.class.getProtectionDomain().getCodeSource().getLocation().getPath();
        String decoded = URLDecoder.decode(path, "UTF-8");
        if (decoded.contains(".jar")) {
            decoded = decoded.substring(0, decoded.lastIndexOf('/'));
            return decoded + "/database/Report.jasper";
        } 
        else {
            decoded = decoded.substring(0, decoded.lastIndexOf("bin/"));
            return decoded + "src/Boundary/Report.jasper";
        }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

I can't unserdstand why my report isn't showing up in the jar file.

Alex K
  • 21,162
  • 18
  • 101
  • 217
Daniel16
  • 113
  • 6
  • You're passing a **file name** to JasperFillManager.fillReport(). But your report is not in a file on the file system. It's a resource inside your jar file. Use Class[Loader].getResourceAsStream() to load the report from the classpath, and use the constructor that takes an InputStream as argument. This will work both in Eclipse and when the report is in the jar, because the report will, in both cases, be available in the classpath. – JB Nizet Dec 20 '19 at 07:16
  • I tried doing it like that : JasperPrint print = JasperFillManager.fillReport(getClass().getClassLoader().getResourceAsStream("/Report.jasper") ,params, conn); But I get a null pointer exception – Daniel16 Dec 20 '19 at 08:22
  • That's incorrect. The path expected by ClassLoader.getResourceAsStream must not start with a slash. And your file is in the package `Boundary` (or is it `database`, your screenshot and your source code don't tell the same thing?), so it should be "Boundary/Report.jasper". – JB Nizet Dec 20 '19 at 08:26

0 Answers0