Search code examples
javapdfcrystal-reportscfmlcommandbox

Export Crystal reports to PDF in java via cfml


Using Commandbox 4.7.0+00026, I'm attempting to open a Crystal Reports document in preparation for output as PDF. I'm not able to get the file to open, getting the error

java.lang.NoClassDefFoundError: com/crystaldecisions/reports/common/data/CrystalResultSet

on line

var rd = rcd.open(reportPath, 0);

I've confirmed CrystalCommon2.jar is in the lib folder.

Here's the full CommandBox task runner:

component{

  /*
  The .jar files from Java Reporting Component SDK are copied to `box\engine\cfml\cli\cfml-web\lib` 
  */

  property name = "out" inject = "java:java.io.ByteArrayOutputStream";
  property name = "crs" inject = "java:com.crystaldecisions.reports.common.data.CrystalResultSet";
  property name = "crv" inject = "java:com.crystaldecisions.report.web.viewer.CrystalReportViewer";
  property name = "oro" inject = "java:com.crystaldecisions.sdk.occa.report.application.OpenReportOptions";
  property name = "rcd" inject = "java:com.crystaldecisions.sdk.occa.report.application.ReportClientDocument";
  property name = "ref" inject = "java:com.crystaldecisions.sdk.occa.report.exportoptions.ReportExportFormat";

  /* resources 
  http://www.javathinking.com/2011/09/using-crystal-reports-java-api-to.html
  https://stackoverflow.com/questions/3178778/export-crystal-reports-to-pdf-in-java?rq=1
  */

  function run(){
    var reportPath = "c:\reports\Statement of Account.rpt";

    var openAsReadOnly = oro._openAsReadOnly;
    var inprocConnectionString = rcd.inprocConnectionString;

    var rd = rcd.open(reportPath, 0);

    out.reset();

  }
}

Some resources indicate a CRConfig.xml needs exist, but I've not had success - tried a myriad of locations. CRConfig.xml:

<?xml version="1.0" encoding="utf-8"?>
<CrystalReportEngine-configuration>
    <reportlocation>..</reportlocation>
    <timeout>0</timeout>
    <ExternalFunctionLibraryClassNames>
      <classname></classname>
    </ExternalFunctionLibraryClassNames>
</CrystalReportEngine-configuration>

Other resources maintain that CrystalReportsEngine-config.xml must exist. Again, not sure where it should reside.

<?xml version="1.0" encoding="utf-8"?>  
<CrystalReportEngine-configuration>  
  <reportlocation>c:\reports\</reportlocation>  
  <timeout>10</timeout>  
  <keycode>MyLicenseKeyCodeHere</keycode>  
</CrystalReportEngine-configuration>  

Solution

  • Turns out I was dropping the Crystal Reports jar files in the wrong folder.

    Drop them into the same folder that's returned by this function:

      function jarPath(){
          variables.jarPath = createObject( "java", "java.io.File" ).init( 
          createObject( "java", "runwar.LaunchUtil"  )
          .getClass()
          .getProtectionDomain()
          .getCodeSource()
          .getLocation()
          .toURI()
          .getSchemeSpecificPart() )
        .getAbsolutePath();
    
        job.start(serialize(variables.jarPath));
        job.complete();
    
      }