J2ME tutorial (Getting started with J2ME)
Basic tools you need to install before startingSee the following table for free download and configuration.
| name of tool | recommended version | download link | notes/configuration |
| Java Development Kit(Standard Edition) | JDK 6 Update 12 or later | Download link. | Needed by the following tool. When downloading, select your platform, then click on the file name. The installation directory of the SDK is C:\Program Files\Java\jdk1.6.0_12, and that of the JRE that is part of the installation is C:\Program Files\Java\jre1.6.0_12. Accept these defaults. |
| Sun Java Wireless Toolkit for CLDC | 2.5.2_01 | Download link | In the "Java (TM) Virtual Machine Location" phase of the installation, the latest JDK on your machine is automatically detected. If that is later than JDK 6 Update 12, then browse to the directory C:\Program Files\Java\jdk1.6.0_12. The default installation directory of the tool is C:\WTK2.5.2. |
| Nokia SymbianOS/S60 SDK for Java | S60 5th Edition SDK, N97 SDK is good, all in one C++/Java | Download link | Unzip Nokia_N97_SDK_v1_0_en.zip, into a directory, and run the unzipped setup.exe installer program. The default installation directory is C:\S60\devices. Some features require Pearl installation. |
| Eclipse IDE | 3.2.2 | Download link | Unzip the downloaded file, eclipse-SDK-3.2.2-win32.zip, into C:\Program Files\eclipse |
| EclipseME plugin for Eclipse | 1.7.9 | Download link | EclipseME version that runs on top of Eclipse v3.2. Save the downloaded file, eclipseme.feature_1.7.9_site.zip, somewhere. Instructions for integrating the plugin with Eclipse are given below |
Integrating EclipseME with Eclipse
Select the menu item '''Help | Software Updates | Find and Install'''.
Select the radio button: '''Search for new features to install''' and press '''Next'''.
Press the button '''New Archived Site'''. In the file selection dialog that appears, select the EclipseME installation file you downloaded, eclipseme.feature_1.7.7_site.zip.
Back in the main dialog, make sure the eclipseme.feature_1.7.7_site.zip checkbox is the only one selected, and press '''Finish'''.
Select the eclipseme.feature_1.7.7_site.zip checkbox and press '''Next'''.
Select the '''I accept the terms in the license agreement''' radio button, and press '''Next'''.
Make sure '''installation directory''' is the directory you unzipped Eclipse into (C:\Program Files\eclipse, or wherever you installed it), and press '''Finish'''.
A JAR verification warning message is given. Don't worry, be happy. Press '''Install'''.
Press '''Yes''' to restart Eclipse. Select the workspace again.
Configuring EclipseME
Select the menu item '''Window | Preferences'''.
Expand node '''J2ME''' and select '''Device Management'''.
In the settings section of the dialog, press '''Import'''.
In the new dialog, browse for the directory C:\S60\devices\Nokia_N97_SDK_v1.0. Click '''Refresh'''.
Make sure the S60Emulator and S60Device devices' checkboxes are both checked. Press '''Finish'''.
Press '''Import''' again.
In the new dialog, browse for the directory C:\WTK2.5.2 (or wherever you installed the Wireless Toolkit). Click '''Refresh'''.
Make sure all the four devices' checkboxes are checked. Press '''Finish'''.
Back in the main dialog, select DefaultColorPhone as the default device. (Selecting the S60Emulator choice will cause running a MIDlet to fail.)
Back in the main dialog, press '''OK'''.
Configuring Eclipse's Java options
In Eclipse, select the menu item '''Window | Preferences'''.
Expand node '''Java | Build Path'''.
For item '''Source and output folder''', select the radio button '''Folders''', and keep the defaults (src and bin).
Expand node '''Java | Debug'''.
Uncheck '''Suspend execution on uncaught exceptions'''.
Uncheck '''Suspend execution on compilation errors'''.
Set '''Debugger timeout (ms)''' to 15000.
Press '''OK'''.
Once your tool has been installed properly go ahead with the following sites to create your J2ME application.
Follow the following instruction to run your first J2ME midlet in emulator.
Step 1: Select File | New | Project
Step 2: Expand J2ME and select J2ME Midlet Suite and press next
Step 3: Give a project name for example hellomymidlet press next
Step 4: Give attention to Device group in the UI, check that Group is Sun Java wireless Toolkit and device is Default color phone, press on next and then Finish.
Step 5: Download the following file mymidlet.java and save it in your computer, also copy the file from your folder.
Step 6: Now go to the eclipse and expand your project and paste the copied file to src folder by right clicking (by Ctrl V).
Step 7: Build the project and run it, See your greetings on the emulator window.
Step 8: If you want to send this to phone, select the project right click | J2ME | Create Package, this will create a deployed folder and where you see the jar file that you can send to phone and install it.
If you want to import all the implementation file from existing project you just copy the respective folder and paste it to respective folder in your project just after Step 4.
0. MIDP 2.0
The MIDP (the Mobile Information Device Profile) is designed for mobile phones and PDAs. And it is designed to operate on top of the Connected, Limited Device Configuration (CLDC).

1. Requirements of MIDP 2.0
MIDP device has the following minimum characteristics:
- Display:
- screen size 96x54
- Input:
- One or more of the following user-input mechanisms: one-handed keyboard, two-handed keyboard, or touch screen
- Memory:
- 256 kilobytes of non-volatile memory for the MIDP implementation, beyond whats required for CLDC
- 8 kilobytes of non-volatile memory for application-created persistent data
- 128 kilobytes of volatile memory for the Java runtime (e.g., the Java heap)
- Networking:
- Two-way, wireless, possibly intermittent, with limited bandwidth
- Sound:
- The ability to play tones, either via dedicated hardware, or via software algorithm.
2. Midlet
MIDP application is a MIDlet. The activity of a MIDlet is managed by the Application Management Software of the mobile device. AMS control the life cycle of a midlet. AMS creates, starts, pauses and destroys the instance of MIDlet. AMS controls the activity of a MIDlet by means of startApp(), pauseApp() and destroyApp() methods.

Our MIDP application, MIDlet, must extend the abstract MIDlet class, which consists of the abstract methods startApp(), pauseApp() and destroyApp(). We have to write these methods also into our MIDlet.

MIDlet States
AMS creates the MIDlet object by allocating memory for it and calling its no-argument constructor. The application can do only little initialization in no-argument constructor. Middlet instance is first in the paused state. If an exception occurs during create phase of the MIDlet object, the application enters immediately the destroyed state. After the exceptionless return from constructor AMS calls startApp() method of the MIDlet. The application enters just before startApp() method call active state. Application has most of the initialization tasks in the startApp() method.

Example.
The following simple MyMIDlet has a TextBox user interface component with some text and an exit command. MyMIDlet is derived from the MIDlet class and it has the no-argument constructor, startApp(), pauseApp() and destroyApp() methods.
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class MyMIDlet extends MIDlet implements CommandListener
{
private Command exitCommand;
private Display display;
public MyMIDlet()
{
display = Display.getDisplay(this);
exitCommand = new Command("Exit", Command.SCREEN, 2);
}
public void startApp()
{
TextBox t = new TextBox("The first midlet", "Welcome to the wonderfull world of midlets", 50, 0);
t.addCommand(exitCommand);
t.setCommandListener(this);
display.setCurrent(t);
}
public void pauseApp(){}
public void destroyApp(boolean b){}
public void commandAction(Command c, Displayable s)
{
if(c == exitCommand)
{
destroyApp(false);
notifyDestroyed();
}
}
}
3. User interface
The user interface for MIDP applications consists of the UI API in the javax.microedition.lcdui package.
Display class represents the manager of the display and input devices of the system. There is exactly one instance of Display per MIDlet and the application can get a reference to that instance by calling the Display.getDisplay() method. Method call is in the constructor or in the startApp() method.
Display instance has no graphical representation form, it only manages the display and input device of the mobile device. Display instance has setCurrent(Displayable displayable) method, which makes Displayable object visible on the display. Displayable class is the super class of all GUI component classes, that has the capability of being placed on the display and that can contain user command activities. The following figure contains the most important classes of the MIDP graphical user interface.

Screen and Canvas classes are extended from Displayable class. Screen class is common superclass of all high-level user interface classes. Canvas is a base class for applications, that need to handle low-level events and to issue graphics for drawing to the display. Java games use Canvas class and its subclasses.
One instance of the four subclasses of the Screen class, TextBox, List, Alert or Form, can be made visible on the display of a mobile device.
Form class represents a form which can contain other user interface controls. The controls added to a Form instance must be derived from Item class. The following classes of MIDP are derived from Item: ChoiceGroup, TextField, DateField, ImageItem, StringItem, Gauge, CustomItem and Spacer.
User interface classes extended from Displayable class derive addCommand() method which enables them to add user interface actions and commands to themselves. All MIDP user interface actions are presented by means of Command classes.
Command class
The Command class is a construct that encapsulates the semantic information of an user interface action. Command only contains information about the command activity, it doesn't contain the program code of the activity. Program code is written in a CommandListener object associated with the Displayable instance.
Commands can be presented to the user of a device in different ways depending on the model of the device, on the type of the command, on the total number of commands, etc.. The presentation way of a action can be a soft key, item in a menu, some other direct user interaction contruct, voice command, ... A Command object is mapped to a GUI component through addCommand() method.
The constructors of the Command class are below:
- Command(String label, int commandType, int priority)
- Command(String shortLabel, String longLabel, int commandType, int priority)
Paramters label, shortLabel and longLabel are a string the application shows to the user which. The string can be direct on the display or a item of a menu. If the string is direct on the display, it presents a soft key.
The second parameter, commandType, specifies the intent of the command. MIDP defines the following command types:
- BACK, return to the previous screen
- CANCEL, standard negative answer to a dialog
- EXIT, exit from application
- HELP, on-line help
- ITEM, hint the implementation of item of the Screen or the elements of a Choice
- OK, standard positive answer to a dialog
- SCREEN, application defined command
- STOP, stop some currently running process, operation, etc.
The commandType definition doesn't do any activity automatically in the application. The activity have to been programmed in the midlet. Command can have a standard presentation way on a display by means of commandType definition. For example, if the application specifies that the command is of type BACK, and if the device has a standard of placing the "back" operation on a certain soft-button, the implementation can follow the style of the device by using the semantic information as a guide.
The last parameter of Command constructor, priority, describes the importance of this command relative to other commands on the same screen. Priority values are integers, where a lower number indicates greater importance. The priority of a command can have meaning, when application decide, if the command is showed in a menu or in a soft key. The in the constructor given value of the priority is only directive, the actual values of priority are chosen by the application.
Example of a definition of a Command instance is below:
new Command("Back", Command.BACK, 1)
CommandListener interface
A program code for user interface action is written in a class that implements CommandListener interface. The CommandListener interface contains only a method, commandAction(). The signature of the method is:
public void commandAction(Command command, Displayable d)
The first parameter, command, identifies the Command instance, which is attached to the user interface command and the second parameter, d, identifies the user interface component, which the activity occured in.
A CommandListener is attached to a user interface component with its setCommandListener() method, whose signature is below:
public void setCommandListener(CommandListener listener)
Parameter listener is a class that implements CommandListener interface.
Often the midlet self implements CommandListener interface and contains commandAction() method, where the program is coded.
