Search code examples
vaadinjsnivaadin-spring-boot

UnsatisfiedLinkError using JSNI with vaadin


I have created a Vaadin project, and I used JSNI for scripting. But when execution reaches at JSNI script, it shows an error.

java.lang.UnsatisfiedLinkError: com.yty.cws.CiwsUI.jsniDemo()V
    at com.yty.cws.CiwsUI.jsniDemo(Native Method) ~[classes/:na]
    at com.yty.cws.CiwsUI.lambda$0(CwsUI.java:31) ~[classes/:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_92]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_92]

The following is my sample code.

public class CiwsUI extends UI{

    private static final long serialVersionUID = 5275145103992848572L;
    VerticalLayout mainLayout=new VerticalLayout();
    private TextField txtName=new TextField();
    private Button btnJSNI=new Button("JSNI");
    @Override
    protected void init(VaadinRequest request) {

        mainLayout.addComponents(txtName,btnJSNI);
        setContent(mainLayout);
        mainLayout.setComponentAlignment(txtName, Alignment.MIDDLE_CENTER);
        mainLayout.setComponentAlignment(btnJSNI, Alignment.MIDDLE_CENTER);
        btnJSNI.addClickListener(e->{
            System.out.println("Clicked");
            jsniDemo();
        });
    }
    private native void jsniDemo()/*-{
    $wnd.alert("Hai JSNI");
    }-*/;

}

Any help is greatly appreciated.


Solution

  • If you look at the differences between GWT & Vaadin you'll notice that besides the cliend-side API, Vaadin also has a server-side API, and the code you posted falls into that category. Searching the Vaadin forum I found this question with your exact problem. Adding below the reply of Henry Sara from the Vaadin dev team:

    JSNI is only usable in the client side widgets whereas you are working on the server side.

    Use Window.executeJavaScript("...") (Vaadin 6) or Root.executeJavaScript("...") (Vaadin 7) if you need to execute JavaScript from your server side application.

    While things may have changed since the answer and Root.executeJavaScript("..."); is now probably JavaScript.getCurrent().execute(...)");, the Vaadin docs offer information on javascript interactions to and from the serverside as well as on js components and extensions and also, you can check the wiki for more examples.