Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
578 views
in Technique[技术] by (71.8m points)

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

My project has the following architecture : enter image description here

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...