GWT - adding external java classes to client project
Solution 1
If you have the java source files you just need to add the directory to you .gwt.xml file. For instance if you had a subdirectory called shared you would add the following line:
<source path='shared'/>
The folder called shared would have to be one level under your main package. So if you project .gwt.xml file is at com.yourdomain.project the shared folder.package would be com.yourdomain.project.shared. Refer to the Source Path section at http://code.google.com/webtoolkit/doc/latest/DevGuideOrganizingProjects.html#DevGuideModules
If you dont have the source and only have the classes you have to import a module as Hilbrand has stated.
Solution 2
Here is another solution that works http://www.gordonizer.com/2012/01/referencing-third-party-library-source.html .
I short (just in case this link also gets invalid some day):
- Suppose the external package containing the classes you want to add is
com.foo.bar.bat
. - In your project, you create package
com.foo.bar
(without the subpackagebat
). -
In your new package you create a new GWT module file
Foo.gwt.xml
with content like<module>
<source path="bat" />
</module>
(I have ommitted the XML header for sake of readability)
-
Finally add this new module to your main GWT module using the
inherit
tag:<inherits name="com.foo.bar.Foo"/>
Solution 3
Any Java classes to be used by GWT must have a module file and conform to a package structure that includes a sub package. See this stackoverflow answer for more details: Adding Java packages to GWT. In this case a module file (e.g. model.gwt.xml
) could be created in the directory com.dominolog.locateme.model
that contains the following content:
<module>
<source path="dto" />
</module>
Add a reference to this module file in your main module file and GWT will then take all classes in the com.dominolog.locateme.model.dto
package.
2 notes:
GWT will look at all classes in the directory (and subdirectories)
The classes in the package must be present in source files and may not contain any references to other libraries not parseable by GWT (This might be a limitation when in the dto classes annotations are used that refer to specific database usages).
Update: Rewritten answer to be more specific.
Comments
-
cubesoft almost 2 years
I have a GWT project. Client code is located in the "client" dir. I want to attach an external java classes (mainly plain POJO DTO classes) that are in external directory. How to configure the gwt.xml file?
I get errors of this kind:
[ERROR] Errors in 'file:/C:/development/projects/CodeSpaces/LocateMe/LocateMeWeb/src/com/dominolog/locateme/client/LocateMeWeb.java' [ERROR] Line 56: No source code is available for type com.dominolog.locateme.model.dto.LocationInfo; did you forget to inherit a required module?
-
cubesoft about 14 yearsThanks but the post you refer to does not explain anything but that I have to add all my code under the client directory. This is not acceptable for me.
-
cubesoft about 14 yearsOk. But what if the code I want to add is not under "client" ie it is an external directory within my workspace?
-
Carnell about 14 yearsIs it in the same project? If so I assume it starts with the same package (com.yourdomain.project in my example) and what I have outlined will work. It does not need to be under client. If its coming from another source/project I don't know how/if you can add it to the project.
-
cubesoft about 14 yearsI'll try to be more specific. My workspace structure is: Workspace-> LocateMeModel LocateMeServer LocateMeMobile LocateMeWeb The LocateMeWeb is a GWT application with standard project layout. In LocateMeModel I have Java classes that define dto (data transfer objects) that are common for LocateMeWeb, LocateMeMobile and LocateMeServer. Therefore, I need that GWT takes files from this directory (LocateMeModel) and compiles it as a client code. Is it possible at all? From what I see GWT allows only that the code MUST BE under client directory with GWT project.
-
Erik Kubica almost 9 yearsi had same problem, in netbeans i had need to remove "exclude from jar" the .java files