JSNI Hello World is not working

JSNI Hello World is not working

本文关键字:not working is World Hello JSNI      更新时间:2023-09-26

我用JSNI做了一个简单的Hello World GWT示例。它只显示一条消息。

下面是入口点类中的代码:

public void onModuleLoad() {
  // TODO Auto-generated method stub
  alert("Hello World!");
 }
 native void alert(String msg) /*-{
  $wnd.alert(msg);
 }-*/;
}

我看到此异常:

java.lang.reflect.InvocationTargetException
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)

知道为什么吗?

我创建了示例 GWT 项目并将您的代码复制到入口点,它工作正常。我认为您有一个更深层次的设置问题。我会查看您的 *.gwt.xml 文件并确保它没有格式不正确,并检查您的 GWT 库引用等。还要打开运行配置并确保它是一个 Web 应用程序。

    /**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class So implements EntryPoint {

    native void alert(String msg) /*-{ 
      $wnd.alert(msg); 
     }-*/; 
    /**
     * This is the entry point method.
     */
    public void onModuleLoad() {
        alert("Hello World!"); 
    }
}

您可能不是在 DevMode 中运行此代码,而是在普通的 JVM 中运行此代码,GWT 无法发挥其魔力