Over a million developers have joined DZone.

GWT: Running a completed project

DZone's Guide to

GWT: Running a completed project

· Java Zone ·
Free Resource

Java-based (JDBC) data connectivity to SaaS, NoSQL, and Big Data. Download Now.

[img_assist|nid=3421|title=|desc=|link=url|url=http://www.manning.com/affiliate/idevaffiliate.php?id|align=left|width=208|height=388]Let's put GWTShell and GWTCompiler to use with a calculator example, CalculatorWidget. First, we'll use GWTShell to get the lay of the land and launch the hosted mode browser. This is how you typically runĀ and debug a project while in hosted mode during development. Then we'll move on to compiling things with GWTCompiler, which is what is normally used as part of a build process to turn your Java into JavaScript and create a deployable artifact for web mode.

Hosted mode and the GWT shell

When you run ApplicationCreator, GWT creates some shortcut scripts for invoking GWTShell and GWTCompiler with the project. For this example, we'll first execute GWTShell using the [PROJECT_HOME]/Calculator-shell script (it will have a platform-dependent extension). The next step is to ensure that the environment is configured as it needs to be to support GWT and the project itself, and then invoke Calculator-shell. It should successfully invoke the example in the shell as shown in figure 1.


Figure 1 The GWT shell and hosted mode browser running CalculatorWidget

The background window in figure 1 is the GWT shell, and the foreground is the hosted mode browser running CalculatorWidget. The GWT shell includes a logging console where any problems will be displayed. This includes exceptions, which are passed up the call stack. The shell also includes buttons for manipulating the log and launching the hosted mode browser.

Can the GWT shell browser be specified?

When you launch the GWT shell, you'll notice that the hosted mode browser is the system browser for the platform you are using. This means Internet Explorer on Windows, Safari on Mac, and Mozilla Firefox on Linux. The hosted mode browser is actually based on SWT browser components, so it is not currently possible to specify or configure which browser is used when running the shell. (This will change in the near future with out-of-process hosted mode, which will allow you to use any supported browser on a platform.)

Recall that the shell and the hosted mode browser execute your project as Java bytecode, as opposed to JavaScript, when executed in web mode (which we'll get to in a moment). This means that from hosted mode, in addition to being able to quickly recompile your code with the Refresh button, you can also use Java debuggers and profilers, as you normally would with any Java project.

To connect to the GWT shell for the purposes of debugging, you can use the Java Platform Debugger Architecture (JPDA), which is built into most JVM implementations, and your favorite IDE. By invoking com.google.gwt.dev.GWTShell with the following command-line options, you can force the GWT shell to delay startup until a debugger has connected:

-Xdebug -Xnoagent -Djava.compiler=NONE

Using this technique, you can run the shell outside of your IDE, and then connect to it with Eclipse, or NetBeans, or such, at localhost:8888. Once this connection is made, the socket listener resets to the HTTP listener from the shell and you can continue your step-through debugging as you normally would.

Within the hosted mode browser, you'll notice the standard browser navigation buttons and a special Compile/Browse button that invokes the GWT compiler for the project. This takes you out of hosted mode (Java) and into web mode (JavaScript).

Web mode and the GWT compiler

GWTCompiler is the GWT facility that compiles your Java source path code into JavaScript, and bundles it with your public path resources in a deployable project form. The Compile/Browse button made available in the GWT shell will compile your project, deploy it in a stripped-down development version of Apache Tomcat on the local host (actually, the GWT shell also uses this mini Tomcat for any service servlets involved), and launch the project in the default configured browser for the machine. As the screenshot in figure 2 shows, the browser used for web mode can be specified (the machine default can be configured), so it can be different from the system browser that hosted mode must use inside the shell.


Figure 2 CalculatorWidget running in web mode, using Firefox on a Mac, after clicking the Compile/Browse button in the GWT shell

With the compile step, you are switching out of hosted mode and into web mode - running your code as JavaScript instead of directly as Java. In addition to the Compile/Browse button in the shell, the shortcut ProjectName-compile script, created by ApplicationCreator, can also be used to compile your project (which will place it by default in the [PROJECT_HOME]/www location). You can, of course, also use the GWT compiler manually, or with an IDE plugin, or by using build tools such as Ant or Maven.

This article is excerpted from Chapter 1 of GWT in Practice, by Robert Cooper and Charlie Collins, and published in May 2008 by Manning Publications.

Connect any Java based application to your SaaS data.  Over 100+ Java-based data source connectors.


Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}