Skip to main content

By clicking Submit, you agree to the developerWorks terms of use.

The first time you sign into developerWorks, a profile is created for you. Select information in your developerWorks profile is displayed to the public, but you may edit the information at any time. Your first name, last name (unless you choose to hide them), and display name will accompany the content that you post.

All information submitted is secure.

  • Close [x]

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerworks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

By clicking Submit, you agree to the developerWorks terms of use.

All information submitted is secure.

  • Close [x]

Think small with J2ME

Java platform offers opportunities on small, networked devices

Soma Ghosh (sghosh@entigo.com), Senior Application Developer, Entigo
A graduate in computer science and engineering, Soma Ghosh has developed a wide range of e-commerce and networking Java applications over the past six years. She believes that wireless commerce represents the near future of the industry, and has recently been drawn to wireless initiatives of existing desktop models. She is currently a senior application developer with Entigo, a pioneer in B2B sell- and service-side e-commerce products and solutions. Contact Soma at sghosh@entigo.com.

Summary:  The Java 2 Platform, Micro Edition (J2ME) offers great tools for developers, porting the Java platform's network-centric and platform-agnostic worldview down to memory- and processor-limited devices. Soma Ghosh explains the basics of the J2ME world, showing you the building blocks of the platform and demonstrating a sample application.

Date:  01 Nov 2001
Level:  Introductory
Also available in:   Russian

Activity:  51780 views
Comments:  

Personalized and intelligent information appliances are necessities in our life today. These appliances, which include cell phones, two-way pagers, smart cards, personal organizers, and palmtops, tend to be special-purpose, limited-resource, network-connected devices, and not the general-purpose desktops we have known until now. To specifically address this vast consumer space, the Java 2 Platform, Micro Edition (J2ME) provides a plethora of innovative Java technologies.

Transition of the Java VM: From desktop to microdevice

The microdevices that J2ME targets have 16- or 32-bit processors and a minimum total memory footprint of approximately 128 KB. They conform to a Connected Limited Device Configuration (CLDC) while maintaining the Java tradition of anytime, anywhere code portability, deployment flexibility, safe network delivery, and code stability. The prerequisite for the J2ME CLDC is a stripped-down JVM, called the K Virtual Machine (KVM). The KVM is designed for small-memory, resource-constrained, network-connected devices.

Another J2ME configuration, the Connected Device Configuration (CDC), targets advanced consumer electronic and embedded devices such as smart communicators, advanced "smart" pagers, smart personal digital assistants (PDAs), and interactive digital television set-top boxes. Typically, these devices run a 32-bit microprocessor/controller and have more than 2 MB of total memory for the storage of the virtual machine and libraries. The CDC contains the C Virtual Machine (CVM). In this article, we will focus on CLDC and KVM architectures. For more information on the CDC and CVM, see the Resources section below.

The KVM has been adapted to the features of small-footprint devices in the following ways:

  • VM size and class libraries have been reduced to a standard of 50 to 80 KB of object code
  • Memory utilization has been reduced to a standard of tens of kilobytes
  • Performance is effective on devices with 16- and 32-bit processors
  • Architecture is highly portable, with a small amount of machine- and/or platform-specific code
  • Multithreading and garbage collection are system independent
  • Components of the virtual machine can be configured to suit particular devices, thus increasing flexibility

J2ME architecture and configuration

The J2ME architecture is based on families and categories of devices. A category defines a particular kind of device; cellular telephones, simple pagers, and organizers are separate categories. A family of devices is made up a of a group of categories that have similar requirements for memory and processing power. Together, cellular phones, simple pagers, and simple personal organizers make up a single family of small-footprint devices.

Figure 1 defines the relationship between the families and categories of devices in the context of J2ME.


Figure 1. Families and categories of devices
Families and categories of devices

In order to support the kind of flexibility and customizable deployment demanded by the family of resource-constrained devices, the J2ME architecture is designed to be modular and scalable. This modularity and scalability is defined by J2ME technology in a complete application runtime model, with four layers of software built upon the host operating system of the device.

Figure 2 shows the J2ME architecture.


Figure 2. The J2ME architecture
The J2ME archtiecture
  • Java Virtual Machine layer: This layer is an implementation of a Java Virtual Machine that is customized for a particular device's host operating system and supports a particular J2ME configuration.
  • Configuration layer: The configuration layer defines the minimum set of Java Virtual Machine features and Java class libraries available on a particular category of devices. In a way, a configuration defines the commonality of the Java platform features and libraries that developers can assume to be available on all devices belonging to a particular category. This layer is less visible to users, but is very important to profile implementers.
  • Profile layer: The profile layer defines the minimum set of application programming interfaces (APIs) available on a particular family of devices. Profiles are implemented upon a particular configuration. Applications are written for a particular profile and are thus portable to any device that supports that profile. A device can support multiple profiles. This is the layer that is most visible to users and application providers.
  • MIDP layer: The Mobile Information Device Profile (MIDP) is a set of Java APIs that addresses issues such as user interface, persistence storage, and networking.

The Java Virtual Machine layer, configuration layer, and profile layer together constitute the Connected Limited Device Configuration (CLDC). The MID Profile and CLDC provide a standard runtime environment that allows new applications and services to be dynamically deployed on end-user devices.


Programming J2ME with MIDP APIs: The building blocks

The combination of CLDC and MIDP provides a complete environment for creating applications on cell phones and simple two-way pagers.

The core of a MID Profile is a MIDlet application. The application extends the MIDlet class to allow the application management software to control the MIDlet, retrieve properties from the application descriptor, and notify and request state changes.

All MIDlets extend the MIDlet class -- the interface between the runtime environment (the application manager) and the MIDlet application code. The MIDlet class provides APIs for invoking, pausing, restarting, and terminating the MIDlet application.

The application management software can manage the activities of multiple MIDlets within a runtime environment. In addition, the MIDlet can initiate some state changes by itself, and notify the application management software of those changes.

The whole set of MIDP API classes can be broken down into two categories:

  • MIDP APIs for the user interface: These APIs are designed so that interaction with the user is based around a succession of screens, each of which presents a reasonable amount of data to the user. Commands are presented to the user on a per-screen basis. The APIs allow the application to determine what screen to display next, what computation to perform, and what request to make of a network service.
  • MIDP APIs for handling the database: These APIs organize and manipulate the devices database, which comprises information that remains persistent across multiple invocations of the MIDlet.

The underlying CLDC API is used to handle strings, objects, and integers. A subset of the Java 2 API is also provided to handle I/O and network communications.

Figure 3 shows the building blocks of J2ME.


Figure 3. The building blocks of J2ME
The building blocks of J2ME

The relationship between the standard and micro edition Java APIs is shown in Figure 4.


Figure 4. The relationship between the J2ME and J2SE APIs
The relationship between the J2ME and J2SE APIs

Event handling in J2ME

Event handling in J2ME, in contrast to event handling on the desktop version of the Java platform, is based around a succession of screens. Each screen carries a certain small amount of data.

Commands are presented to the user on a per-screen basis. The Command object encapsulates the name and information related to the semantics of an action. It is primarily used for presenting a choice of actions to the user. The resulting command behavior is defined in a CommandListener associated with the screen.

Each Command contains three pieces of information: a label, a type, and a priority. The label is used for the visual representation of the command; the type and priority are used by the system to determine how the Command is mapped onto a concrete user interface.

Figure 5 shows the event handling mechanism in J2ME.


Figure 5. Handling user events in J2ME
Handling user events in J2ME

Designing the user interfaces

Although it maintains a constrained profile, the MIDP API provides a complete set of UI elements. The following are some of the most important ones:

  • An Alert acts as a screen to provide information to the user about an exceptional condition or error.
  • A Choice implements a selection from a predefined number of choices.
  • A ChoiceGroup provides a group of related choices.
  • A Form acts as a container for the other UI elements.
  • A List provides a list of choices.
  • A StringItem acts as a display-only string.
  • A TextBox is a screen that allows the user to enter and edit text.
  • A TextField allows the user to enter and edit text. Multiple TextFields can be placed in a Form.
  • A DateField is an editable component for presenting date and time information. A DateField can be placed in a Form.
  • A Ticker acts as a scrollable display of text.

A complete list of UI elements is available in the MID Profile API documentation that accompanies the J2ME Wireless Toolkit (see Resources below for more information).

Managing the device database

The MIDP provides a set of classes and interfaces to organize and manipulate a device's database: RecordStore, RecordComparator, and RecordFilter. A RecordStore consists of a collection of records, which remain persistent across multiple invocations of the MIDlet. Comparing records in a RecordStore or extracting sets of records from a RecordStore is functionality provided by RecordComparator and RecordFilter interfaces.


Developing J2ME applications

The previous sections have given an overview of J2ME. In this section, we will acquaint ourselves with the practical details of the platform through development of a real-world application on a phone interface.

A sample application: Phone calendar

One of the notable features in J2ME is its date manipulation functionality in a constrained environment. The DateField UI Item offered by J2ME is an editable component for presenting calender information (i.e., date and time). In this section, we will develop a J2ME application that displays a scrolling calendar on a cell phone UI, using DateField and Date functions.


A phone calendar application



// Import of API classes
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.util.*;

//A first MIDlet with simple text and a few commands.
public class PhoneCalendar extends MIDlet
 implements CommandListener, ItemStateListener {

//The commands
private Command exitCommand;

//The display for this MIDlet
private Display display;

// Display items e.g Form and DateField
Form displayForm;
DateField date;

public PhoneCalendar() {
  display = Display.getDisplay(this);
exitCommand = new Command("Exit", Command.SCREEN, 1);
  date = new DateField("Select to date", DateField.DATE);

}

// Start the MIDlet by creating the Form and 
// associating the exit command and listener.
public void startApp() {
displayForm = new Form("Quick Calendar");
  displayForm.append(date);
  displayForm.addCommand(exitCommand);
displayForm.setCommandListener(this);
displayForm.setItemStateListener(this);
display.setCurrent(displayForm);
}

public  void itemStateChanged(Item item)
{
 // Get the values from changed item
}

// Pause is a no-op when there is no  background
// activities or record stores to be closed.
public void pauseApp() { }

// Destroy must cleanup everything not handled 
// by the garbage collector.
public void destroyApp (boolean unconditional) { }

// Respond to commands. Here we are only implementing
// the exit command. In the exit command, cleanup and
// notify that the MIDlet has been destroyed.
public void commandAction (
Command c, Displayable s) {
if (c == exitCommand) {
destroyApp(false);
  notifyDestroyed();
}
}


}

The PhoneCalendar MIDlet as defined above extends an ItemListener and a CommandListener. It gives the MIDlet the capability of tracking an item change on the screen and responding to user commands. The user interface initiative by this application begins by defining a display for the phone screen and attaching a Form to it. The Form acts as a container and can hold a number of user interface items. The commandAction() function acts a command handler in J2ME and defines the actions to be taken for a certain command.


Deploying J2ME

You can download an emulator from Sun that allows you to test J2ME applications on your desktop system. If you'd rather shun all that graphical overhead, you can also deploy J2ME on your command line.

Deploying in an emulated environment

Deploying and running a J2ME application in an emulated environment involves the installation and configuration of an emulator. The J2ME Wireless Toolkit provides an emulated environment for development and deployment of Java applications on top of resource-constrained devices. Here's how to get yourself up and running:

  1. Install the J2ME Wireless Toolkit (see Resources). The installer program will guide you with necessary instructions. Choose a standalone mode for running these examples. Choose an integrated mode in case you want to integrate it with an IDE.
  2. Create a new project through the user interface of KToolbar. Indicate a class name.
  3. Place the class name from Step 2 in the C:\[J2ME Installation directory]\apps\[Project Name]\src directory.
  4. Build the project.
  5. Choose DefaultGrayPhone as a default device from J2ME Wireless Toolkit -> Default Device Selection.
  6. Run the project.

The toolkit also provides an option to package the project into a jar file and a jad file. A double-click on the jad file will deploy the application indicated by the jar file.

Deploying on the command line

A number of command-line options are also available.

1. Create the classfile:

C:\J2ME\apps\PhoneCalendar>
 javac _ tmpclasses _ootclasspath 
C:\J2ME\lib\midpapi.zip -classpath tmpclasses;
classes src\*.java

2. Create a manifest file, manifest.mf:

MIDlet-1: PhoneCalendar,
PhoneCalendar.png,
PhoneCalendar
MIDlet-Name: Phone Calendar
MIDlet-Vendor: Sun Microsystems
MIDlet-Version: 1.0
MicroEdition-Configuration: CLDC-1.0
MicroEdition-Profile: MIDP-1.0

3. Create a jar file:

C:\J2ME\apps\PhoneCalendar>jar cfm .\bin\
PhoneCalendar.jar 
manifest.mf  -C classes . _ res .

4. Create a jad file:

MIDlet-1: PhoneCalendar, 
PhoneCalendar.png, 
PhoneCalendar
MIDlet-Jar-Size: 4490
MIDlet-Jar-URL:
F:\J2ME\apps\PhoneCalendar\bin\
PhoneCalendar.jar
MIDlet-Name: PhoneCalendar
MIDlet-Vendor: Sun Microsystems
MIDlet-Version: 1.0

5. Run the jad file:


C:\J2ME\bin> emulator -Xdescriptor:
C:\J2ME\apps\PhoneCalendar
\bin\PhoneCalendar.jad


The bottom line

J2ME is a significant wireless move from the portable, network-centric Java Virtual Machine. The flexibility in development and deployment of J2ME applications will efficiently cater to the increasing requirements of the wireless world. Stay connected!


Resources

About the author

A graduate in computer science and engineering, Soma Ghosh has developed a wide range of e-commerce and networking Java applications over the past six years. She believes that wireless commerce represents the near future of the industry, and has recently been drawn to wireless initiatives of existing desktop models. She is currently a senior application developer with Entigo, a pioneer in B2B sell- and service-side e-commerce products and solutions. Contact Soma at sghosh@entigo.com.

Report abuse help

Report abuse

Thank you. This entry has been flagged for moderator attention.


Report abuse help

Report abuse

Report abuse submission failed. Please try again later.


developerWorks: Sign in


Need an IBM ID?
Forgot your IBM ID?


Forgot your password?
Change your password

By clicking Submit, you agree to the developerWorks terms of use.

 


The first time you sign into developerWorks, a profile is created for you. Select information in your developerWorks profile is displayed to the public, but you may edit the information at any time. Your first name, last name (unless you choose to hide them), and display name will accompany the content that you post.

Choose your display name

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerWorks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

(Must be between 3 – 31 characters.)

By clicking Submit, you agree to the developerWorks terms of use.

 


Rate this article

Comments

Help: Update or add to My dW interests

What's this?

This little timesaver lets you update your My developerWorks profile with just one click! The general subject of this content (AIX and UNIX, Information Management, Lotus, Rational, Tivoli, WebSphere, Java, Linux, Open source, SOA and Web services, Web development, or XML) will be added to the interests section of your profile, if it's not there already. You only need to be logged in to My developerWorks.

And what's the point of adding your interests to your profile? That's how you find other users with the same interests as yours, and see what they're reading and contributing to the community. Your interests also help us recommend relevant developerWorks content to you.

View your My developerWorks profile

Return from help

Help: Remove from My dW interests

What's this?

Removing this interest does not alter your profile, but rather removes this piece of content from a list of all content for which you've indicated interest. In a future enhancement to My developerWorks, you'll be able to see a record of that content.

View your My developerWorks profile

Return from help

static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=Java technology
ArticleID=10602
ArticleTitle=Think small with J2ME
publish-date=11012001
author1-email=sghosh@entigo.com
author1-email-cc=

Tags

Help
Use the search field to find all types of content in My developerWorks with that tag.

Use the slider bar to see more or fewer tags.

For articles in technology zones (such as Java technology, Linux, Open source, XML), Popular tags shows the top tags for all technology zones. For articles in product zones (such as Info Mgmt, Rational, WebSphere), Popular tags shows the top tags for just that product zone.

For articles in technology zones (such as Java technology, Linux, Open source, XML), My tags shows your tags for all technology zones. For articles in product zones (such as Info Mgmt, Rational, WebSphere), My tags shows your tags for just that product zone.

Use the search field to find all types of content in My developerWorks with that tag. Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere). My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).

Special offers