ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

Calling JAVA method from RPGLE

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Calling JAVA method from RPGLE

    Hi all,

    hopefully this should be an easy one to solve! I have just started doing a bit of a refresher on creating JAVA objects and calling their methods from and RPG program. However, I cannot for the life of me remember how all of this stuff worked (or rather does work). I started off with something simple: attempt to retrieve the current time (in milliseconds) from the system.

    In JAVA this would simply look like:

    Code:
    long milliSeconds = System.currentTimeMillis();
    I have this in my RPG routine:

    Code:
    //=======================================================================*
          // Variable Declarations                                                 *
          //=======================================================================*
    
           dcl-pr   System object(*JAVA:'java.lang.System')
                           extproc(*JAVA:'java.lang.System':*CONSTRUCTOR);
           end-pr;
    
           dcl-pr   CurrentTime int(20:0)
                           extproc(*JAVA:'java.lang.System':'currentTimeMillis');
           end-pr;
    
           dcl-s    systemObject           like(System);
           dcl-s    milliSeconds           like(CurrentTime);
    
          //=======================================================================*
          // Program Mainline                                                      *
          //=======================================================================*
    
            systemObject = System();
            milliSeconds = CurrentTime(systemObject);
    
            *inlr = *ON;
            return;
    
          //=======================================================================*
          // Subroutines                                                           *
          //=======================================================================*
           begsr *pssr;
             dump;
             return;
           endsr;
    
          //=======================================================================*
    Retrieving the system object to call the method from is working fine, but I am falling short on the call to the method implementation. The error I get is:

    Code:
    Java exception received when calling Java method.
    
    RPG procedure SLAVETEST in program QDEVOPLIB/SLAVETEST received Java exception "java.lang.NoSuchMethodError: java/lang/System.currentTimeMillis()J" when calling method "currentTimeMillis" with signature "()J" in class "java.lang.System".
    Now I checked the signature of the method using (JAVAP -S java.lang.System) and this was the result:

    Code:
    public static native long currentTimeMillis();
      descriptor: ()J
    I am at a loss as to what I am getting this error, maybe because it is static but I am not sure. Any ideas or comments would be appreciated. Thanks


  • #2
    Yep - I think you need to add STATIC to the currentTimeMillis prototype, and that in turn means removing the parm from the call so it should be:

    Code:
    dcl-pr   CurrentTime int(20:0) static                           
             extproc(*JAVA:'java.lang.System':'currentTimeMillis');                                                                 
    end-pr;                                                         
    systemObject = System();     
    milliSeconds = CurrentTime();
    Seems to work for me - although I didn't check if the result was accurate.

    Comment


    • #3
      Make your CurrentTime ptototype static, and delete the System prototype (you don't need it).

      Comment

      Working...
      X