UnsatisfiedLinkError 使用 JSNI 和 vaadin

UnsatisfiedLinkError using JSNI with vaadin

我创建了一个Vaadin项目,我使用JSNI来编写脚本。但是当执行到JSNI脚本时,却报错

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[=10=](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]

以下是我的示例代码。

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");
    }-*/;

}

非常感谢任何帮助。

如果您查看 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 与您的确切问题。在 Vaadin 开发团队的 Henry Sara 的回复下方添加:

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.

虽然答案可能已经改变,Root.executeJavaScript("..."); 现在可能是 JavaScript.getCurrent().execute(...)");,但 Vaadin 文档提供了有关 javascript interactions to and from the serverside as well as on js components and extensions and also, you can check the wiki 的更多示例信息。