Search code examples
javagwterrai

Call Java function from JSNI


I'm having trouble with a JSNI calling a Java method:

public static native void update() /*-{
    [email protected]::populate()();
}-*/;

No errors, its just that the method is not getting triggered, the Java method, populate() which shows a alert box when called, is not firing.

MyPage however is a Errai page annotated with @Page


Solution

  • I think you are miss-understanding the meaning of the instance preceding the @ symbol in JSNI.

    You are calling the method populate() of the instance this, but your update() method is static.

    You have either define populate() as static and call it in a static way.

    package app.client.local;
    class MyPage {
    
      public static native void update() /*-{
        @app.client.local.MyPage::populate()();
      }-*/;
    
      public static void populate() {
      }
    }
    

    Or you can pass the instance of the class having the method as argument to your jsni code:

    package app.client.local;
    
    class MyClass {
      public void populate() {
      }
    }
    
    class MyPage {
      public static native void update(MyClass instance) /*-{
        [email protected]::populate()();
      }-*/;
    }