Trying to built-in JSNI support to a legacy GWT project. I've added simple JSNI alert function which is called from button's onclick handler -
public static native void JSAlert(String msg)
/*-{
$wnd.alert(msg);
}-*/;
public class JSNITestCommandHandler extends AbstractEventListener<JSNITestCommand> {
@Override
public void execute(JSNITestCommand event) {
JSAlert("TEST JS ALERT");
}
}
When I click on the button in the browser, it shows "Executing..." for about 10 seconds, and then error message appears -
------------- SOMETHING WENT WRONG ------------- com.gotapi.hcsapplication.client.XLcmApplicationClient.JSAlert(Ljava/lang/String;)V
java.lang.UnsatisfiedLinkError: com.gotapi.hcsapplication.client.XLcmApplicationClient.JSAlert(Ljava/lang/String;)V
What to do to fix this?
This error indicates you are trying to run GWT-only code from within a normal JVM (i.e. not legacy "dev mode", or by actually executing the code in the browser). Specifically, JSNI or JsInterop native
calls cannot be run in the JVM.
GWT is made to let you compile Java to run in the browser, not to let you run JS in the JVM. Old JSNI code or new JsInterop is how you write Java to describe something that will happen within the browser, outside of what Java can understand. It cannot be run in a normal JVM.
To run unit tests, use GWTTestCase as the superclass of your test and use the gradle/maven GWT plugin to invoke the test. This will let GWT compile your Java into JS, and then run it all (including the JSNI) within whatever browser you configure.