Support free tutorials:

vogella training Training Books

GWT - Using external jars / Java Projects - Tutorial

Lars Vogel

Version 1.9


Revision History
Revision 0.1 03.04.2007 Lars
Revision 0.2 - 1.9 11.10.2008 - 13.11.2010 Lars
bugfixes and enhancements

GWT Tutorial

This tutorial describes how to use external jars or Java projects in GWT. The article was developed using JDK 1.6, GWT 2.0 and Eclipse 3.6 (Helios)).

Table of Contents

1. Google Web Toolkit
2. Using external jars / Java projects in GWT
2.1. Overview
2.2. Create the module project
2.3. Use the module in another project
3. Support free vogella tutorials
3.1. Thank you
3.2. Questions and Discussion
4. Links and Literature
4.1. GWT resources

1. Google Web Toolkit

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

2. Using external jars / Java projects in GWT

2.1. Overview

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

2.2. Create the module project

Create a Java project "de.vogella.gwt.module.model" and package "de.vogella.gwt.module.model". Create the following class.

package de.vogella.gwt.module.model;

public class Person {
  private String firstName;

/** * @return the firstName */
public String getFirstName() { return firstName; }
/** * @param firstName * the firstName to set */
public void setFirstName(String firstName) { this.firstName = firstName; } }

Create in package "de.vogella.gwt.module" the file "model.gwt.xml" with the following content. This will be the module definition for GWT.

  <inherits name=''/>
  <source path="model"></source>

2.3. Use the module in another project

We want to use this model in a GWT project. Create therefore a new GWT project "de.vogella.gwt.module.application" similar to the first example of this article with the following entry point.

package de.vogella.gwt.module.application.client;


import de.vogella.gwt.module.model.Person;

public class ModulTest implements EntryPoint {

  public void onModuleLoad() {
    Person p = new Person();
    Label label = new Label("Hello " + p.getFirstName());

To make the module project available for the Java compiler, right-click your project, select properties -> Java Build Path and add a dependency to the project "de.vogella.gwt.module.model".

Make the Java class of your new module available to the GWT compiler by using "inherits" in your file "De_vogella_gwt_module_application.gwt.xml".

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE module PUBLIC "-//Google Inc.//DTD Google Web Toolkit 1.6.4//EN" "">
<module rename-to='de_vogella_gwt_module_application'>
  <!-- Inherit the core Web Toolkit stuff.                        -->
  <inherits name=''/>

  <inherits name=''/>
  <inherits name='de.vogella.gwt.module.model'/>

  <!-- Other module inherits                                      -->

  <!-- Specify the app entry point class.                         -->
  <entry-point class='de.vogella.gwt.module.application.client.ModulTest'/>


If you make a typo here you will get an error message: [ERROR] Unable to find 'de/vogella/gwt/module/model2.gwt.xml' on your classpath; could be a typo, or maybe you forgot to include a classpath entry for source?

If everything was setup correctly you can run your application.

3. Support free vogella tutorials

Maintaining high quality free online tutorials is a lot of work. Please support free tutorials by donating or by reporting typos and factual errors.

3.1. Thank you

Please consider a contribution if this article helped you.

Flattr this

3.2. Questions and Discussion

If you find errors in this tutorial, please notify me (see the top of the page). Please note that due to the high volume of feedback I receive, I cannot answer questions to your implementation. Ensure you have read the vogella FAQ as I don't respond to questions already answered there.

4. Links and Literature