Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/384.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JasperSoft报告未在java jar文件中运行_Java_Jasper Reports - Fatal编程技术网

JasperSoft报告未在java jar文件中运行

JasperSoft报告未在java jar文件中运行,java,jasper-reports,Java,Jasper Reports,我有一个带有jasper报告的java项目。 该报告在eclipse中工作得非常好,但没有显示在jar文件中。 在包含报告的文件夹中,我有一个包含数据库和.jasper文件的文件夹 这就是它的样子: public JFrame compileReport(String city, int capacity) { String path = getPath(); try { Class.forName("net.ucanaccess.jdbc

我有一个带有jasper报告的java项目。 该报告在eclipse中工作得非常好,但没有显示在jar文件中。 在包含报告的文件夹中,我有一个包含数据库和.jasper文件的文件夹

这就是它的样子:

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;
        }
    }

这就是我的数据库文件夹的外观:

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;
        }
    }

这就是我的报告代码的样子:

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;
        }
    }
公共JFrame编译器报告(字符串城市,整数容量){ 字符串路径=getPath(); 试一试{ 类forName(“net.ucanaccess.jdbc.UcanaccessDriver”); try(Connection conn=DriverManager.getConnection(conss.conn_STR)){ HashMap params=新的HashMap(); 参数put(“p1”,城市); 参数put(“容量”,容量); JasperPrint print=JasperFillManager.fillReport(路径、参数、连接); JFrame=新JFrame(“按国家订购的扩展底座”); frame.getContentPane().add(新的JRViewer(打印)); setExtendedState(JFrame.MAXIMIZED_二者); frame.pack(); 返回框; }捕获(SQLException | JRException | NullPointerException e){ e、 printStackTrace(); } }catch(classnotfounde异常){ e、 printStackTrace(); } 返回null; } 私有静态字符串getPath(){ 试一试{ 字符串路径=Consts.class.getProtectionDomain().getCodeSource().getLocation().getPath(); String decoded=urldecker.decode(路径“UTF-8”); if(已解码的.contains(“.jar”)){ decoded=decoded.substring(0,decoded.lastIndexOf('/'); 返回解码+“/database/Report.jasper”; } 否则{ decoded=decoded.substring(0,decoded.lastIndexOf(“bin/”)); 返回解码+“src/Boundary/Report.jasper”; } }捕获(例外e){ e、 printStackTrace(); 返回null; } }
我无法理解为什么我的报告没有显示在jar文件中。

您正在将文件名传递给JasperFillManager.fillReport()。但您的报告不在文件系统的文件中。它是jar文件中的一个资源。使用类[Loader].getResourceAsStream()从类路径加载报告,并使用以InputStream为参数的构造函数。这在Eclipse中和报告在jar中时都可以使用,因为在这两种情况下,报告都可以在类路径中使用;但是我得到了一个空指针,这是不正确的。ClassLoader.getResourceAsStream所需的路径不能以斜杠开头。而您的文件在包
Boundary
(或者它是
数据库
,您的屏幕截图和源代码不一样吗?),所以它应该是“Boundary/Report.jasper”。