我想在TextField表达式中使用lambda表达式,如下所示:
Arrays.asList($F{field1}, $F{field2}, $F{field3}).stream().filter(i -> i != null).collect(java.util.stream.Collectors.joining(" / "))
假设这些字段的值:
$F{field1} = 1 $F{field2} = null $F{field3} = 2
TextField评估的预期结果是:
1 / 2
而不是我在IDE中收到错误:
Lambda expressions are allowed only at source level 1.8 or above
这是编译期间IDE内部的异常:
net.sf.jasperreports.engine.JRException: Errors were encountered when compiling report expressions class file: 1. Lambda expressions are allowed only at source level 1.8 or above value = "Contato: " + Arrays.asList(((java.lang.String)field_c_telefone.getValue()), ((java.lang.String)field_c_celular.getValue()), ((java.lang.String)field_c_fax.getValue()), ((java.lang.String)field_c_email.getValue())).stream().filter(i -> i != null).collect(java.util.stream.Collectors.joining(" / ")); //$JR_EXPR_ID=22$ <------------> 2. Lambda expressions are allowed only at source level 1.8 or above value = "Contato: " + Arrays.asList(((java.lang.String)field_c_telefone.getOldValue()), ((java.lang.String)field_c_celular.getOldValue()), ((java.lang.String)field_c_fax.getOldValue()), ((java.lang.String)field_c_email.getOldValue())).stream().filter(i -> i != null).collect(java.util.stream.Collectors.joining(" / ")); //$JR_EXPR_ID=22$ <------------> 3. Lambda expressions are allowed only at source level 1.8 or above value = "Contato: " + Arrays.asList(((java.lang.String)field_c_telefone.getValue()), ((java.lang.String)field_c_celular.getValue()), ((java.lang.String)field_c_fax.getValue()), ((java.lang.String)field_c_email.getValue())).stream().filter(i -> i != null).collect(java.util.stream.Collectors.joining(" / ")); //$JR_EXPR_ID=22$ <------------> 3 errors . at net.sf.jasperreports.engine.design.JRAbstractCompiler.compileReport(JRAbstractCompiler.java:206) at net.sf.jasperreports.eclipse.builder.JasperReportCompiler.compileReport(JasperReportCompiler.java:294) at net.sf.jasperreports.eclipse.builder.JasperReportCompiler.compileReport(JasperReportCompiler.java:146) at net.sf.jasperreports.eclipse.builder.JasperReportsBuilder.compileJRXML(JasperReportsBuilder.java:220) at com.jaspersoft.studio.editor.action.CompileAction.actionCompile(CompileAction.java:142) at com.jaspersoft.studio.editor.action.CompileAction$1.run(CompileAction.java:93) at org.eclipse.core.internal.jobs.Worker.run(Worker.java:55)
我已经将我的报告项目更改为使用JRE System 1.8,但错误仍在继续.
我该如何解决?jasper编译器是否支持lambda?
要启用Java 8支持,请尝试将此3个参数添加到Jaspersoft Studio.ini文件(放置在\TIBCO\Jaspersoft Studio-6.x.final
文件夹中):
-Dorg.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 -Dorg.eclipse.jdt.core.compiler.compliance=1.8 -Dorg.eclipse.jdt.core.compiler.source=1.8
我的ini文件看起来像这样(对于Jaspersoft Studio 6.3.1版本):
-startup plugins/org.eclipse.equinox.launcher_1.3.100.v20150511-1540.jar --launcher.library plugins/org.eclipse.equinox.launcher.win32.win32.x86_64_1.1.300.v20150602-1417 -data @noDefault --launcher.defaultAction openFile -vm features/jre.win32.win32.x86_64.feature_1.8.0.u92/jre/bin -vmargs -Xms128m -Xmx1024m -XX:+CMSClassUnloadingEnabled -XX:+UseConcMarkSweepGC -Dfile.encoding=UTF-8 -Djava.net.preferIPv4Stack=true -Dorg.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 -Dorg.eclipse.jdt.core.compiler.compliance=1.8 -Dorg.eclipse.jdt.core.compiler.source=1.8
我检查了这份报告,编译得很好:
i != null).collect(java.util.stream.Collectors.joining(" / "))]]>
编译结果:
JSS中生成的结果:
我Lambda expressions are allowed only at source level 1.8 or above
在JSS中不使用这3个参数得到了相同的错误():
如何在jrxml文件中使用lambda表达式?
什么版本的JaspeReports支持Java 8?
JDK 8是否支持JasperReports 6?