Search code examples
gwtjsni

Calling JSNI method from Java method


I have following code:

public static void show(String value){
   Window.alert("From Java");
   invokeMethod(); //Does not get called
}  

public static native void invokeMethod() /*-{ 
    alert("From JSNI");
}-*/; 

I want to call a JSNI method from Java method, however invokeMethod() never gets called... I could not find much use cases for calling JSNI method from Java.

Why isn't above code working?


Solution

  • First of all, as Colin Alworth said, you need to change your code to $wnd.alert("From JSNI"); If you want to call another JS function apart from alert(), you should write the body of your function in your html page. You'll find all information needed here

    So your code should look like this:

    public static void show(String value){
       Window.alert("From Java");
       invokeMethod(); 
    }  
    
    public static native void invokeMethod() /*-{ 
        $wnd.alert("From JSNI"); //Added "$wnd."
    }-*/;