J2ME - HelloWorld Application

Recently I have been experimenting J2ME with my Sony Ericsson w710i. The simplicity of J2ME application is amazing.

Let’s create a J2ME project first. Hope you have already installed the Sun Java Wirless Toolkit for CLDC. Launch the CLDC KToolbar application. Select New Project. Give HelloWorld as Project Name and MIDlet Class name. It will show the Settings screen, go to the Required section and set the MicroEdition-Configuration and MicroEdition-Profile as per your phone. Select OK.

The KToolBar would have generated your application folder structure inside the apps folder of your installation, if it is not there check out in the Documents and Settings folder, under your <login> folder it would have created the j2mewtk folder and stored you application there. You have to put all you sources in the <app_name>\src directory.

Let’s create the main application class file. Create a text file in the name of HelloWorld.java in the src folder. It is really simple, you just need to have a class that extends the MIDlet class, now you have to implement the three basic function startApp, destroyApp, pauseApp. As the name suggests these are called when you Start, Destroy or Pause your application.

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

public class HelloWorld extends MIDlet
{
  private Display display;

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

  protected void destroyApp(boolean unconditional)
  {
    notifyDestroyed();
  }

  protected void pauseApp()
  {
  }
}
The function startApp() is called when you launch your application, the destroyApp() function is called when you close is application, notifyDestroyed() is called to notify the system that the application is closed. The pauseApp() function is called when the application is pause by System due to a interruption like incoming call or sms.

The Display class is the highest object in the application, using display class you can show a Form in the screen.

The basic screen component in J2ME is a Form. A Form can show UI Components, a Form can be shown / hide. Let’s add a Form to the application.

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

public class MyFirstApp extends MIDlet
{
  private Display display;
  private Form mForm;

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

    mForm = new Form("Sample Application");
    mForm.append("Hello World");
    display.setCurrent(mForm);
  }

  protected void destroyApp(boolean unconditional)
  {
    notifyDestroyed();
  }

  protected void pauseApp()
  {
  }
}
That’s it. Now we have added a Form with a title “Sample Application” and text “Hello World”. The form will be displayed when the display.setCurrent(mForm); is called.

Now you can Build the application and Run the application in the simulator. To see your app in the phone select Project->Package->Create Package, it will create the jad and jar file in the <app_name>\bin folder. Download the jar file to your phone via cable or Bluetooth and select the file in the phone. Follow the steps given in your phone to install and run the application.

6 Responses to "J2ME - HelloWorld Application"

Unknown responded on March 24, 2009 at 4:03 AM #

hi Karthik ,

it's great blog for j2me starters ..

but I tried helloworld cldc app.
But my Sony Ericsson(w550i) mobile in not able to run that.

It's just saying "operation failed"
At the time of installation ..

please suggest me

Unknown responded on March 24, 2009 at 4:03 AM #
This comment has been removed by the author.
Karthik responded on March 13, 2010 at 10:49 PM #

Did your app run in the emulator?

Did you try signing your App?

Ryan Cheng responded on March 29, 2010 at 5:57 PM #
This comment has been removed by the author.
Ryan Cheng responded on March 29, 2010 at 6:00 PM #

Hi Karthik,

I had the same problem with a simple hello World j2me application that works in the emulator but I have not tried signing my app (is that necessary?).

Every time I try to transfer the jar or the jad to my sony ericsson W580i via bluetooth it says 'Operation failed'


Would you happen to know the reason why?

Thanks

vvMINOvv responded on June 1, 2010 at 5:38 PM #

i have the same issue as Ragunath on my w300i, and I'v followed instructions to the T.

ps. i also signed as you suggested