JSNI Hello World无法正常工作

时间:2012-06-07 15:55:43

标签: javascript gwt jsni

我使用JSNI做了一个简单的 Hello World GWT示例。除了显示消息外,它什么都不做。

这是EntryPoint类中的代码:

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)

知道为什么吗?

2 个答案:

答案 0 :(得分:1)

我创建了示例GWT项目并将您的代码复制到入口点,它运行正常。我认为你有更深层次的设置问题。我会查看你的* .gwt.xml文件并确保它没有格式错误并检查你的GWT库引用等。同时打开Run Configration并确保它是一个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!"); 
    }
}

答案 1 :(得分:0)

您可能没有在DevMode中运行此代码,但在普通的JVM中,GWT无法执行 magic