编译GWT失败 - 类型没有可用的源代码

时间:2014-07-16 21:35:13

标签: java gwt

我有2个模块:GWT和Java(SPring等)。 小解释:在Java模块中,我有一个Java Mail Sender方法,我想在GWT Widget中使用它。该方法有2个参数:java.util.List<User> recipientsString message。  有一个例子

Project
  -GWT
  -Java(Spring and etc)

我需要什么(在这里您可以看到我的尝试以及下面的代码将是我所拥有的代码):
1)我想在我的类中调用在Composite类上继承的java方法。然后我得到以下错误:
No source code is available for type my.packet.proj.MyClass; did you forget to inherit a required module?
2)我了解到在GWT中调用Java代码(不是全部)是不可能的,我使用JSNI(我编写了一些原生方法)来实现这一点,但这是不可能的对我来说,不幸的是...... 我有一个代码示例。

public class MyClass extends Composite{

   @UiField
   TextBox text;

   @UiField
   Button btn;



   interface EmailMailingUiBinder extends UiBinder<HTMLPanel, MyClass> {
   }

   private static MyClassUiBinder myUiBinder = GWT.create(MyClassUiBinder.class);
   /*@param mailService is an object of messaging class in Java module*/

   public MyClass(MailService mailService){
     btn.addClickHandler( (event) ->{

     sendOnClick(mailService, recipients ,message); //code below
    });

    public native void sendOnClick(MailService mailService, List<User> recipients,   String msg)/*-{
       mailService.@my.packet.proj.MailService::sendMessage(Ljava/util/List,/*and so on*/)(recipients, msg)
     }-*/
}

当Gradle编译Gwt时,上面的代码没有错误。但是当我创建一个MyClass实例时,我需要在MainPanel中调用它(因为它的小部件,页面),如下所示:new MyClass(new MailService()) compileGwt FAILED with compilation errors

No source code is available for type my.packet.proj.MailService; did you forget to inherit a required module?

伙计们,您知道如何将实例作为参数传递吗?

P.S。我删除了一些不必要的代码 P.S.S我甚至尝试过(创建我的MyClass实例):

new MyClass(mailServiceInstance());

/*and anywhere below*/
//this is a X line below, for explanation
public native MailService mailServiceInstance()/*-{
        return @my.packet.proj.MailService::new()();
    }-*/;

我在X行上有同样的错误

No source code is available for type my.packet.proj.MailService; did you forget to inherit a required module?

1 个答案:

答案 0 :(得分:0)

我认为你误解了GWT的主要基础,GWT是在客户端运行的,它意味着在浏览器中运行的纯javascript。因此,必须编写要编译为javascript的每个Java代码,因为知道运行时是一个具有很多限制而不是标准JVM的浏览器。

你想运行一个类来发送被认为在JVM中运行的电子邮件,可能你的my.packet.proj.MyClass想要打开套接字连接到smtp服务器等,这是不可能在JS中模拟的因为您无法打开该服务器的正常套接字。这只是您在客户端的大量限制列表的一个示例。

所以,所有可以在客户端运行的java代码都来自gwt兼容的库,这段代码用java编写为gwt,或者可能是一些代码最初可以用java编写并移植到gwt。在.jar文件中提供的这些库必须包含它在项目中继承的模块定义(gwt.xml),原始java源代码等。事实上,GWT编译器不使用字节代码(.class)文件,虽然在IDE等中可能需要它。

说,为了使用任何不为GWT编写的java代码,你必须:

  1. 将它移植到GWT,创建一个你必须继承的.gwt.xml文件,先验证所有代码不会破坏浏览器限制,不依赖于非gwt 3party库等。
  2. 在服务器端运行它,并从浏览器进行ajax调用。
  3. 所以在你的情况下,最简单的方法,我猜唯一的方法是,你在.war中包含my.packet.proj.MyClass,并创建一个RPC service(或任何ajax解决方案)客户端发送邮件。

相关问题