Java 2 Micro Edition: Java For Small and Embedded Systems
By Mark J. Balbes, Ph.D., OCI Senior Software Engineer
January 2001
Introduction
For several years, Java has been a strong tool for desktop and enterprise application development. But where do you turn when the software you want to develop runs on a computer that fits in the palm of your hand?
The answer is to turn to the new Java 2 Micro Edition (J2ME).
J2ME is a subset of the familiar Java 2 Standard Edition (J2SE) designed specifically for programming small and embedded devices where memory footprint and performance issues due to limited resources are key concerns.
J2ME is complementary to other technologies for embedded and wireless systems, such as the Wireless Application Protocol (WAP).
Whereas WAP allows a user to access the web on a PDA (for example, a Palm or Visor) or a mobile device (such as a cell phone or pager), J2ME lets users run applications on these devices. Thus, the relationship of J2ME to WAP is similar to that between J2SE and HTML.
Configurations and Profiles
Because different devices have different limitations, J2ME defines the operating parameters of different device classes by specifying different configurations.
The Connected, Limited Device Configuration (CLDC) is currently the only defined configuration. It requires a device with 160 to 512 kB of memory, a 16 or 32 bit processor, and network connectivity (possibly wireless). The network can be used to deliver applications as well as data.
Most obviously missing is support for floating-point operations due to the limited processing power of the targeted devices.
CLDC addresses Java language and virtual machine features, core Java libraries, i/o, networking, security, and internationalization. It does not address such issues as application life-cycle management (e.g., how the application gets loaded, initialized, executed, and stopped), GUI functionality, or event handling.
Removed from CLDC is support for Java Native Interface, user-defined class loaders, reflection, thread groups and daemon threads, finalization, and weak references.
For each configuration, profiles address additional specific issues, such as the application life cycle and user interface functionality.
Currently, only one profile is defined, the Mobile Information Device Profile (MIDP), which is used to develop applications for mobile devices, such as cell phones and pagers.
MIDP defines APIs for user interfaces (addressing the small screen size and common buttons found on most cell phones), persistent data storage, networking, and application lifecycle (via the MIDlet class, which serves a similar purpose as Applets).
A future article will discuss mobile device programming using CLDC with MIDP.
Programming the Palm with J2ME
In this article, we will focus on application development for the Palm platform.
Because the virtual machine (VM) for the Palm was developed before J2ME, it does not quite follow the J2ME form. Rather than defining a profile for the Palm, we have the kiloVirtualMachine (kVM).
To get started developing applications for the PalmOS, we will first need to gather our development tools.
We will, of course, download the J2ME CLDC SDK which includes the J2ME kVM classes. Plug-ins for Java IDE make it easy to write, compile and deploy applications.
Now that we've set up our development environment, we're ready to write our first J2ME application.
To create an application for the Palm, we need to create a spotlet, which is somewhat analogous to an applet in the browser.
The spotlet is a container for other components. It also receives all events and handles them either directly or by delegating to one of its contained components. This event handling mechanism is not as elegant as the one in J2SE and is reminiscent of the original JDK 1.0 event model.
Although an application can consist of many spotlets, only one can be registered at a time. When a spotlet is registered, it is displayed on the screen and receives all events from the keys and screen.
Here is a simple spotlet class that prints the phrase "Hello Spotlet" to the screen.
- public class HelloSpotlet extends Spotlet {
- private Graphics g;
- private Button quitButton;
- private String message = "Hello Spotlet";
-
- public HelloSpotlet() {
- g = Graphics.getGraphics();
- g.clearScreen();
- quitButton = new Button("Quit", 5, 125);
- quitButton.paint();
- g.drawString(message, 5, 5, Graphics.PLAIN);
- }
-
- public void penDown(int x, int y) {
- if (quitButton.pressed(x, y)) {
- System.exit(0);
- }
- }
-
- public static void main(String[] args) {
- new HelloSpotlet().register(Spotlet.NO_EVENT_OPTIONS);
- }
- }
Note that we must override the penDown()
method in order to process events for the button.
Also, we have specified the position and label for the quitButton
, but not its size. The GUI components in the kjava package of the kVM are very limited. The size of the button is determined by the label being displayed.
In order to gain more control over the UI and to use more familiar components, a lightweight clean-room implementation of the J2SE AWT components is available from the kAWT Project. By using kAWT, you can run the same application on a desktop and on the Palm. However, the footprint of kAWT will add significantly to the memory footprint of your application.
Once we have compiled HelloSpotlet
, we can deploy it using the Deployment Wizard in JBuilder Handheld Express, or from the command line with java palm.database.MakePalmApp HelloSpotlet
. This will create a .prc file.
To run HelloSpotlet
, we simply install this .prc file into POSE or to an actual Palm device via the hot sync operation.
To build a more sophisticated application, CLDC provides networking capabilities and access to the infra-red port of the Palm.
To sync data between a desktop and the Palm, a conduit must be built.
Final Thoughts
J2ME provides a mechanism for Java developers to easily transfer their desktop application development skills to Palm and mobile-device platforms.
Through the use of configurations and profiles, applications can target different classes of devices with different capabilities and restrictions. Although necessary, this turns the promise of "write once, run anywhere" into "write once, run on any device with the same configuration and profile."
Currently, only one configuration (CLDC) and one profile (MIDP) exist. It remains to be seen how many different classes of devices will need to be defined and how easily an application written for one profile can be deployed to a different profile.
In this article, we focused on J2ME development on the Palm platform. In a future article we will discuss the J2ME Wireless Toolkit, which is used to develop applications for mobile devices that conform to the MIDP profile.
References
- [1] CLDC Specification
http://java.sun.com/aboutJava/communityprocess/final/jsr030/index.html - [2] MIDP Specification
http://java.sun.com/aboutJava/communityprocess/final/jsr037/index.html - [3] J2ME Archive
http://www.billday.com/j2me/index.html - [4] KVM World
http://www.kvmworld.com/ - [5] Palm Emulator (POSE)
http://www.palmos.com/dev/tech/tools/emulator/ - [6] kAWT Project
http://www.trantor.de/kawt/ - [7] J2ME Wireless Toolkit
http://java.sun.com/products/j2mewtoolkit/ - [8] Conduit Development Kit - Windows Java Edition
http://www.palmos.com/dev/tech/tools/cdk/java
Software Engineering Tech Trends (SETT) is a regular publication featuring emerging trends in software engineering.