ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button

What's J2ME?

by Daniel A. Tauber
03/08/2001

Java 2 Micro Edition (J2ME) is Sun's version of Java aimed at machines with limited hardware resources such as PDAs, cell phones, and other consumer electronic and embedded devices. J2ME is aimed at machines with as little as 128KB of RAM and with processors a lot less powerful than those used on typical desktop and server machines. J2ME actually consists of a set of profiles. Each profile is defined for a particular type of device -- cell phones, PDAs, microwave ovens, etc. -- and consists of a minimum set of class libraries required for the particular type of device and a specification of a Java virtual machine required to support the device. The virtual machine specified in any profile is not necessarily the same as the virtual machine used in Java 2 Standard Edition (J2SE) and Java 2 Enterprise Edition (J2EE). You'll see that the profile we'll use to develop a Palm OS device application is a subset of the Java Virtual Machine you already know.

To date, Sun has released the following profiles:

  • The Foundation Profile -- A profile for next generation consumer electronic devices
  • The Mobile Information Device Profile (MIDP) -- A profile for mobile information devices, such as cellular phones and two-way pagers, and PDAs

A profile in itself does not do anything; it just defines the specification. Profiles are implemented with a configuration. You can think of a configuration as an implementation of a J2ME profile for a particular type of device such as a PDA. Some of the configurations currently available are

Connected Device Configuration (CDC)

An implementation of the Foundation Profile for next-generation, consumer electronic and embedded devices

Connected Limited Device Configuration (CLDC)

An implementation of MIDP for small, resource-constrained devices such as Palm OS devices.

Since each profile defines a different set of Java class libraries, you cannot take a Java application written for one profile and run it on a machine that supports another profile. Likewise, you cannot take an application written for Java 2 Standard Edition (J2SE) or Java 2 Enterprise Edition (J2EE) and run it on a machine that supports J2ME. You can only use the Java classes provide in the Java class library included in your target device's profile. Restricting yourself from using all of the Java classes you've grown to rely on is one of the hardest parts of writing Java applications for small devices.

The rest of this article focuses on using the MIDP profile and CLDC configuration to create an application for a PDA running the Palm operating system.

Getting J2ME

The J2ME CLDC comes as two files that you can download from the Sun Community site. The first file is j2me_cldc_-1_0-src.winsol.zip and contains all of the class libraries needed to develop CLDC applications on your computer. In addition to containing the class libraries, it also includes both source code and Windows and Unix binaries for a reference implementation of the Java Virtual Machine specified by MIDP. This Java Virtual Machine is referred to as the K Virtual Machine (KVM), and its inclusion saves you the trouble downloading your applications to a handheld device during development. You can test them right on the same desktop machine you use for development.

The second file available from the Sun Community site is j2me_cldc-1_0-src-palm_overlay.zip, which contains an implementation of the KVM for devices running the Palm operating system, along with the tools to turn the .class file generated by a Java compiler into an executable Palm file. Note that this is a particular implementation of the CLDC for the Palm operating system. As other implementations become available, your Java application will be able to run on those other machines as well.

You'll need to install both of these ZIPs on your computer to develop applications for Palm OS devices. For the rest of this article, I assume you have these installed in the CLDC folder on the C: drive.

Note that you do not need a special Java compiler in order to develop CLDC applications. You use the compiler that comes with J2SE. The only difference is that you'll specify an alternative location for the class library during compile. You'll also need to process the resulting .class file with some special software included with the CLDC download.

Running the Sample Apps

The files you downloaded from Sun include both source and compiled versions of many sample applications. You can run these sample applications on your desktop computer using the KVM for Windows or Unix. Running these applications will let you get a feel of what a CLDC application is like. Before you can run these programs, you need to start a command shell and enter the following commands.

set cldc_classpath = c:\cldc\bin\api\classes;.
set bin = %bin%;c:\cldc\bin
cd \cldc\bin\samples\classes

Now that you have set up the environment variables and changed into the directory that holds the sample applications, you can start to run them. To run the UITest application, enter

kvm -classpath %cldc_classpath% UITest

Once you're done looking at the UITest application, you can take a look at the Pong game application by entering, at the the command line, the command

kvm -classpath %cldc_classpath% Pong

You can now go through and run each of the sample applications. Later you'll use the same method to run the sample application we'll develop on your desktop machine.

Building a Palm App

Now that you have a feel for the pieces that make up J2ME, let's move to actually building an application in Java to run on a Palm OS device. The first thing to realize is that just because J2ME is Java, that doesn't mean you can take your existing Java applications, re-compile them, and have them run on new devices. Key features found in the Java Virtual Machine used by J2SS and J2EE have been removed from J2ME's virtual machine to enable it to run on limited resource machines.

The Java Virtual Machine that comes with J2ME (known as KVM) lacks support for floating point numbers. Another important difference between J2ME applications and J2SE and J2EE applications is the lack of preemptive multitasking. While not a requirement of the Java specification, almost all machines your J2SE and J2EE applications run on support preemptive multitasking. This means that the CPU is shared between multiple Java threads without adding any special code to your programs. This is not the case for many of the devices targeted by J2ME. In order to insure that multi-threading works, you'll have to include calls to Thread.yield() method at any place where your code will tie up the processor -- which is usually inside of loops.

Pages: 1, 2

Next Pagearrow