How to call GWT java function from Javascript?

10,566

your example is not going to work, since you are trying to use JSNI in some external script. If you want to call something from external JS you need to use approach described in this question or use GWT exporter

UPDATE:

The safest way to expose the GWT stuff is to wrap invocation in some other function. For example:

    public native void expose()/*-{
    $wnd.exposedMethod = function(param){
         @com.my.MyClass::myFunction(*)(param);
    }
}-*/;

Otherwise you might encounter some strange bugs in production mode=)

Share:
10,566
Dims
Author by

Dims

Software developer & Machine Learning engineer C/C++/Java/C#/Python/Mathematica/MATLAB/Kotlin/R/PHP/JavaScript/SQL/HTML/ LinkedIn: http://www.linkedin.com/in/dimskraft Telegram: https://t.me/dims12 I prefer fishing rod over fish.

Updated on June 28, 2022

Comments

  • Dims
    Dims almost 2 years

    Is it possible to call Java (GWT) methods from Javascript? It is also unclear from documentation. All samples here http://code.google.com/intl/ru/webtoolkit/doc/latest/DevGuideCodingBasicsJSNI.html demonstrate calling java functions from JSNI (not JS) functions.

    UPDATE 1

    Here is a Java code:

    public class Test_GoogleWeb_JSNI_02 implements EntryPoint {
    /**
     * This is the entry point method.
     */
    public void onModuleLoad() {
    }
    
    public static void Callee() {
        Window.alert("Callee");
    }
    }
    

    Here is caller button samples in html:

    <input type='button' value='Call' onclick='Test02()'>
    

    And here are some functions I tried and which were not worked:

    <script type="text/javascript">
    
        function Test01() {
            @com.inthemoon.tests.client.Test_GoogleWeb_JSNI_02::Callee()();
        }
    
        function Test02() {
            com.inthemoon.tests.client.Test_GoogleWeb_JSNI_02::Callee()();
        }
    
    
    </script>
    

    UPDATE 2

    The following worked.

    Java preparation:

    public void onModuleLoad() {
        Prepare();
    }
    
    public static native void Prepare() /*-{
        $doc.calleeRunner = @com.inthemoon.tests.client.Test_GoogleWeb_JSNI_02::Callee();
    }-*/;
    
    public static void Callee() {
        Window.alert("Callee");
    }
    

    Caller:

    function Test03() {
            document.calleeRunner();
    }
    

    Is there a better way?

  • Dims
    Dims over 12 years
    Thanks! Is you update concerns my Update2? I mean do you suggest not to write [email protected]::myFunction(*)(param)? Why?
  • jusio
    jusio over 12 years
    It might prevent GWT from doing some basic optimization. Performance impact most likely won't be big, but you should keep it in mind. Also your way will only work with exposure of static methods, it most likely break if you try to call some instance method.
  • chelo_c
    chelo_c over 8 years
    can you call a javascrypt method that returns an object? and use that object?
  • haui
    haui almost 7 years
    The exposed function should be wrapped with the $entry(...) function, see gwtproject.org/doc/latest/DevGuideCodingBasicsJSNI.html#call‌​ing: `$wnd.exposedMethod = $entry(function(param) {...});
  • sushmitha shenoy
    sushmitha shenoy about 6 years
    Can non static methods be called this way?