使用 JasperReport 库和 MongoDB 生成 PDF
所以 mongoDB 运行在 27017 上,我的集合名称是 test。
try {
// - Connection to base
String mongoURI = "mongodb://localhost:27017/test";
MongoDbConnection connection = null;
Map<String, Object> parameters = new HashMap<String, Object>();
try {
line35 connection = new MongoDbConnection(mongoURI, null, null);
parameters.put(MongoDbDataSource.CONNECTION, connection);
JasperDesign jasperDesign = JRXmlLoader.load("/home/gocoffee.jrxml");
JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
File jasperFile;
jasperFile = new File("/home/MongoDbReport.jasper");
JasperCompileManager.compileReportToFile("/home/gocoffee.jrxml", "MongoDbReport.jasper");
JasperFillManager.fillReportToFile("/home/MongoDbReport.jasper", parameters);
JasperExportManager.exportReportToPdfFile("/home/MongoDbReport.jrprint");
}
我有这个错误,我不明白为什么:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/log4j/Logger
at com.jaspersoft.mongodb.MongoDbConnection.<init>(MongoDbConnection.java:62)
at Generate.main(Generate.java:35)
Caused by: java.lang.ClassNotFoundException: org.apache.log4j.Logger
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
MongoURI 看起来很好,我想我没有任何登录名或密码
So mongoDB is running on 27017 and my collection name is test.
try {
// - Connection to base
String mongoURI = "mongodb://localhost:27017/test";
MongoDbConnection connection = null;
Map<String, Object> parameters = new HashMap<String, Object>();
try {
line35 connection = new MongoDbConnection(mongoURI, null, null);
parameters.put(MongoDbDataSource.CONNECTION, connection);
JasperDesign jasperDesign = JRXmlLoader.load("/home/gocoffee.jrxml");
JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
File jasperFile;
jasperFile = new File("/home/MongoDbReport.jasper");
JasperCompileManager.compileReportToFile("/home/gocoffee.jrxml", "MongoDbReport.jasper");
JasperFillManager.fillReportToFile("/home/MongoDbReport.jasper", parameters);
JasperExportManager.exportReportToPdfFile("/home/MongoDbReport.jrprint");
}
And I have this error, I don't understand why :
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/log4j/Logger
at com.jaspersoft.mongodb.MongoDbConnection.<init>(MongoDbConnection.java:62)
at Generate.main(Generate.java:35)
Caused by: java.lang.ClassNotFoundException: org.apache.log4j.Logger
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
The MongoURI seems fine and i think i don't have any login or password
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我同意@AdamC:你需要 log4j。但我想说,这不是“安装”它的问题;而是“安装”它的问题。您只需将 log4j.jar 文件添加到项目类路径中即可。你的代码看起来不错。
编辑:将信息从我的评论移到答案中。
除了将 log4j.jar 添加到类路径之外,您还需要一个名为 log4j.properties 的文件来配置 log4j。您可以创建自己的副本,也可以从 jasperforge 上的 MongoDB 连接器源获取副本。查找 src/main/resources/log4j.properties。
I agree with @AdamC: you need log4j. But I would say that it's not a matter of "installing" it; you just need to add a log4j.jar file to your project classpath. Your code looks fine.
EDIT: moving info from my comment into the answer.
In addition to adding log4j.jar to the classpath, you also need a file called log4j.properties to configure log4j. You can create your own, or you can grab the copy from the MongoDB connector source on jasperforge. Look for src/main/resources/log4j.properties.