|
Adele,
I'm trying the GetStringLength but I get an error.
Here's an example of retrieving the contents of a string via the JNI API:
H DFTACTGRP(*NO) BNDDIR('QC2LE')
/define OS400_JVM_12
/define JNI_COPY_STRING_FUNCTIONS
/copy qsysinc/qrpglesrc,jni
D String_new pr extproc(*JAVA:'java.lang.String'
D : *CONSTRUCTOR )
D like(jstring)
D initval 65535A varying const
D GetJniEnv PR *
D memcpy PR * extproc('memcpy')
D dst * value
D src * value
D size 10U 0 value
D sleep PR 10I 0 extproc('sleep')
D secs 10i 0 value
D teststr s like(jstring)
D isCopy s like(jboolean)
D len s like(jsize)
D char s like(jchar) based(p_Char)
D unicode s 20C
D output s 20A
/free
teststr = String_new('Welcome to RPG400-L!');
JNIENV_P = GetJNIEnv();
len = GetStringLength(JNIENV_P: teststr);
if (ExceptionCheck(JNIENV_P) = JNI_TRUE);
ExceptionDescribe(JNIENV_P);
ExceptionClear(JNIENV_P);
sleep(5);
len = 0;
endif;
p_char = GetStringChars(JNIENV_P: teststr: isCopy);
if (ExceptionCheck(JNIENV_P) = JNI_TRUE);
ExceptionDescribe(JNIENV_P);
ExceptionClear(JNIENV_P);
sleep(5);
p_char = *NULL;
endif;
// at this point, p_char is a pointer to a unicode string
// and len is the length of the string. You can do whatever
// you need with this string, and then call the
// ReleaseStringChars() when you're done.
//
// For the sake of example, I'll copy it to an RPG unicode
// variable, then copy that to an EBCDIC character string,
// and then display it.
//
if (len <= %len(unicode));
memcpy(%addr(unicode): p_Char: len * %size(jchar));
output = %char(unicode);
dsply output;
endif;
ReleaseStringChars(JNIENV_P: teststr: char);
*inlr = *on;
/end-free
*++++++++++++++++++++++++++++++++++++++++++++++++++++++
* GetJniEnv(): Get pointer to JNI environment
*
* Returns *NULL upon failure,
* or pointer to environment
*++++++++++++++++++++++++++++++++++++++++++++++++++++++
p GetJniEnv B
D GetJniEnv PI *
D AttArg ds likeds(JavaVMAttachArgs)
D JVM s like(JavaVM_p) dim(1)
D JVMc s like(jSize)
D Env s * inz(*null) static
D RC s 10I 0
/free
if (env <> *NULL);
return env;
endif;
monitor;
RC = JNI_GetCreatedJavaVMs(JVM: 1: JVMc);
if (RC <> JNI_OK or JVMc = 0);
return *NULL;
endif;
JavaVM_P = JVM(1);
Attarg = *ALLx'00';
Attarg.version = JNI_VERSION_1_2;
RC = AttachCurrentThread(JVM(1): Env: %addr(AttArg));
if (RC <> JNI_OK);
env = *NULL;
endif;
on-error;
env = *NULL;
endmon;
return ENV;
/end-free
P E
As an Amazon Associate we earn from qualifying purchases.
This mailing list archive is Copyright 1997-2025 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].
Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.