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

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

项目
   -GWT
   -Java(Spring等)

我需要什么(在这里你可能会看到我的尝试,下面将是我的代码):
1)我想在我的类中调用在Composite类上inheritance的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 { } 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 recipients, String msg)/*-{ mailService.@my.packet.proj.MailService::sendMessage(Ljava/util/List,/*and so on*/)(recipients, msg) }-*/ } 

当Gradle编译Gwt时,上面的代码没有错误。 但是当我创建一个MyClass实例时,我需要在MainPanel中调用它(因为它是widget,page),如下所示: 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? 

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

PS我删除了一些不必要的代码PSS我甚至尝试过(创建我的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? 

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

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

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

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

  1. 将它移植到GWT创建一个你必须inheritance的.gwt.xml文件,先validation所有代码不会破坏浏览器限制,不依赖于非gwt 3party库等。
  2. 在服务器端运行它,并从浏览器进行ajax调用。

因此,在您的情况下,最简单的方法,我想唯一的方法是,在.war中包含my.packet.proj.MyClass ,并创建一个RPC服务 (或任何ajax解决方案),您可以从客户端使用发了邮件。