× The internal search function is temporarily non-functional. The current search engine is no longer viable and we are researching alternatives.
As a stop gap measure, we are using Google's custom search engine service.
If you know of an easy to use, open source, search engine ... please contact support@midrange.com.



This thread about RPG and Java got me started.

I wrote a quickie helloWorld that takes but one parm - the name.  Compiled,
ran in QSH and it works.  After doing some reading, I prototyped it in RPG
and commenced my struggle with the CLASSPATH.  Hint: after changing
CLASSPATH via ADDENVVAR, sign off and on.  It appears that the JVM reads the
class path when it is first started.  To get a new class path, you need a
new JVM and thus, a new job.  Anyway, I finally got the thing to run.  I
think.  It flashes something very quickly and then it's gone.

Barbara's mention about DeleteLocalRef lead me to The Manual, in this case,
the RPG Programmers Guide.  There's a bunch of cool things there, and among
them is 'Calling Java Methods from RPG', item 4:

"Normally, Java does its own garbage collection, detecting when an object is
no
longer needed. When you create objects by calling Java constructors from
your
non-native RPG procedure, Java has no way of knowing that the object can be
destroyed, so it never destroys them. If you know you are not going to need
an
object any more, you should tell this to Java by calling the JNI function
described in "Telling Java you are finished with a temporary object" on
page 174."

I don't want stuff floating about: I want to use freeLocalRef to clean up my
Java string in the RPG program.  It appears I need to obtain the JNI
environment pointer first.  So off to page 176 I go and my real odyssey
begins.  I copy/paste the getJniEnv procedure into Code/400 and it's
hideously misaligned and uses a goofy character for the apostrophe.  Fix
that and compile.  No prototypes.  I read a bit more and sure enough,
QSYSINC/QRPGLESRC member JNI has a PILE of prototypes.  Add that and
compile.  Binding fails.  Turns out there's a Java service program QJVAJNI
that has all the stuff I need.  That guy is in binding directory QUSAPIBD.
(Neither of these things is mentioned in the book.)  Add that and compile.
"Definition not found for symbol '_QRNX_JNI_FREE_OBJ'."

Well, this isn't in my RPG source and it isn't in the /copy JNI member.
It's not in the cross reference the compiler prints either, so I'm guessing
it's some internal thing.  Other '_QRNX' references appear in QRNXUTIL,
QRNXIE and QRNXIO.

Searches in the APAR and PTF database on '_QRNX_JNI_FREE_OBJ' and 'RPG Java'
reveal nothing.  What am I doing wrong?

h thread(*serialize) bnddir('QUSAPIBD')                                
 * dbgview(*list) tgtrls(V5R2M0)                                       
                                                                       
d helloWorld      pr                  extproc(*java:                   
d                                       'helloWorld':                  
d                                       'main')                        
d                                       static                         
d  args                           o   class(*java: 'java.lang.String') 
d                                       const                          
d                                       dim(256)                       
d                                       options(*varsize)              
                                                                       
d newJavaString   pr              o   extproc(*java:                   
d                                       'java.lang.String':            
d                                       *constructor)                  
d  inpValue                    256a   const varying                    
                                                                       
d getJniEnv       pr              *                                    
                                                                       
 * the IBM JNI library                                                 
 /copy qsysinc/qrpglesrc,JNI                                           
                                                                  
d string          s                   like(newJavaString)         
                                                                  
c                   eval      string = newJavaString('Buck')      
c                   callp     helloWorld(string)                  
c                   eval      *inlr = *on                         
                                                                  
                                                                  
 * getJniEnv                                                      
p getJniEnv       b                   export                      
d getJniEnv       pi              *                               
d rc              s                   like(jint)                  
d rpgRc           s               N                               
d jvm             s               *   dim(1)                      
d env             s               *                               
d bufLen          s                   like(jsize) inz(%elem(jvm)) 
d nVMs            s                   like(jsize)                 
                                                                  
d initArgs        ds                  likeds(JDK1_1InitArgs)      
d attachArgs      ds                  likeds(JDK1_1AttachArgs)    
 /free                                                                
  //----------------------------------------------------------------  
  // See if there s a JVM started                                     
  //----------------------------------------------------------------  
  rc = JNI_GetCreatedJavaVMs (jvm : bufLen : nVMs);                   
                                                                      
  //----------------------------------------------------------------  
  // If JVM is started, just attach to it, to get the env pointer     
  //----------------------------------------------------------------  
  if (rc = 0                                                          
    and nVMs > 0);                                                    
    attachArgs = *ALLX'00';                                           
    JavaVM_P = jvm(1);                                                
    rc = AttachCurrentThread (jvm(1):                                 
           env : %addr(attachArgs));                                  
                                                                      
   //---------------------------------------------------------------- 
   // If JVM is not started, start it with the default classpath.     
   //---------------------------------------------------------------- 
  else;                                                               
    initArgs = *ALLX'00';                                             
                                                                      
    rc = JNI_GetDefaultJavaVMInitArgs (%addr(initArgs));      
    if (rc = 0);                                              
      rc = JNI_CreateJavaVM (jvm(1) : env : %addr(initArgs)); 
    endif;                                                    
  endif;                                                      
                                                              
  if (rc = 0);                                                
    return env;                                               
  else;                                                       
    return *NULL;                                             
  endif;                                                      
 /end-free                                                    
p getJniEnv       e                                           


// Hello, world!                               
// to use: java helloWorld <yourname>          
import java.io.*;                              
                                               
public class helloWorld                        
{                                              
    public static void main(String argv[])     
  {                                            
      System.out.println("Hello, " + argv[0]); 
      return;                                  
  }                                            
}                                              

  --buck

As an Amazon Associate we earn from qualifying purchases.

This thread ...

Follow-Ups:

Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2024 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.