Use lambda expressions inside TextField expression in Jaspersoft Studio 6.3

I want to use lambda expressions inside a TextField expression as follows:

Arrays.asList($F{field1}, $F{field2}, $F{field3}).stream().filter(i -> i != null).collect(java.util.stream.Collectors.joining(" / ")) 

Assuming these values ​​for the fields:

 $F{field1} = 1 $F{field2} = null $F{field3} = 2 

Expected result of TextField evaluation:

 1 / 2 

Instead, I get an error message in the IDE:

 Lambda expressions are allowed only at source level 1.8 or above 

And this is an exception inside the IDE at compile time:

 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) 

I have already changed my draft report to use JRE System 1.8, but the error continues.

How can i solve this? Does jasper compiler support lambdas?

+6
source share
1 answer

To enable Java 8 support, try adding these 3 parameters to the Jaspersoft Studio.ini file (located in the \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 

My ini file looks like this (for 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 checked this report and compiled it and worked well:

 <?xml version="1.0" encoding="UTF-8"?> <jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="java8support" pageWidth="595" pageHeight="842" whenNoDataType="NoDataSection" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20"> <property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/> <parameter name="p1" class="java.lang.String" isForPrompting="false"> <defaultValueExpression><![CDATA["1"]]></defaultValueExpression> </parameter> <parameter name="p2" class="java.lang.String" isForPrompting="false"> <defaultValueExpression><![CDATA["2"]]></defaultValueExpression> </parameter> <title> <band height="79" splitType="Stretch"> <textField> <reportElement x="80" y="20" width="234" height="30"/> <textFieldExpression><![CDATA[Arrays.asList($P{p1}, $P{p2}).stream().filter(i -> i != null).collect(java.util.stream.Collectors.joining(" / "))]]></textFieldExpression> </textField> </band> </title> </jasperReport> 

Compilation result:

Compilation Result in JSS

Generated result in JSS:

Preview in JSS

Note

I got the same error ( Lambda expressions are allowed only at source level 1.8 or above ) without using these 3 parameters in JSS:

Error compiling in JSS


More on how to compile templates from a Java project in messages:

+5
source

Source: https://habr.com/ru/post/1014021/


All Articles