14

I have a GWT project and I would like to add a script tag to the main html file of the GWT project that calls a Java function located in my client code.

According to the documentation I should add something like the following html tag:

<script type='text/javascript'> 
this.@com.myCompany.myProject.client.myClass::myFunction();
</script>

where com.myCompany.myProject.client.myClass is the class path and myFunction is the java function I would like to call.

When I try this with the following implementation of myFunction nothing happens:

public void myFunction() {
    HTMLPanel panel = new HTMLPanel("I have been called");
    RootPanel.get().add(panel);
}

That is, myFunction is not being called.

But when I make the same call from a JSNI method, then it works.

Is it maybe not possible to do the call from an html script, or am I doing something wrong?

Thanks!

Mike
  • 223
  • 2
  • 4
  • 7

1 Answers1

13
  1. What you are trying to do does not work because GWT compiler renames all identifier names to minimize produced code size: so myFunction() exists, but it's called something else.

  2. You were looking at old version of documentation. In the latest version this is all explained: Calling a Java Method from Handwritten JavaScript

The solution - add an additional method somewhere:

public static native void exportMyFunction() /*-{
   $wnd.myFunction =
      $entry(@com.myCompany.myProject.client.myClass::myFunction());
}-*/;

then in your app initialization you must call EnclosingClass.exportMyFunction(). Then in hand-crafted javascript you can access it via:

window.myFunction();
Peter Knego
  • 79,991
  • 11
  • 123
  • 154
  • 1
    I am trying to solve a similar problem, just a question, how to pass String to myFunction()? like myFunction(String s) ? – quarks Aug 18 '11 at 09:48
  • 1
    Look atbthis doc to see hoe to add arguments to JSNI calls: http://code.google.com/webtoolkit/doc/latest/DevGuideCodingBasicsJSNI.html#methods-fields – Peter Knego Aug 18 '11 at 10:28
  • 2
    Also in this answer there are complete examples: http://stackoverflow.com/questions/5234735/gwt-jsni-problem-passing-strings – Peter Knego Aug 18 '11 at 10:37
  • How does this work if myFunction() is an instance method and not static? How does the JavaScript know which instance of myFunction() to call? It doesn't. I'd be curious to hear if this solved the problem, because I have the same situation and it throws an exception for that reason. I don't think you can call a non-static GWT Java method from handwritten JavaScript, because in your app initialization when you export the native function, you don't have an instance of EnclosingClass yet, and you have a chicken-and-egg problem. – jewbix.cube May 21 '14 at 21:16
  • @axle123 it's all in the docs: http://www.gwtproject.org/doc/latest/DevGuideCodingBasicsJSNI.html#methods – Peter Knego May 21 '14 at 21:41
  • I re-read the docs and still come to the same conclusion. In order to put the [instance-expr.] before the @className, you'd have to pass the instance as an argument to exportMyFunction. But you don't have the instance yet on app initialization. – jewbix.cube May 21 '14 at 21:52
  • This works for calling an Java instance method from a native method, but not from *handwritten* JavaScript. That's because you have to do an intial export of your method to JavaScript (just like this answer and the docs show). But how would you export a single JavaScript function that can be used in the future to call instance methods for instances that don't even exist yet? There's a reason why the example in the docs for "Calling a Handwritten Java Method From JavaScript" shows two static methods. If anyone can do that with an instance method I'll buy you a six-pack. – jewbix.cube May 21 '14 at 22:19