RPG and Java Lists
Posted: Tue Oct 01, 2024 6:07 pm
I saw a question on one of the Facebook Groups about needing to split a PDF file using RPG and I had previously done something with merging multiple PDF documents into one PDF document with PDFBox and thought maybe this would be similar. I made a good bit of progress using RDi to create the Java Methods I needed, but one Method returns a List of Objects and I wasn't sure how to handle that. I found one example on the internet of a get that I thought would work but I keep getting the following error when I call it.
The error happens on this line
pdfPartialDocument = get(splitRet : 0);
I've tried 0 and 1 as I wasn't sure whether the list would be 0 or 1 based since it was RPG but I assumed 0. Is there anyway to see what is really wrong?
I used this page to help me get started trying to convert the Java to RPG
https://stackoverflow.com/questions/402 ... -filenames
Also found some information here:
https://stackoverflow.com/questions/164 ... -using-rpg
Code: Select all
Java exception "java.lang.NoSuchMethodError: java/util/List.get(I)Ljava/util/Object;" when calling method "get" with
signature "" in class "java.util.List".
pdfPartialDocument = get(splitRet : 0);
I've tried 0 and 1 as I wasn't sure whether the list would be 0 or 1 based since it was RPG but I assumed 0. Is there anyway to see what is really wrong?
I used this page to help me get started trying to convert the Java to RPG
https://stackoverflow.com/questions/402 ... -filenames
Also found some information here:
https://stackoverflow.com/questions/164 ... -using-rpg
Code: Select all
**free
// https://stackoverflow.com/questions/40221977/pdfbox-split-pdf-in-multi-files-with-different-page-
//
CTL-OPT ACTGRP(*NEW) Main(Main);
DCL-DS psds psds;
MsgID CHAR(7) POS(40);
ExceptData CHAR(80) POS(91);
END-DS;
DCL-PROC Main;
// Create Java String Object
DCL-PR newString Object(*JAVA:'java.lang.String')
ExtProc(*JAVA:'java.lang.String':*CONSTRUCTOR);
*N Varchar(65535) Const;
END-PR;
// Create Java File Object
DCL-PR newFile Object(*JAVA:'java.io.File')
ExtProc(*JAVA:'java.io.File':*CONSTRUCTOR);
*N Object(*Java:'java.lang.String');
END-PR;
DCL-PR get OBJECT(*JAVA : 'java.util.Object' )
EXTPROC(*JAVA :
'java.util.List' : 'get');
arg0 INT(10) VALUE;
END-PR;
// -----------------------------------------------------------------
// Returned field declaration for Java method:
// load in class PDDocument
// Java returned type is org.apache.pdfbox.pdmodel.PDDocument
// -----------------------------------------------------------------
DCL-S pdfDocument OBJECT(*JAVA : 'org.apache.pdfbox.pdmodel.PDDocument' );
DCL-S pdfPartialDocument OBJECT(*JAVA : 'org.apache.pdfbox.pdmodel.PDDocument' );
// -------------------------------------------------------------------------
// Parameter field declarations for Java method:
// load in class PDDocument
// -------------------------------------------------------------------------
// Parameter field declaration for Java type: File
DCL-S j_loadParam0 OBJECT(*JAVA : 'java.io.File' );
// --------------------------------------------------------------------
// Prototype for Java method:
// load in class PDDocument in package org.apache.pdfbox.pdmodel
// --------------------------------------------------------------------
DCL-PR load OBJECT(*JAVA : 'org.apache.pdfbox.pdmodel.PDDocument' )
EXTPROC(*JAVA : 'org.apache.pdfbox.pdmodel.PDDocument' : 'load' )
STATIC;
// Parameter prototype declaration for Java type: File
arg0 OBJECT(*JAVA : 'java.io.File' );
END-PR ;
// --------------------------------------------------
// Declaration for Java object of class Splitter in package org.apache.pdfbox.multipdf
// --------------------------------------------------
DCL-S splitterObj Object(*java: 'org.apache.pdfbox.multipdf.Splitter' );
// -----------------------------------------------
// Returned field declaration for Java method:
// split in class Splitter
// Java returned type is java.util.List
// -----------------------------------------------
DCL-S splitRet OBJECT(*JAVA : 'java.util.List' );
// -------------------------------------------------------------------------
// Parameter field declarations for Java method:
// split in class Splitter
// -------------------------------------------------------------------------
// Parameter field declaration for Java type: PDDocument
DCL-S j_splitParam0 OBJECT(*JAVA : 'org.apache.pdfbox.pdmodel.PDDocument' );
// -----------------------------------------------------
// Prototype for Java constructor in class
// Splitter in package org.apache.pdfbox.multipdf
// -----------------------------------------------------
DCL-PR splitterObjCtor Object(*java :
'org.apache.pdfbox.multipdf.Splitter' )
Extproc(*java : 'org.apache.pdfbox.multipdf.Splitter' : *CONSTRUCTOR);
END-PR ;
// --------------------------------------------------------------------
// Prototype for Java method:
// split in class Splitter in package org.apache.pdfbox.multipdf
// --------------------------------------------------------------------
DCL-PR split OBJECT(*JAVA : 'java.util.List' ) EXTPROC(*JAVA : 'org.a+
pache.pdfbox.multipdf.Splitter' : 'split' );
// Parameter prototype declaration for Java type: PDDocument
arg0 OBJECT(*JAVA : 'org.apache.pdfbox.pdmodel.PDDocument' );
END-PR ;
// ---------------------------------------------------------------------------
// Prototype for Java method:
// setStartPage in class Splitter in package org.apache.pdfbox.multipdf
// ---------------------------------------------------------------------------
DCL-PR setStartPage EXTPROC(*JAVA : 'org.apache.pdfbox.multipdf.Spli+
tter' : 'setStartPage' );
// Parameter prototype declaration for Java type: int
arg0 INT(10) VALUE;
END-PR ;
// ---------------------------------------------------------------------------
// Prototype for Java method:
// setStartPage in class Splitter in package org.apache.pdfbox.multipdf
// ---------------------------------------------------------------------------
DCL-PR setEndPage EXTPROC(*JAVA : 'org.apache.pdfbox.multipdf.Spli+
tter' : 'setStartPage' );
// Parameter prototype declaration for Java type: int
arg0 INT(10) VALUE;
END-PR;
// --------------------------------------------------
// Declaration for Java object of class PDDocument in package org.apache.pdfbox.pdmodel
// --------------------------------------------------
DCL-S pDDocumentObj Object(*java: 'org.apache.pdfbox.pdmodel.PDDocume+
nt' );
// -------------------------------------------------------------------------
// Parameter field declarations for Java method:
// save in class PDDocument
// -------------------------------------------------------------------------
// Parameter field declaration for Java type: File
DCL-S j_saveParam0 OBJECT(*JAVA : 'java.io.File' );
// ------------------------------------------------------
// Prototype for Java constructor in class
// PDDocument in package org.apache.pdfbox.pdmodel
// ------------------------------------------------------
DCL-PR pDDocumentObjCtor Object(*java : 'org.apache.pdfbox.pdmodel.PD+
Document' ) Extproc(*java : 'org.apache.pdfbox.pdmodel.PDDocum+
ent' : *CONSTRUCTOR);
END-PR ;
// --------------------------------------------------------------------
// Prototype for Java method:
// save in class PDDocument in package org.apache.pdfbox.pdmodel
// --------------------------------------------------------------------
DCL-PR save EXTPROC(*JAVA : 'org.apache.pdfbox.pdmodel.PDDocument' :
'save' );
// Parameter prototype declaration for Java type: File
arg0 OBJECT(*JAVA : 'java.io.File' );
END-PR ;
DCL-S vPdfFilePathAndName CHAR(5000);
DCL-S j_NewFile OBJECT(*JAVA : 'java.io.File' );
DCL-S j_PartialNewFile OBJECT(*JAVA : 'java.io.File' );
DCL-S j_NewString OBJECT(*JAVA : 'java.lang.String');
// Parameter field declaration for Java type: int
DCL-S j_StartPage INT(10) INZ(1);
DCL-S j_EndPage INT(10) INZ(2);
DCL-S j_Index INT(10) INZ(0);
// QCMDEXEC to run IBMi commands
DCL-PR QCmdExc ExtPgm('QCMDEXC');
*N CHAR(2000) Const;
*N PACKED(15:5) Const;
END-PR;
DCL-S cmdString CHAR(2000);
DCL-C TICK '''';
// Set Classpath or Change Classpath
cmdString = 'ADDENVVAR (CLASSPATH) VALUE(' + TICK +
'/home/rthompson/pdfbox2/pdfbox-app-2.0.27.jar' + TICK +
')';
Monitor;
QCmdExc(cmdString : %len(%trim(cmdString)));
On-Error;
If MsgID = 'CPFA980';
cmdString = 'CHGENVVAR (CLASSPATH) VALUE(' + TICK +
'/home/rthompson/pdfbox2/pdfbox-app-2.0.27.jar' + TICK +
')';
Monitor;
QCmdExc(cmdString : %len(%trim(cmdString)));
On-Error;
*INLR = *On;
Return;
EndMon;
EndIf;
EndMon;
vPdfFilePathAndName = '/home/rthompson/pdfMerge/nwpdf2.pdf';
j_NewString = newString(%trim(vPdfFilePathAndName));
j_NewFile = newFile(j_NewString);
// --------------------------------
// Call Java method:
// load in class PDDocument
// --------------------------------
//
pdfDocument = load(j_NewFile);
splitterObj = splitterObjCtor();
// --------------------------------------
// Call Java method:
// setStartPage in class Splitter
// --------------------------------------
setStartPage( splitterObj : j_StartPage );
setEndPage( splitterObj : j_EndPage );
// -------------------------------
// Call Java method:
// split in class Splitter
// -------------------------------
splitRet = split( splitterObj : pdfDocument );
pdfPartialDocument = pDDocumentObjCtor();
pdfPartialDocument = get(splitRet : 0);
vPdfFilePathAndName = '/home/rthompson/pdfMerge/split.pdf';
j_NewString = newString(%trim(vPdfFilePathAndName));
j_PartialNewFile = newFile(j_NewString);
// --------------------------------
// Call Java method:
// save in class PDDocument
// --------------------------------
save( pdfPartialDocument : j_PartialNewFile );
END-PROC;