Hot questions for Using JasperReports in java 8

Question:

I want to use lambda expressions inside a TextField expression like this:

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

Assuming this values for the fields:

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

The expected result is of the TextField evaluation is:

1 / 2

Instead of I'm getting an error in the IDE:

Lambda expressions are allowed only at source level 1.8 or above

And this is the exception inside the IDE during the compile:

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 already changed my report project to use the JRE System 1.8, but the error continues.

How can I solve it? Does jasper compiler supports lambdas?


Answer:

To enable Java 8 support try to add this 3 parameters to the Jaspersoft Studio.ini file (placed at \TIBCO\Jaspersoft Studio-6.x.final folder):

-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 version):

-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 it compiled 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>

Compiling result:

The generated result in JSS:

Remark

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


More info how to compile templates from Java project in posts:
  • How to use lambda expression in jrxml file?
  • What version of JaspeReports has support for Java 8?
  • Does JDK 8 supports JasperReports 6?

Question:

I've been trying to get my report working for few days but still no luck. I have no idea if it's something wrong with the dependencies.

I asked this question previously.

Anyway i just want to know if anyone have used JasperReports 6 with JDK 8? after all i doubt if these are compatible.


Answer:

Update your JDT

   <dependency>
        <groupId>net.sf.jasperreports</groupId>
        <artifactId>jasperreports</artifactId>
        <version>5.6.1</version>
        <exclusions>
            <exclusion>
                <groupId>eclipse</groupId>
                <artifactId>jdtcore</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

    <dependency>
        <groupId>org.eclipse.jdt.core.compiler</groupId>
        <artifactId>ecj</artifactId>
        <version>4.4</version>
    </dependency>

Source : How to Run JasperReport with Java 8

Question:

I am trying to use lambda expression inside jrxml file to get values for my field.

I imported appropriate classes but it is giving me error that

p cannot be resolved to a variable.

How to use a lambda expression inside JasperReports's templates?.

<textFieldExpression>
    <![CDATA[StringUtils.join(((Collection<Property>)((Object2)$F{field1}.getPropertyValue("property1")).getPropertyValue("property2")).stream().map(p->((Object1)p.getValue()).getName()).collect(toList()),',');]]>
</textFieldExpression>

Exception:

net.sf.jasperreports.engine.JRException: Errors were encountered when compiling report expressions class file:
1. p cannot be resolved to a variable

Answer:

If you're using the Eclipse JDT compiler, first make sure that you have a fairly recent version.

You can get 4.5.1 from Maven for instance, then add the following properties to your jasperreports.properties file:

org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.source=1.8

Question:

Using Netbeans IDE 8.0.2 and JDK8, when I try to generate a PDF report i get this error:

The type java.lang.CharSequence cannot be resolved. It is indirectly referenced from required .class filesvalue = ((java.lang.String)field_type.getValue()).contentEquals("XML"); //$JR_EXPR_ID=26$

This error occurs everytime I use this expression $F{type}.contentEquals("XML") or even the .equals expression. With JDK6 it doesn't happen.

I've searched the web and I find a fix for this but for ecliplse in this url: https://community.jaspersoft.com/jasperreports-server/issues/3498

My dependencies are:

 <dependency>
            <groupId>net.sf.jasperreports</groupId>
            <artifactId>jasperreports</artifactId>
            <version>5.6.0</version>
        </dependency>
        <dependency>
            <groupId>net.sf.jasperreports</groupId>
            <artifactId>jasperreports-fonts</artifactId>
            <version>5.6.1</version>
        </dependency>

Answer:

Well i didnt find a perfect solution for this, although to walktrough this issue I used .endsWith() instead of .equals. Works in this situation.