Getting started with J2ME

J2ME is a software used to develop applications for Java enabled devices like Mobile Phones and PDAs. For more information about J2ME refer J2ME in Wikipedia. This article explains how to write a simple hello world program in J2ME.

Installing J2ME SDK

Writing a Hello World Program

  • Open SDK and select option File.

  • Select option New Project. It will open a new project window.

  • Select MIDP Application and then click Next.

  • Select your Project location and enter the new project Name, For Ex. HelloMidlet

  • Then Click on Finish.

  • This will automatically import a HelloMidlet program.

/static/images/choose-project.png
Figure 1. Choose project

Given below is the common structure of MIDlet program.

import javax.microedition.midlet.*;

public class HelloWorld extends MIDlet {
    public void startApp() {

    }

    public void pauseApp() {

    }

    public void destroyApp(boolean unconditional) {

    }
}

HelloWorld is a class that extends the MIDlet class and is the interface between MIDlet application and the run-time environment, which is controlled by the application manager present in the mobile phone. A MIDlet class must contain three methods that are called by the application manager to manage the life cycle of the MIDlet. These abstract methods are startApp(), pauseApp(), and destroyApp().

The startApp() method is called by the application manager when the MIDlet is started and contains statements that are executed each time the application begins execution.

The pauseApp() method is called before the application manager temporarily stops the MIDlet. The application manager restarts the MIDlet by recalling the startApp() method.

The destroyApp() method is called prior to the termination of the MIDlet by the application manager.

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

public class HelloWorld extends MIDlet implements CommandListener{
        private Display display;
        private TextBox textBox;
        private Command quitCommand;

        public void startApp() {
                display = Display.getDisplay(this);
                quitCommand = new Command("Quit", Command.SCREEN, 1);
                textBox = new TextBox("Hello World", "My first MIDlet", 40, 0);
                textBox.addCommand(quitCommand);
                textBox.setCommandListener(this);
                display.setCurrent(textBox);
        }

        public void pauseApp() {

        }

        public void destroyApp(boolean unconditional) {

        }

        public void commandAction(Command choice, Displayable displayable) {
                if (choice == quitCommand) {
                        destroyApp(false);
                        notifyDestroyed();
                }
        }
}

Running the MIDlet using the Emulator

  • Create a new project and type the above code.

  • Run the code by pressing F6 key in the keyboard.

  • The result will be displayed on the emulator screen.

  • Now your MIDlet application is ready for installing on the Mobile phone.

/static/images/output.png
Figure 2. Emulator output

Installing the MIDlet on a Java Enabled Mobile Phone

  • Go to your Project Folder. You will find a folder dist. This folder has two files named Hello.JAD and Hello.jar files.

  • For example we shall consider Sony Erricson F305 handset.

  • Copy these two files in the memory card.

  • In your mobile phone goto file manager and the exact location of the files you copied.

  • Click the Hello.jar and install it.

  • Run the application. You will seen the Hello World printed in your Mobile Screen.