Google has provided a new choice. Google has provided what they call the Google Web Toolkit or GWT. Through use of GWT, you can develop Java programs which run from within your browser. Without GWT, it is possible to write Java programs that can run from within your browser but these would have to be developed as Java applets, in which case you would use one of the above methods of developing Java programs. It is also possible to write JavaScript programs to run from within your browser, but JavaScript is not true Java. Therefore, if your interests lie in learning Java, then JavaScript does not contribute much to your objective. However, GWT allows you to write Java programs, and GWT converts your Java source into JavaScript so you can run your code from your browser. There are some limitations with GWT but as a method for learning Java, it provides a new alternative.
This is not a recommendation to use GWT in place of writing a Java application using one of the mentioned Java development environments, rather it is just pointing out that an alternative exists and that after you have been able to begin writing Java programs using GWT, you should feel confident to transition to one of the Java development environments.
How to get started
One of the advantages of GWT is that it is easy to get a program up and running and get immediate feedback. Immediate feedback can provide motivation for continuing to advance your skills. Writing Java programs with GWT still requires a prerequisite knowledge of Java, so if you are learning Java from scratch, I would recommend the tutorials on the Sun website.
How to get and install GWT
You can download GWT as a zip file. There is no installer application. You just unzip the file where you would like the files to reside. (Upon opening the zip file, one of the tasks which appear on the left is to "Extract all files".)
To use GWT in command line mode, you'll need to navigate to where you have extracted the toolkit. When you're in the right directory, you'll find applicationCreator.cmd
Use this command to create your application structure. For example:
> applicationCreator -out . myprog.client.HelloWorld
This will create a new project HelloWorldwith the main class of the same name. The package name is myprog.client. You'll then find that myprogis created in the GWT directory structure under the src directory:
The client subdirectory is for the source code. The public subdirectory is for other files which you can place other files which you need to run your code, such as your html files, graphic files, and so forth.
How to create and run your first program
Upon creation of your application structure, by default, the initial source file will contain the following HelloWorld program:
package myprog.client;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Widget;
public class HelloWorld implements EntryPoint {
public void onModuleLoad() {
final Button button = new Button("Click me");
final Label label = new Label();
button.addClickListener(new ClickListener() {
public void onClick(Widget sender) {
if (label.getText().equals(""))
label.setText("Hello World!");
else
label.setText("");
}
});
//
RootPanel.get("slot1").add(button);
RootPanel.get("slot2").add(label);
}
}
With this program, a new button button is created and initialized to "Click me" and a new label label is created which is initialized to a null value. The button is located in slot1 and the label is located in slot2.
You'll find in the public directory, the html file has a table defined with slot1 and slot2.
<table align="center">
<tbody>
<tr>
<td id="slot1"></td><td id="slot2"></td>
</tr>
</tbody></table>
Therefore, the button and label will appear within the browser corresponding to where the table cells slot1 and slot2 appear.To run your program within the Google GWT Development Shell (simulated execution environment), you would enter in the command line:
> HelloWorld-shell
The result is the following appearing. By clicking the button, then the "Hello World!" message appears.
Once you have tested your program and would like to place your files on a web server so you can run your files, you would enter in the command line:
> HelloWorld-compile
Note that applicationCreator created the HelloWorld-shell and HelloWorld-compile commands for you based on the project name you specified when you created your application structure.
The compile command will create all the files you need to load onto the web server to execute your code. These are placed in a project directory in the www directory.
One advantage of a product like GWT is that there is an endless supply of people who are willing to share their knowledge of how to use GWT.
As you see, GWT is very easy to start using. Although easy to use to create a simple client application, GWT also supports client/server applications.