简体   繁体   English

将Java包添加到GWT

[英]Adding Java packages to GWT

I've tried searching but couldn't come up with a defined way on how to add your own packages to a GWT project. 我已经尝试过搜索,但是无法想出如何将自己的包添加到GWT项目中。

My tree structure looks like this: 我的树结构如下所示:

-com.mycompany
  -public
    MyApplication.html
  MyApplication.gwt.xml


-com.mycompany.client
  MyApp.java

-com.mycompany.gui
  TableLayout.java

The answer I've seen out there says to add the packages relative to the root directory of the gwt.xml file, like so: 我在那里看到的答案是说相对于gwt.xml文件的根目录添加包,如下所示:

<module>
  <inherits name="com.google.gwt.user.User" />
  <entry-point class="com.mycompany.client.MyApp" />
  <source path="client" />
  <source path="gui" />
</module>

It then complains: 然后抱怨:

Unable to find type 'com.technicon.client.MyApp'
   Hint: Previous compiler errors may have made this type unavailable
   Hint: Check the inheritance chain from your module; it may not be inheriting a required module or a module may not be adding its source path entries properly

Can anyone tell me what I'm doing wrong and how to fix this? 任何人都可以告诉我我做错了什么以及如何解决这个问题?

even though, as @rustyshelf pointed out, gwt will convert everything that is under client.* automatically, there will be times when you will want to keep things outside of your client packages (reusing them in several project might be one of them) and for that the solution still resides in adding other packages to the process using the source element. 尽管如此,正如@rustyshelf指出的那样,gwt将转换client.*下的所有内容client.*有时候,你会想要将东西保存在客户端软件包之外(在多个项目中重用它们可能就是其中之一)为此,解决方案仍然在于使用source元素向流程中添加其他包。

now there is a trick, you have to decide whether you want to move the gwt.xml config file or whether you need to create a new one. 现在有一个技巧,你必须决定是否要移动gwt.xml配置文件或是否需要创建一个新文件。

for your case in particular (where both packages share a root in the package, com.mycompany) you can just move the <project_name>.gwt.xml file to the top most common package and just add the new package as a source (and keeping the <source path="client"/> there as well) thus making your file to look like: 特别是对于您的情况(两个包共享包中的根,com.mycompany),您只需将<project_name>.gwt.xml文件移动到最常见的包,只需添加新包作为源(和同时保持<source path="client"/> ,从而使您的文件看起来像:

<source path="client"/>
<source path="gui"/>

on the other hand if the packages don't share any root, just create a new *.gwt.xml file with only the source elements and place it on a parent package to the sub-package you want to add, ie: 另一方面,如果包不共享任何根,只需创建一个仅包含源元素的新*.gwt.xml文件,并将其放在父包中,添加到要添加的子包中,即:

<module>
   <source path=""/>
</module>

note that if you need to give compilation-access to nested sub-packages do so by separating them with a / like in "admin/client" 请注意,如果您需要对嵌套子包进行编译访问,请使用"admin/client"/ like分隔它们。

hope this help you get back on track and organize your code the best way possible. 希望这有助于您回到正轨并以最佳方式组织代码。

You can get rid of the two source path lines, because by default GWT will pick up anything that is relative to the root, and in the client package like you have. 您可以摆脱两个源路径行,因为默认情况下,GWT会选择与根相关的任何内容,并在客户端包中提取。 You also need to move your gui package into your client package, so it would become: 您还需要将gui包移动到您的客户端包中,因此它将变为:

-com.mycompany
  -public
    MyApplication.html
  MyApplication.gwt.xml


-com.mycompany.client
  MyApp.java

-com.mycompany.client.gui
  TableLayout.java


<module>
  <inherits name="com.google.gwt.user.User" />
  <entry-point class="com.mycompany.client.MyApp" />
</module>

Assuming your MyApp.java is an actual EntryPoint, then this should work just fine. 假设您的MyApp.java是一个真正的EntryPoint,那么这应该可以正常工作。

One other thing to note is that you cannot use java classes that are not part of the GWT JRE Emulation library, and your project won't compile if you do. 另外需要注意的是,您不能使用不属于GWT JRE Emulation库的java类,如果您这样做,您的项目将无法编译。 You should get very specific errors about this though. 你应该得到关于这个的非常具体的错误。 For example you cannot use library classes like java.math.BigDecimal, if they are not emulated. 例如,如果未模拟它们,则不能使用java.math.BigDecimal等库类。 All of your own classes you create can be used though. 您创建的所有类都可以使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM