Using classes from external jars in GWT project


For an introduction in GWT please see GWT tutorial. The following describes how to use external jars or Java projects within your GWT project.

The standard approach in Java is to have separated projects for separate purposes. For example the domain model of the application is usually defined in its own project. One way of making these classes available to GWT is to copy them into the package “client” in your GWT project. This is bad practice as it leads to code duplication (which is inherently evil). This chapter describes how you can make these projects available to the GWT compiler as modules.

GWT need to have access to the source files to compile them into Javascript code. If you add the project or the jar file to your GWT classpath then the Java compiler will not complain if you use the classes from the included project / jar but the GWT compiler will not be able to compile them.

To make the Java files available to the GWT compiler you need to

  • Create a gwt.xml file in the Java project / jar file which you want to use – This will instruct the GWT compiler to use the listed classes.
  • Use the included library via the inherit definition
  • If you are using a jar file you also need to include the source files in the jar

Here’s a short example *.gwt.xml file:


  
  

To make the classes available for GWT use the inherti tag in your *.gwt.xml file:

, ,

  1. #1 by Aira on 2011/05/09 - 07:10

    Good point. I hadn’t tuhohgt about it quite that way. :)

(will not be published)
*