RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Port Your Java MIDlets to a Palm Device : Page 3

The Palm platform is a significant portion of the pervasive computing space. Porting your J2ME MIDlets as pilot resource code opens the door to this large Palm device audience with little coding effort.


Create a Simple J2ME Application

For demonstration purposes, create a small J2ME MIDlet that displays the current time when you invoke it on your Palm device. The following is the code for this simple J2ME application (click here to download a zip file of this code):

package com.j2me.example;

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.util.Calendar;

public class TimeTeller extends MIDlet {

    private Display display;
    TextBox box=null;

    public TimeTeller(){


    public void startApp() {

	Calendar now = Calendar.getInstance();
	int hours = now.get(Calendar.HOUR_OF_DAY);
	int minutes = now.get(Calendar.MINUTE);
	if (hours==0) hours+=12;

	String message = "The time is ";
	box=new TextBox("Simple Example", message, 20, 0);

    public void pauseApp() {


    public void destroyApp(boolean b) {


Place the above code in a file named TimeTeller.java under the directory structure: \com\j2me\example. Assuming you have the Java compiler in your path and you are in the parent directory where you placed the TimeTeller source code, run the compilation command:

javac –bootclasspath \lib\cldcapi11.jar;
    \lib\midpapi20.jar com\j2me\example\TimeTeller.java

So for example, if your J2ME wireless toolkit installation directory is c:\wtk22\ (i.e., the default), then your command would look like the following:

javac –bootclasspath c:\wtk22\lib\cldcapi11.jar;
    c:\wtk22\lib\midpapi20.jar com\j2me\example\TimeTeller.java

[Author Note: The remainder of the article assumes that the J2ME toolkit was installed in c:\wtk22. If this is not the case for you, modify the commands accordingly.]

Notice the inclusion of cldcapi11.jar and midpapi20.jar to your bootclass path during compilation. By specifying this compilation option, you are telling your JDK to use J2ME libraries so you don't inadvertently pick up J2SE classes during your compilation. The compilation should produce a TimeTeller .class file in your com\j2me\example subdirectory.

Next, you need to preverify the class you produced so that it can be run on a J2ME target device. Without delving too deeply into the science of J2ME, you perform preverification because you do not have the luxury of intense bytecode verification in your J2ME device's virtual machine. From the parent directory of the TimeTeller class, run the following preverification command:

c:\wtk22\bin\preverify -classpath c:\wtk22\lib\cldcapi11.jar;
    c:\wtk22\lib\midpapi20.jar com.j2me.example.TimeTeller

This command should produce a directory called "output", which is under the packaging directory structure com\j2me\example. In this subdirectory, you should find the preverified version of the TimeTeller.class with the same name (TimeTeller.class).

Next, you need to package your class as a JAR file. The JAR file must contain a Manifest file that describes your MIDlet. In the output directory that was automatically created in the preverification step, create a file named Manifest.mf with the following text in it:

Manifest-Version: 1.0
MIDlet-1: TimeTeller, , com.j2me.example.TimeTeller
MIDlet-Name: TimeTeller
MIDlet-Version: 1.0
MIDlet-Vendor: Kulvir Bhogal
MicroEdition-Configuration: CLDC-1.0
MicroEdition-Profile: MIDP-1.0

(Note that a new line after the last attribute must be present. Otherwise, this attribute will not be recognized.)

Create the JAR file with the following command issued from the output directory:

JAR cvfm TimeTeller.jar Manifest.mf .\com

Issuing the command above should yield something like the following:

added manifest
adding: com/(in = 0) (out= 0)(stored 0%)
adding: com/j2me/(in = 0) (out= 0)(stored 0%)
adding: com/j2me/example/(in = 0) (out= 0)(stored 0%)
adding: com/j2me/example/TimeTeller.class(in = 1307) (out= 705)(deflated 46%)

Click to enlarge 
Figure 11. Pointing the J2ME Toolkit Install to Your J2SDK Install

For a J2ME device to be able to use the JAR file you just produced, you also need to create a Java Application Descriptor (JAD) file. Create a file named TimeTeller.jad in the output directory with the following content:

MIDlet-Name: TimeTeller
MIDlet-Version: 1.0
MIDlet-Vendor: Kulvir Bhogal
MIDlet-Description: Simple J2ME Program
MicroEdition-Profile: MIDP-1.0
MicroEdition-Configuration: CLDC-1.0
MIDlet-1: TimeTeller, , com.j2me.example.TimeTeller
MIDlet-Jar-URL: TimeTeller.jar
MIDlet-Jar-Size: 1603

Click to enlarge 
Figure 12. Trying TimeTeller Out on Sun's MIDP Emulator

Pay particular attention to the name value pairs of options that exist in both the JAD file and the Manifest file you created earlier. Also, notice the MIDlet-Jar-Size name value pair. You will need to set the value of this option to the number of bytes that your TimeTeller.jar file takes on your machine. In my case, this was 1,603 bytes, as shown in Figure 11.

At this point, make sure that your J2ME application works as expected using the J2ME Toolkit's built-in emulator. Issue the following command from your output directory:

C:\wtk22\bin\emulator –Xdescriptor:TimeTeller.jad

Doing so should yield your J2ME application deployed to the Sun's MIDP emulator as shown in Figure 12.

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date