这不是一个新问题,但我的帮助/线索较少...所以在这里问...当我尝试时,我收到错误“java.lang.ClassNotFoundException:org.apache.commons.digester.Rule”运行 DynamicReport 示例项目。我的规范如下: Java - 1.7,commons-beanutils-1.9.3,commons-collections-3.2.2,commons-digester-3.2,commons-lang-3.7,commons-logging-1.2,dynamicreports-core-5.0.0,jasperreports-5.6, jasperreports-javaflow,
代码片段如下:
// import statements
public class sampleReport {
public sampleReport() {
build();
}
private void build() {
try {
JasperReportBuilder report = DynamicReports.report();
// a new report
report // create new report design
.columns(
// add columns title, field name data type
Columns.column("Item", "item",
DataTypes.stringType()),
Columns.column("Quantity", "quantity",
DataTypes.integerType()),
Columns.column("Unit price",
"unitprice",
DataTypes.bigDecimalType()))
.title(Components.text("Getting started report")
.setHorizontalAlignment(HorizontalAlignment.CENTER))
// shows report title
.pageFooter(Components.pageXofY())
// shows number of page at page footer
.setDataSource(createDataSource())
// set datasource
.show(); // create and show report
// show the report
report.show();
// export the report to a pdf file
report.toPdf(new FileOutputStream("c:/report.pdf"));
} catch (DRException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
private JRDataSource createDataSource() {
DRDataSource dataSource = new DRDataSource("item",
"quantity", "unitprice");
dataSource.add("Notebook", 1, new BigDecimal(500));
dataSource.add("DVD", 5, new BigDecimal(30));
dataSource.add("DVD", 1, new BigDecimal(28));
dataSource.add("DVD", 5, new BigDecimal(32));
dataSource.add("Book", 3, new BigDecimal(11));
dataSource.add("Book", 1, new BigDecimal(15));
dataSource.add("Book", 5, new BigDecimal(10));
dataSource.add("Book", 8, new BigDecimal(9));
return dataSource;
}
public static void main(String[] args) {
new sampleReport();
}
感谢任何帮助。
最佳答案
我不知道您的 IDE 配置和/或构建配置。但是,该错误将我引向jar 版本不兼容问题。 基本上发生的情况是,您的一个 jar 正在引用一个由于版本不同(较旧或较新)而未包含在当前构建路径中的类
建议的解决方案:如果您有能力使用更高级的构建工具,例如 maven或gradle ,它会自动解决依赖关系并可以指出版本问题。使用它们的最佳方法是逐一添加 Maven 工件(从最复杂的依赖 jar 开始),直到删除所有导入错误。这可能会解决所有版本问题。
替代(快速)解决方案:下载 this apache.digester 的版本,因为如果你看 maven dependencies在 jasper 中,您可以看到它引用了digester 2.1 版本,而您使用的是3.2 版本,该版本可能在不同的包下具有相同的类。
希望有帮助。
关于java.lang.ClassNotFoundException : org. apache.commons.digester.Rule,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48176440/