Tutorial of Java2Script SWT and Simple RPC Application

In this article, I will show you a simple example using Java2Script Simple RPC (For “what is Java2Script Simple RPC? “, please read this post).

Step 1. Create a Java2Script Servlet Project

Here are instructions: File -> New -> Project … -> Java2Script -> Java2Script Servlet Proejct -> Next -> Key in project name and select “Create separate folders for sources and class files” -> Next or Finish or Finish on next page

Create Java2Script Project Menu Screenshot

Create Java2Script Project Select Screenshot

Create Java2Script Project Wizard Screenshot

When finish this step, try to expand the project sources, you should get similar structures as below:

Java2Script Servlet Project Sources Structure

Later, I will explain more about every files.

Step 2. Create an SWT Application

Here are instructions: Select “src” source folder and create package named “org.java2script.demo.simplerpc”, and create a class name “SimpleSWTRPC” in that package. The class’ source is as following:

package org.java2script.demo.simplerpc;

import net.sf.j2s.ajax.SimpleRPCSWTRequest;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class SimpleSWTRPC extends Shell {

private Text responseText;
private Text requestText;
private Label statusLabel;
* Launch the application
* @param args

public static void main(String args[]) {
try {
Display display = Display.getDefault();
SimpleSWTRPC shell = new SimpleSWTRPC(display, SWT.SHELL_TRIM);
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
} catch (Exception e) {

* Create the shell
* @param display
* @param style

public SimpleSWTRPC(Display display, int style) {
super(display, style);
setLayout(new FillLayout());

* Create contents of the window

protected void createContents() {
setText("Hello SWT & Simple RPC");
setSize(371, 300);

final Composite composite = new Composite(this, SWT.NONE);
composite.setLayout(new GridLayout());

final Label requestLabel = new Label(composite, SWT.NONE);
final GridData gd_requestLabel = new GridData(SWT.FILL, SWT.CENTER, true, false);

requestText = new Text(composite, SWT.MULTI | SWT.BORDER);
final GridData gd_requestText = new GridData(SWT.FILL, SWT.FILL, true, true);
gd_requestText.heightHint = 80;
gd_requestText.minimumHeight = 80;

final Button sendButton = new Button(composite, SWT.NONE);
sendButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
statusLabel.setText("Sending request ...");
String text = requestText.getText();
text = "[Server echo]:" + text;
statusLabel.setText("Server responded.");
final GridData gd_sendButton = new GridData(SWT.RIGHT, SWT.CENTER, false, false);
sendButton.setText("Send Simple RPC Request");

final Label reponseLabel = new Label(composite, SWT.NONE);

responseText = new Text(composite, SWT.MULTI | SWT.READ_ONLY | SWT.BORDER);
final GridData gd_responseText = new GridData(SWT.FILL, SWT.FILL, true, true);
gd_responseText.heightHint = 80;

statusLabel = new Label(composite, SWT.BORDER);
final GridData gd_statusLabel = new GridData(SWT.FILL, SWT.CENTER, true, false);

protected void checkSubclass() {
// Disable the check that prevents subclassing of SWT components


Use Instantiations’ WindowBuilder Pro to Design SWT Application

I used Instantiations’ WindowBuilder Pro (also known as SWT Designer) to generate the above codes. I only wrote a few lines as:

sendButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
statusLabel.setText("Sending request ...");
String text = requestText.getText();
text = "[Server echo]:" + text;
statusLabel.setText("Server responded.");

Step 3. Run as Java Application and Java2Script.

Use context menu to “Run as” -> Java Application, and “Run as” -> Java2Script Application. You should get things work.

Java2Script Example Run as Native SWT Desktop Application

Java2Script Example Run as Java2Script Application Inside Browser

Step 4. Add Simple RPC into Example

Now it is time to move

text = "[Server echo]:" + text;

to the server side.

First, create a new EchoRunnable class extending net.sf.j2s.ajax.SimpleRPCRunnable as follow:

package org.java2script.demo.simplerpc;

import net.sf.j2s.ajax.SimpleRPCRunnable;

public class EchoRPCRunnable extends SimpleRPCRunnable {

public String text;

public void ajaxRun() {
text = "[Server echo]:" + text;


Then modify button’s event handler as follow:

sendButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
statusLabel.setText("Sending request ...");
SimpleRPCSWTRequest.swtRequest(new EchoRPCRunnable() {

public void ajaxIn() {
text = requestText.getText();;

public void ajaxOut() {
statusLabel.setText("Server responded.");

public void ajaxFail() {
statusLabel.setText("Request failed.");


Re-test the application in native Java SWT application mode. It should work as expected.

Step 5.? Deploy Java2Script Simple RPC Application

Open WEB-INF/web.xml and add org.java2script.demo.simplerpc.EchoRunnable to simple.rpc.runnables list. Here is the web.xml:

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
<description>Java2Script application</description>
Qualified names of inherited net.sf.j2s.ajax.SimpleRPCRunnable
classes, seperated by ";".

Select build.xml file, right click to bring up context-menu and “Run as” -> Ant Build, and then select the project and right click and select “Refresh” to refresh the project files, you will see a “org.java2script.demo.simplerpc.war” file. Now try to deploy this war file to a Java servlet container, such as Tomcat server.

If your Tomcat? server is installed in localhost, try url:


to deploy the above mentioned war file.

Deploy Java2Script Simple RPC Application *.war Package

But you haven’t finish deployment yet, because you still need to deploy Java2Script’s core *.js library files to the server.

Add the following to build.xml:

<target name="pack.plugins.j2slib.war">
<delete file="${basedir}/../plugins.war" quiet="true"/>
<zip destfile="${basedir}/../plugins.war">
<fileset dir="${basedir}/../../../plugins/">
<include name="net.sf.j2s.lib_1.0.0/**"/>
<exclude name="net.sf.j2s.lib_1.0.0/library.jar"/>
<exclude name="net.sf.j2s.lib_1.0.0/plugin.xml"/>
<exclude name="net.sf.j2s.lib_1.0.0/META-INF/**"/>

And “Run as” -> “Ant Build …”, select “pack.plugins.j2slib.war” in the “Targets” tab and “Run”. Then refresh the project, and deploy the “plugins.war” to Tomcat server.

And after all things deployed, please visit:


Here is the online demo of this Simple RPC example.

(To be continued)

This entry was posted in Tutorials. Bookmark the permalink.

2 Responses to Tutorial of Java2Script SWT and Simple RPC Application

  1. Arun Christopher says:

    i deployed the gtalk.war(from http://j2s.svn.sourceforge.net/viewvc/*checkout*/j2s/trunk/demos/org.java2script.demo.gtalk/gtalk.war) in my jboss.. while running the application, the gtalk gadget is loading but when i give the username and password and click “sign in”, an error message saying “Trying to connect server failed” arises.

    Can u please help me to figure it out?

  2. Pingback: Hacking so Existing » Blog Archive » Implementing Gmail-alike Web Mail Client by Java2Script

Leave a Reply

Your email address will not be published. Required fields are marked *