Compiling and building your first MIDP Application

Back | Next

This article deals with compile and build process of MIDP applications. The tools we will use are popular IDE Eclipse and J2ME WTK. However there are number of other IDE's like NetBeans, Web Sphere Device Developer(WSDD) etc which you can use. The HelloWorld program in the previous chapter will be used in this article.

You will need to install following software before starting up

Before getting hands on into the build process lets look at various steps involved in the build process.

Obfuscation can also be done between these steps. Obfuscation is a technique used not only to protect and make your source code unreadable but also comes with added advantage of smaller JAR file size. The most popular open source obfuscators are ProGuard and RetroGuard. Lets get hands on with your first build.

Create a New Project in KToolbar

KToolbar: Create project

Create Coding environment in Eclipse IDE

You can skip this Eclipse section and just copy the HelloWorld.java into location <WTK_HOME>\apps\HelloWorld\src and go ahead with the JAR file creation if coding environment is not your present worry. Skip this section and go to "Running the application with KToolbar".

Eclipse: Create MIDP project

Eclipse: Create MIDP project

package com.j2me.salsa.hello;

//Midlet's User interface API
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;

//Midlet class's package
import javax.microedition.midlet.MIDlet;

public class HelloWorld extends MIDlet
			implements CommandListener {

	private Form helloForm;
	private Command exitCommand;

 /**
  * Midlet constructor is called
  * The objects are instantiated.
  * Midlet enters paused state
  * */

    public HelloWorld() {

    	exitCommand = new Command("Exit", Command.EXIT, 1);
    	helloForm = new Form("Hello World");
    	helloForm.append(new String("Hello World"));
    	helloForm.addCommand(exitCommand);
    	helloForm.setCommandListener(this);

    }

    /**
     * startApp() method is called to
     * change the paused state to active state.
     */

    protected void startApp() {

        Display.getDisplay(this).setCurrent(helloForm);
    }

    /**
     * The MIDlet frees as much resources as it can.
     */

    protected void pauseApp() {}

    /**
     * Midlet cleans up resources before the application exits.
     *
     */

    protected void destroyApp(boolean bool) {}

    /**
     * Midlet event handling
     */
    public void commandAction(Command cmd, Displayable disp) {
        if (cmd == exitCommand) {
            destroyApp(false);
	    // Midlet notifies the AMS that it has done its work.
            notifyDestroyed();
        }
    }
}

Running the application with KToolbar

To Create a JAR File with KToolbar and deploy it on to mobile device.

Let us investigate the package structure at location <WTK_HOME>\apps\HelloWorld.

If you investigate JAR file it has the mobile.salsa.HelloWorld.class and Manifest file. Your JAD file looks like the listing given below

HelloWorld.jad

MIDlet-1: HelloWorld, HelloWorld.png, com.jme.hello.HelloWorld
MIDlet-Jar-Size: 1433
MIDlet-Jar-URL: HelloWorld.jar
MIDlet-Name: HelloWorld
MIDlet-Vendor: javamicroedition.com
MIDlet-Version: 1.0
MicroEdition-Configuration: CLDC-1.0
MicroEdition-Profile: MIDP-2.0

Following parameters are important in your JAD file

MIDlet-1: This parameter has reference to the entry point of your application. This should be the same class which extends MIDlet class.

MIDlet-Jar-Size: This is the size of the JAR file and should be exactly equal to your JAR file size. JAD file is always first loaded to a device and this JAR Size is referred to read the number of bytes from the JAR file. So if there is any difference the application will not work properly.

If every thing is done properly following screen will appear on your emulator.

Hello World on emulator

Finally you have just run your first J2ME application. Welcome to J2ME community, the salsa begins!!!!!


j2meSalsa goodies
Execute Hello World Sample online Select "Hello World" on the online emulator on your left frame.
Download demo code for deploying directly to WTK Click here to download Zip File Containing WTK compatible file structure.

Back | Next

site comments powered by Disqus

This page is a part of a frames based web site. If you have landed on this page from a search engine click here to view the complete page.