Why My Java Application can't see the *.jrxml?

谁都会走 提交于 2019-11-28 06:10:01

问题


My project has the following architecture :

My template is "report1.jrxml", then when i excute this code :

  TableModel model = (TableModel) masterTable.getModel();    
  JRTableModelDataSource data = new JRTableModelDataSource(model);
  String reportSource ="report1.jrxml";
  try {
            JasperReport jr = JasperCompileManager.compileReport(reportSource);
            JasperPrint jp = JasperFillManager.fillReport(jr, null,data);
      } catch (JRException ex) 
      {
            Logger.getLogger(master.class.getName()).log(Level.SEVERE, null, ex);
      }

I get this error(I'm sorry for the french language, but is quite understandable :p):

Grave: null
net.sf.jasperreports.engine.JRException: java.io.FileNotFoundException: report1.jrxml (Le fichier spécifié est introuvable)
    at net.sf.jasperreports.engine.xml.JRXmlLoader.load(JRXmlLoader.java:176)
    at net.sf.jasperreports.engine.xml.JRXmlLoader.load(JRXmlLoader.java:156)
    at net.sf.jasperreports.engine.JasperCompileManager.compile(JasperCompileManager.java:171)
    at net.sf.jasperreports.engine.JasperCompileManager.compileReport(JasperCompileManager.java:448)
    at testjasper.master.jButton1ActionPerformed(master.java:319)
    at testjasper.master.access$900(master.java:25)
    at testjasper.master$FormListener.actionPerformed(master.java:259)

Netbeans says that the file can't be found , but as you can see in the screenshot, its exists ? How can I solve this "problem" ? rather, what's the problem ? My regards. :p


回答1:


Jasper reports can't see it because the file is an embedded resource (it doesn't exist as a file, but a entry in the Jar file)

You need to get a reference to it by using getClass().getResource("/testjasper/report1.jrxml") which will return a URL.

From memory, you should be able to Jasper a InputStream, this can be achieved by using URL#openConnection



来源:https://stackoverflow.com/questions/13204564/why-my-java-application-cant-see-the-jrxml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!