yrk
yrk

Reputation: 87

Call java method from RPGLE (as400)

I need to call a java method from RPGLE, Im getting following error. I think the way I did prototype the method is wrong.

    Cause . . . . . :   RPG procedure TESTJ in program TESTLIB/TESTJ received 
  Java exception "java.lang.NoSuchMethodError:                             
  NumberToWord.strconvert([B)Ljava/lang/String;" when calling method       
  "strconvert" with signature "([B)Ljava.lang.String;" in class            
  "NumberToWord". 

My java code works fine. code is something like below,

public class NumberToWord
{
 .....
  .
   .
   public static String strconvert(String nms) {
    .
    .
    .
    return nms;
   }

}

My RPGLE code, which should be wrong is this,

  /free

   ctl-opt  dftactgrp(*no) actgrp(*caller);

   dcl-s String1 object(*java:'java.lang.String');
   dcl-s String2 object(*java:'java.lang.String');
   dcl-s str varchar(250);
   dcl-s JAVA_String object( *JAVA :'java.lang.String' );   


   dcl-pr JAVA_toString like( JAVA_String )
                        extproc( *JAVA :
                                 'java.lang.String' :
                                 *constructor );
   value varchar( 65535 ) const;
   end-pr;


   dcl-pr getNMTW static like( JAVA_String )
                        extproc( *JAVA :
                                 'NumberToWord' :
                                 'strconvert' );
   value varchar(65535) const;
   end-pr;


   dcl-pr getBytes char(250) extproc(*java:'java.lang.String':'getBytes');

   end-pr; 



   String1 = JAVA_toString('543');  //works fine

   str = getBytes(String1);         //works fine

   String2 = getNMTW('12345');      //exception occurs at this point

   str = getBytes(String2);

   *INLR = *ON;

 /END-FREE

JAVA_toString and getBytes methods work fine, when debug, exception occurs at getNMTW function.

CLASSPATH is already set. I think its fine since i'm getting NoSuchMethodError instead of NoClassFound error.

Upvotes: 0

Views: 3617

Answers (3)

rurugg
rurugg

Reputation: 31

What about you pass an array instead a simple char.

Upvotes: 0

yrk
yrk

Reputation: 87

I found the error.

when JVM has already started, the changes does not recognize by Java in that job I have to sign off and sign back on again to see the changes I made to the java function.

http://www.ibm.com/developerworks/rational/cafe/docBodyAttachments/2681-102-2-7220/Troubleshooting_RPG_Calls_To_Java_v2.html#changeclasses

Upvotes: 2

Tracy Probst
Tracy Probst

Reputation: 1859

It looks like your declaration for function getNMTW is set to pass a varchar to the Java method. When RPG calls Java, a varchar field translates to a Java byte array. So you first need to convert the RPG field to a Java String and then pass that string along. Try this:

dcl-pr getNMTW static like( JAVA_String )
                     extproc( *JAVA :
                              'NumberToWord' :
                              'strconvert' );
   value object( *JAVA :'java.lang.String' ) ;
end-pr;

and then:

String2 = JAVA_toString('12345');
String2 = getNMTW('12345');

A second option is to change your Java getNMTW method to accept a byte array and then convert the byte array into a String in Java. This is my preferred method because it helps keep the RPG code less cluttered. But either way will work.

Upvotes: 3

Related Questions