将 Java 包添加到 GWT
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/440018/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding Java packages to GWT
提问by Organiccat
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?
谁能告诉我我做错了什么以及如何解决这个问题?
采纳答案by rustyshelf
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. You also need to move your gui package into your client package, so it would become:
您可以去掉两个源路径行,因为默认情况下 GWT 会选择与根相关的任何内容,并且像您一样在客户端包中。你还需要将你的 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. 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. All of your own classes you create can be used though.
另一件需要注意的事情是,您不能使用不属于 GWT JRE 仿真库的 Java 类,如果这样做,您的项目将无法编译。不过,您应该会收到非常具体的错误信息。例如,您不能使用像 java.math.BigDecimal 这样的库类,如果它们没有被模拟。不过,您创建的所有类都可以使用。
回答by samiq
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.*
自动转换所有内容,但有时您会希望将内容保留在客户端包之外(在多个项目中重用它们可能是其中之一),为此解决方案仍然在于使用该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, i.e:
另一方面,如果包不共享任何根,只需创建一个*.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"
hope this help you get back on track and organize your code the best way possible.
希望这可以帮助您回到正轨并以最佳方式组织您的代码。