Tutorials‎ > ‎

J2ME Tutorial (Part I)

posted Dec 27, 2011, 12:36 AM by Reza Aggi Prasetyo   [ updated Aug 15, 2016, 11:31 PM by Surya Wang ]

Introduction of J2ME

J2ME (Java 2 Platform Micro Edition) is a part of Java platform that is a collection of Java API. It is used to develop small, trim, and memory-efficient software. J2ME is very popular for developing game and mobile applications on mobile devices.On the stage of development of J2ME, it has an emulator that can be used to try the application on a developer’s PC, so that must be easier for developers to do coding, testing, and debugging on the emulator that installed on PC.

                J2ME is divided into two parts, the first part is a platform that are used to develop application on small mobile device standard (usually using Connected Limited Device Configuration/CLDC), and the second part is a platform that are used to develop application on smart phone mobile device standard (usually using Connected Device Profile/CDC).

 

Introduction of CLDC, MIDP, and MIDlet

                CLDC is a collection of Java libraries that intended to develop application on small mobile device standard. CLDC is specially designed so that applications that made using this library could run in a limited-memory condition, small power supply, or minimum graphic capability. Configuration of CLDC consists of basic library sets and basic features of virtual machine that implemented in J2ME environment.

                Mobile Information Device Profile (MIDP) is a collection of Graphical User Interface (GUI) API libraries. In MIDP, there is also API for application development requirement which based on 2D like mobile game.

                MIDlet is a mobile application that written using Java by software developer.MIDlet can be written once and could run on every mobile device that supports Java Mobile specification.

 

J2ME Software Tool

                Software Development Kit (SDK) that used to make a J2ME application:

-          JDK 5.0 (http://java.scun.com/javase/downloads)

-          Java Wireless Toolkit 2.5 for CLDC (http://java.sun.com/products/sjwtoolkit)

 

Editor applications that used to make a J2ME application:

-          Textpad 5.x (http://www.textpad.com/download)

-          Netbeans 6.1 (http://download.netbeans.org/netbeans/6.1/final)

 

It is recommended that you download complete version of Netbeans.

 

SDK and Editor Installation

1.       JDK 5.0

After you downloaded Java Development Kit (JDK) 5.0, you will receive jdk-1_5_0_xxx.exe file that used to JDK 5.0 installation.

1. Run this installer by double clicking it.

2. Press Next Button to install JDK.

3. Wait until installation progress is completed.

 

We also have to set Environtment Variable in Windows, in order to make it knows where Java Home located.

1. Open Control Panel à System, select Advanced tab and click the Environment Variables. Then edit the variable Path at System Variables and add Java Home directory in it. For example, Java Home directory on my computer is “C:\Program Files\jdk1.5.0_16\bin”.


 

2. After that, we must check whether if compiler and java runtime has been recognized by system. To do that, you must type javac (compiler) and java (runtime) command on command prompt. If command prompt show full command list of the program, then the environment variable setting on system has been right.


 

 

 

2.       WKT 2.5 installation:

1.       Run this program by double clicking it.

2.       Press Next button to install JDK.

3.       If there is a request about JVM location, then point it to Java Home directory.

4.       Wait until installation progress is completed.

 

3.       For Textpad and Netbeans installation are like other software installation.

 

 

Hello World MIDlet

                Here is an example of Java program listing that shows an “hello world” alert on emulator J2ME. This program listing can be typed using Textpad or Netbeans.

 

HelloMIDlet.java

import javax.microedition.midlet.*;

import javax.microedition.lcdui.*;

 

public class HelloMIDlet extends MIDlet implements CommandListener {

    private Alert alert = null;

    private Command exit = null;

    private Display display = null;

 

    public HelloMIDlet()

    {

        alert = new Alert("Hello", "Hello World", null, AlertType.INFO);       

        exit = new Command("EXIT",Command.EXIT,1);

        alert.addCommand(exit);

        alert.setTimeout(Alert.FOREVER);

        alert.setCommandListener(this);

    }

    public void startApp() {

        if(display == null)

        {

            display = Display.getDisplay(this);

            display.setCurrent(alert);

        }  }

    }

 

    public void pauseApp() {

    }

 

    public void destroyApp(boolean unconditional) {

    }

 

    public void commandAction(Command c,Displayable d)

    {

        if(c == exit)

        {

            notifyDestroyed();

            destroyApp(true);

        }

    }

}

public void pauseApp() {

    }

    public void destroyApp(boolean unconditional) {

    }

    public void commandAction(Command c,Displayable d)

    {

        if(c == exit)

        {

            notifyDestroyed();

            destroyApp(true);

        }

    }

}

 

Code Description

 

import javax.microedition.midlet.*;

import javax.microedition.lcdui.*;

 

Both of the packages below are the main packages of making an MIDlet application in J2ME. These packages contain classes that handle user interface of a J2ME application.

 

public class HelloMIDlet extends MIDlet implements CommandListener {

 

To make a J2ME application, the class that we created must be inherited from MIDlet class. Here, we use CommandListener interface, because we will use some buttons to control the application.

 

private Alert alert = null;

private Command exit = null;

private Display display = null;

 

Code above shows three objects that we will use in our application. The variable alert is used to show Hello World text, the variable exit is a button that used to exit from application, and the variable display is the main screen handler of J2ME application.

 

   

public HelloMIDlet()

    {

        alert = new Alert("Hello", "Hello World", null, AlertType.INFO);       

        exit = new Command("EXIT",Command.EXIT,1);

        alert.addCommand(exit);

        alert.setTimeout(Alert.FOREVER);

        alert.setCommandListener(this);

    }

 

The code listing above is the constructor that will initialize objects in our application. An exit button will be added to the alert. Normally, time for the alert to show up is only a few seconds, but we will set the alert timeout to be Alert.FOREVER, so that the alert will always showed on application screen.

 

   

public void startApp() {

        if(display == null)

        {

            display = Display.getDisplay(this);

            display.setCurrent(alert);

        }  }

    }

 

    public void pauseApp() {

    }

 

    public void destroyApp(boolean unconditional) {

    }

 

Those three methods are methods that must be overridden or written in our application code. It is because a MIDlet application usually described by this three cycles. The startApp method will be run when the application starts, the pauseApp method will be run when the applications stops, and method the destroyApp method will be run when the application exits.

 

   

public void commandAction(Command c,Displayable d)

    {

        if(c == exit)

        {

            notifyDestroyed();

            destroyApp(true);

        }

    }

 

The commandAction method is a method that must be overridden because we implemented the CommandListener interface. In this method, we can define actions that must be done by application when a button pressed.

 

Compiling and Package

Here, I will explain how to compile and run the program in two ways. The first way is using Textpad as the editor and WTK 2.5 as the compiler. The second way is using Netbeans.

 

Using Textpad and WTK 2.5

                Run Wireless Toolkit 2.5 and then press New Project button to create a new project. Fill the project name with HelloMIDlet and MIDlet class name with HelloMIDlet, then press the Create Project button. After Setting for Project window shown, press the OK button.

 


 

 

 

Pay attention to the directory that used to save the J2ME project created by the Wireless Toolkit. The directory will be used to save our code.

                After creating project in Wireless Toolkit, run Textpad to write our code. Write the code given before and then save with the name HelloMIDlet.java in our project directory.

 


 

                After that, run your Wireless Toolkit. Press the Build button to compile the code that we created before. After it is success, press the Run button so that the mobile device emulator will be shown like the picture below.

 

                            

 

                To create the .jar package of our program, you must open the menu Project > Package > Create Package. After the .jar package has been created, download it into your mobile device and run the package file using your mobile device.

 


 

Using Netbeans

                Run the Netbeans and open the menu File > New Project to create a new project.

 


 

Then on New Project window, choose Mobility Category and Mobile Application Project. After that, press the Next button.

 


 

On the next New Project window, fill the project name with HelloWorld and uncheck the Create Hello MIDlet checkbox. After that, press the Next button again.

 


 

On the Default Platform Selection window, make it sure that the Device Profile choosen is MIDP-2.0. It is done to avoid incompatibility of our application on the emulator with the real mobile device, because mobile devices that often used nowadays are using Device Profile MIDP-2.0. Then, press the Next button.

 


 

On the last New Project window, press the Finish button.

 


 

On the explorer tree, open the Source Packages and right click on the Default Package. After that, choose New > MIDlet.

 


 

On the New File window, change the MIDlet Name to be HelloWorld, and then press the Finish button.

 


 

Then write this code below in HelloWorld.java file.

 

import javax.microedition.midlet.*;

import javax.microedition.lcdui.*;

 

public class HelloWorld extends MIDlet {

 

    private Alert alert = null;

    private Display display = null;

 

    public HelloWorld()

    {

        alert = new Alert("Hello", "Hello World", null, AlertType.INFO);

        alert.setTimeout(Alert.FOREVER);

    }

 

    public void startApp() {

        if(display == null)

        {

            display = Display.getDisplay(this);

            display.setCurrent(alert);

        }

    }

 

    public void pauseApp() {

    }

 

    public void destroyApp(boolean unconditional) {

    }

}

 

The example of program listing view is like the picture below. After writing the program scripting, press the green arrow button on the toolbar.

 


 

Originally written by Krisna Rengga Buana.

Originally written by Krisna Rengga Buana.