Posted by Fadi Hania on 16th March 2011

J2ME: Hello World Mobile Application

Mobile technology is a day-to-day technology that almost everyone has. Some even use this technology more than PC and they are always in need for new features and functionality. These features and functionality are provided using Mobile Applications, so I decided to write a series of articles about building mobile applications. At the beginning, I will start with J2ME applications and later I will cover other technologies.

J2ME applications are simply Java applications with a limited functionality that is designed to run on mobile devices. The standard Java runtime environment for these devices is provided using Mobile Information Device Profile (MIDP) combined with the Connected Limited Device Configuration (CLDC). MIDP and CLDC simply provide the core application functionality required by mobile applications. To write J2ME applications you need the Java Platform Micro Edition SDK and an IDE, which can be Netbeans or Eclipse.

Creating MIDlet

All J2ME application must have a class derived from a special class called MIDlet. It is like the entry point for the application and manages the life cycle of the application. The MIDlet can be in one of three states: active, paused and destroyed. MIDlet is put into a paused state when the application manager calls pauseApp() method, and when it calls startApp() method the MIDlet is put into the active state. The destroyed state is entered when the MIDlet destroyApp() method is called or when the MIDlet itself calls notifyDestroyed() method.

The MIDlet class is located in the javax.microedition.midlet package, so the first thing to do is to import this package in addition to the javax.microedition.lcdui package which contains the J2ME UI components.

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

The next step is to create a new class derived from MIDlet class, and to implement the three abstract methods: startApp(), pauseApp() and destroyApp().

public class HelloMIDlet extends MIDlet {
    public HelloMIDlet() {
    }

    protected void startApp() {
    }

    protected void pauseApp() {
    }

    protected void destroyApp(boolean unconditional) {
    }
}

The Display and Displayable

The J2ME application can run in background or interact with user. Interactive applications need access to mobile display by obtaining an instance of Display class. The Display object is used to display the required user interface which should be derived from the Displayable class, in this article an instance of the Form class will be used as the user interface. The Form is a derived class of the Displayable class, which has a title and you can add contents to it using other UI components.

So the next step is to define two data members of the Display and Form classes.

public class HelloMIDlet extends MIDlet {
    private Display display;
    private Form helloFrm;
    ...
}

In the constructor of MIDlet class create an instance of Form class by calling the constructor and passing the form title to it.

public HelloMIDlet() {
    helloFrm = new Form("Hello World");
}

Display The Form

The next step is to obtain an instance of the Display class and display the form. To get an instance of the Display class you call the Display class static method getDisplay() and passing a reference to the MIDlet object to it. Next use the Display object setCurrent() method to display the Form.

protected void startApp() {
    display = Display.getDisplay(this);
    display.setCurrent(helloFrm);
}

Conclusion

This article describes how to create a J2ME Hello World mobile application. The first step which is required for all J2ME applications is to create the MIDlet class, which is the main class. Then you can use an instance of the Form class as the main UI in the application, which should be displayed using an object of the Display class.

Following is the complete application code, the downloadable Java source file and the output files as Jad/Jar.

Do not forget to share it and if you have any questions, please leave a comment.

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

public class HelloMIDlet extends MIDlet {
    private Display display;
    private Form helloFrm;

    public HelloMIDlet() {
        helloFrm = new Form("Hello World");
    }

    protected void startApp() {
        display = Display.getDisplay(this);
        display.setCurrent(helloFrm);
    }

    protected void pauseApp() {
    }

    protected void destroyApp(boolean unconditional) {
    }
}
  • Regis Latawiec

    Hello,

    This is nice to have recent article about J2ME CLDC. :-)
    For your information, you can use J2ME CLDC to write rich applications for any kind of embedded systems, not only mobiles. Which is a great new opportunity for embedded software designers.

    • Thanks Regis,
      That’s right but this article and future articles focus on J2ME CLDC applications for Mobile Devices as I mentioned in the article.

  • Thanks Regis,
    That’s right but this article and future articles focus on J2ME CLDC applications for Mobile Devices as I mentioned in the article.

  • Pingback: Fadi Hania Blog - J2ME: Using StringItems and Commands()

  • Aziz

    Hello..

    nice articles here..thanks for sharing your knowledge

  • Pingback: Fadi Hania Blog - J2ME: Using TextFields and Alerts()

  • Anonymous

    Thanks Fadi for this article.
    I was just thinking, about the scope of J2ME applications in the present times.I have been working on J2me since 4 years and find that its relevance is decreasing day by day. J2ME was designed to be an industry standard to support on most devices. But currently each vendor has come up with its own application platform which are far much superior than J2ME. Android and Iphone do not support J2me, this reduces the market share of J2me devices to the symbian and Windows and Blackberry mobile platforms which are also struggling to keep pace with the  latest trends.
     Thus J2ME as a platform has to strongly evolve as a standard, and a way has to be devised so that it becomes mandatory for a vendor/manufacturer to support it.
    What are your views ?

  • Location-Based Marketing

    I would
    like to thank you for the efforts you have made in writing this article. I am
    hoping the same best work from you in the future as well. I just can’t stop
    reading this. It’s so cool, so full of information that I just didn’t know. I’m
    glad to see that people are actually writing about this issue in such a smart
    way, showing us all different sides to it. You’re a great blogger. Please keep
    it up.
    Location-Based Marketing