How to include a external jar in a GWT (Google Web Toolkit) project?

I have a external jar file named "xxx.jar". I use "xxx.jar" in my GWT project.

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.

[ERROR] Line 45: No source code is available for type org.xxx.ObjectName; did you forget to inherit a required module?

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

  • 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 :/
  • 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?


    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.

    Check out Lars Vogels article with a brief section on this in his tutorial

    It also makes sense, due to the restrictions that Google Outline that all of the code in this JAR may not compile to GWT javascript even if you can get the source.

    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

    Robert Hanson provides a step by step on how to package GWT components

    Good Luck...


    You should have a project xml file under src/com.myproject.blah (mine is called Setup.gwt.xml) which looks something like:

    <?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:

    <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.

    As to point (3), if the extrnal lib only uses that subset of Java the GWT compiler knows about, you're fine.

    I'm not 100% sure the above is correct, but it does seem to work for me.

    链接地址: http://www.djcxy.com/p/46908.html

    上一篇: 用(python)Scipy装配一个pareto发行版

    下一篇: 如何在GWT(Google Web Toolkit)项目中包含外部jar?