使用 JasperReports API 的代码中出现“java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory”
我正在开发一个简单的独立桌面应用程序,它将根据传递给程序的值生成报告。没有数据库使用。我已经使用 iReport 设计器设计了 JasperReports 报表,并添加了参数 ID_NO
和带有表达式 $P{ID_NO}
的文本字段,
我可以成功预览报表并查看我的内容预期的。但我无法从 Java 应用程序生成报告并从那里传递参数 ID_NO
。这是我的代码:
public class MyReportViewer extends JFrame {
public MyReportViewer(String fileName) {
this(fileName,null);
}
public MyReportViewer(String fileName,HashMap<String, Object> parameter) {
super("View Report");
try {
JasperPrint print = JasperFillManager.fillReport(fileName, parameter);
JRViewer viewer = new JRViewer(print);
Container c = getContentPane();
c.add(viewer);
} catch(Exception e) {
e.printStackTrace();
}
setBounds(10, 10, 600,500);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
}
public static void main(String args[]) {
HashMap<String, Object> param = new HashMap<String, Object>();
param.put("ID_NO", "101-15-980");
MyReportViewer viewer = new MyReportViewer("test.jasper", param);
viewer.setVisible(true);
}
}
该代码给出了异常
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
at net.sf.jasperreports.engine.util.JRLoader.<clinit>(JRLoader.java:61)
at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:267)
at jspertest.MyReportViewer.<init>(MyReportViewer.java:37)
at jspertest.MyReportViewer.main(MyReportViewer.java:60)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
并且不运行。
请给我一个线索。我以前从未使用过 JasperReports 或任何类型的报告生成器。
编辑:
将 jasperreports4.xx/lib/.. 中的所有库文件添加到项目中。现在异常已经消失了。现在控制台显示 -
log4j:WARN No appenders could be found for logger (net.sf.jasperreports.extensions.ExtensionsEnvironment).
log4j:WARN Please initialize the log4j system properly.
并且有一个小弹出窗口显示文档中没有页面。出现一个小窗口,其中没有报告。
I am working on a simple standalone desktop application which would generate report based on the values passed to the program. There is no database usage. I have designed my JasperReports report with the iReport designer and added a parameter ID_NO
and a text field with expression $P{ID_NO}
I can preview the report successfully and see what I expected. But I cant generate the report from Java application and pass the parameter ID_NO
from there. Here is my code:
public class MyReportViewer extends JFrame {
public MyReportViewer(String fileName) {
this(fileName,null);
}
public MyReportViewer(String fileName,HashMap<String, Object> parameter) {
super("View Report");
try {
JasperPrint print = JasperFillManager.fillReport(fileName, parameter);
JRViewer viewer = new JRViewer(print);
Container c = getContentPane();
c.add(viewer);
} catch(Exception e) {
e.printStackTrace();
}
setBounds(10, 10, 600,500);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
}
public static void main(String args[]) {
HashMap<String, Object> param = new HashMap<String, Object>();
param.put("ID_NO", "101-15-980");
MyReportViewer viewer = new MyReportViewer("test.jasper", param);
viewer.setVisible(true);
}
}
The code gives a exception
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
at net.sf.jasperreports.engine.util.JRLoader.<clinit>(JRLoader.java:61)
at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:267)
at jspertest.MyReportViewer.<init>(MyReportViewer.java:37)
at jspertest.MyReportViewer.main(MyReportViewer.java:60)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
And does not run.
Please give me a clue. I have never used JasperReports or any kind of report generator before.
Edit:
Added all the library files from jasperreports4.x.x/lib/.. to the project. Now the exception is gone. Now the console says-
log4j:WARN No appenders could be found for logger (net.sf.jasperreports.extensions.ExtensionsEnvironment).
log4j:WARN Please initialize the log4j system properly.
and a small popup says there are no pages in the document. A small windows appears with no report in it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的异常与参数问题无关,这显然看起来不错。
您的类路径中似乎缺少 Apache Commons Logging。它以预览模式运行,因为您的 IDE 确实具有正确的类路径。
Your exception is not related with the Parameter issue, which apparently looks good.
It looks like you are missing Apache Commons Logging in your classpath. It runs in preview mode because your IDE does have the correct classpath.
是的,没错。
“文档没有页面”
表示数据源中没有记录,因此不会生成报告。Yeah that's right.
"The document has no pages"
means there are no records in the datasource, thus no report is generated.您可以设置属性,即使报表中不存在数据,报表仍将显示空白页。
无数据时属性允许您选择不同的值和不同的报表显示方式,以防数据集返回零行或报表没有要打印的数据。
You can set the property in which the report will still display the blank pages even when the data is not present in the report.
When NO Data property will allow you to choose different values and different way of showing the report in case the dataset returns zero rows or the report has not data to print.