java 如何在 GWT(Google Web Toolkit)项目中包含外部 jar?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3243290/
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
How to include a external jar in a GWT (Google Web Toolkit) project?
提问by Stephen Cagle
I have a external jar file named "xxx.jar". I use "xxx.jar" in my GWT project.
我有一个名为“xxx.jar”的外部 jar 文件。我在 GWT 项目中使用“xxx.jar”。
When I attempt to build the JavaScript version of my project in Ant, I get one of the following type of errors at every location in which I use xxx. I get a error of this kind when doing the "gwtc" task in Ant, the javac compilation process proceeds just fine.
当我尝试在 Ant 中构建我的项目的 JavaScript 版本时,我在使用 xxx.xxx 的每个位置都会遇到以下类型的错误之一。在 Ant 中执行“gwtc”任务时出现此类错误,javac 编译过程进行得很好。
[ERROR] Line 45: No source code is available for type org.xxx.ObjectName; did you forget to inherit a required module?
[错误] 第 45 行:没有适用于类型 org.xxx.ObjectName 的源代码;您是否忘记继承所需的模块?
Ok, so clearly it is not able to see/use the xxx.jar. Fixing this problem however is not as simple in GWT as it is in "plain" java. From the internet ref1, I gather that I need to
好的,很明显它无法看到/使用 xxx.jar。然而,在 GWT 中解决这个问题并不像在“普通”java 中那么简单。从互联网ref1,我认为我需要
- Include all the source (.java) files from xxx.jar in a source directory
- Add this source directory in some sort of new gwt.xml file
- Hope and pray that all the java files are translatable by GWT :/
- 将 xxx.jar 中的所有源 (.java) 文件包含在源目录中
- 将此源目录添加到某种新的 gwt.xml 文件中
- 希望并祈祷所有的 java 文件都可以被 GWT 翻译:/
So... What exactly do I do? what is this gwt.xml file I need to generate (Step 2)? Where do I put the source directory, and how to I reference it (Step 1)? What exactly are the mechanical steps necessary to add a external jar file in GWT?
那么……我到底该怎么做?我需要生成的这个 gwt.xml 文件是什么(第 2 步)?我将源目录放在哪里,如何引用它(步骤 1)?在 GWT 中添加外部 jar 文件所需的机械步骤到底是什么?
采纳答案by MadMurf
Because your GWT source has to be compiled to JavaScript to work on the client side browser it makes sense that the source code should be available to the GWT compiler.
因为您的 GWT 源代码必须编译为 JavaScript 才能在客户端浏览器上运行,所以源代码应该可供 GWT 编译器使用是有意义的。
Check out Lars Vogels articlewith a brief section on this in his tutorial
查看 Lars Vogels文章,在他的教程中对此有一个简短的部分
It also makes sense, due to the restrictions that Google Outlinethat all of the code in this JAR may not compile to GWT javascript even if you can get the source.
这也是有道理的,由于Google Outline的限制,即使您可以获得源代码,此 JAR 中的所有代码也可能无法编译为 GWT javascript。
GWT supports only a small subset of the classes available in the Java 2 Standard and Enterprise Edition libraries, as these libraries are quite large and rely on functionality that is unavailable within web browsers. To find out exactly which classes and methods are supported for core Java runtime packages, see the GWT JRE Emulation Reference
GWT 仅支持 Java 2 标准版和企业版库中可用类的一小部分子集,因为这些库非常大,并且依赖于 Web 浏览器中不可用的功能。要准确了解核心 Java 运行时包支持哪些类和方法,请参阅 GWT JRE 仿真参考
Robert Hanson provides a step by step on how to package GWT components
Robert Hanson 提供了有关如何打包 GWT 组件的分步说明
Good Luck...
祝你好运...
回答by BJB
You should have a project xml file under src/com.myproject.blah (mine is called Setup.gwt.xml) which looks something like:
您应该在 src/com.myproject.blah 下有一个项目 xml 文件(我的名为 Setup.gwt.xml),它看起来像:
<?xml version="1.0" encoding="UTF-8"?>
<module rename-to='setup'>
<!-- Inherit the core Web Toolkit stuff. -->
<inherits name='com.google.gwt.user.User'/>
<!-- Inherit the default GWT style sheet. You can change -->
<!-- the theme of your GWT application by uncommenting -->
<!-- any one of the following lines. -->
<inherits name='com.google.gwt.user.theme.standard.Standard'/>
<!-- <inherits name='com.google.gwt.user.theme.chrome.Chrome'/> -->
<!-- <inherits name='com.google.gwt.user.theme.dark.Dark'/> -->
<!-- Other module inherits -->
<inherits name="com.some.external.library.Utils"/>
<!-- Specify the app entry point class. -->
<entry-point class='com.myproject.blah.client.Setup'/>
<stylesheet src="MyStyle.css"/>
</module>
In the build.xml file there is a section:
在 build.xml 文件中有一个部分:
<target name="libs" description="Copy libs to WEB-INF/lib">
<mkdir dir="war/WEB-INF/lib" />
<copy todir="war/WEB-INF/lib" file="${gwt.sdk}/gwt-servlet.jar" />
<copy todir="war/WEB-INF/lib" file="/path/to/external/lib.jar" />
where lib.jar contains the com.some.external.library.Utils source referenced from the gwt.xml file.
其中 lib.jar 包含从 gwt.xml 文件引用的 com.some.external.library.Utils 源。
As to point (3), if the extrnal lib only uses that subset of Java the GWT compiler knows about, you're fine.
至于第 (3) 点,如果外部库仅使用 GWT 编译器知道的 Java 子集,则没问题。
I'm not 100% sure the above is correct, but it does seem to work for me.
我不是 100% 确定上述内容是正确的,但它似乎对我有用。

