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


AddThis Social Bookmark Button

What's J2ME?
Pages: 1, 2

Another tricky part of using the CLDC profile is that the class library lacks many of the classes found in the J2SE class library. You'll have to work within the limitations of this smaller class library while building your application. You can find out which classes are included in the CLDC specification by reviewing the documentation you get when you download the software. The final tricky area you'll have to deal with is the lack access to UI elements -- buttons, menus, graphics, from within the CLDC class library. Since it's next to impossible to develop an application for a Palm OS device without using the Palm's UI, Sun had addressed this problem with the inclusion of a separate class library, com.sun.kjava, which gives you access to the underlying Palm user interface. The following sample program relies on this library to create windows, buttons, and dialog boxes. Be warned that the kjava class library is not an official part of either the MIDP configuration or CLDC profile, and it is likely to change or disappear in a future release. This means that any programs you write that rely on the kjava class library will have to be modified to work with future versions of J2ME. But, right now, it is your only option for accessing the Palm's user interface from within a Java application.

import lang.*;
import com.sun.kjava.*;

public class HelloWorld extends Spotlet
implements DialogOwner {

private Button quitButton;
private Button aboutButton;

private TextBox welcomeTextBox;

private final String welcomeString = "Hello World sample applications demonstrating the use of J2ME and the com.sun.kjava library to produce a Palm Pilot application in Java.";
private final String aboutTitleString = "About Hello World";
private final String aboutString = "HelloWorld J2ME CLDC Application\n (c) 2001 Daniel A. Tauber\ninfo@tauberkienan.com\nhttp://www.tauberkienan.com";

public HelloWorld() {

;welcomeTextBox = new TextBox(welcomeString, 10, 10, 140, 130);
aboutButton = new Button("About", 75, 140);
quitButton = new Button("Quit", 125, 140);

public void dialogDismissed(String title) {

public void paint() {

public void penDown(int x, int y) {
if (aboutButton.pressed(x, y)) {
Dialog aboutDialog = new Dialog(this, aboutTitleString, aboutString, "OK");
if (quitButton.pressed(x, y))

public static void main(String args[]) {
new HelloWorld();

Dissecting the HelloWorld Application

The HelloWorld application shows the basic features of a CLDC application for a device running the Palm OS. As you look over the code, you'll see a number of features of building applications using the kjava class library. First, the program extends the com.sun.kjava.spotlet class. With kjava, a spotlet is any class that can get focus, that is, receive events from the system such as pen down. In our case, our HelloWorld application needs to respond to pen clicks on the About and Quit buttons, so it needs get the focus. Your application can contain as many classes derived from spotlet as you need, although only one of them can have the focus at any one time. Applications actually take the focus by calling the register method, which we do at the beginning of the HelloWorld constructor and in the dialogDismissed method.

Also, the HelloWorld class implements the DialogOwner interface. A class that implements the DailogOwner interface is a class that creates a dialog box. Our application creates a dialog box in response to a pen click on the About button. To implement the DialogOwner interface, your class needs to include a dialogDismissed member method. This method is called when a dialog box is closed.

The HelloWorld constructor creates three interface elements on the screen: a text box and two buttons. You'll immediately notice one big difference between kjava and J2SE's AWT: kjava does not have any equivalent to AWT's automatic layouts. You have to hard code the position of each interface element on the screen. In our program, we place the text box between 10, 10 and 140, 130. Our two buttons are place at 75,140 and 125, 140. Hard coding positions does not pose a problem when designing for the current crop of Palm OS devices, which always have the same screen size. But hard coding position would be a major headache if your application needed to support devices with multiple screen sizes.

HelloWorld's penDown method is called whenever the user taps the pen on the device. We delegate the work of determining if the click occurred within the bounds of the buttons by calling each button's pressed method in turn. If a button's pressed method returns a true value, which means that the pen down event happened within the bounds of the button, then the proper action for the button is taken. In the case of the About button, we create the About dialog box by creating a new Dialog object. In the case of the Quit button, we terminate the program.

Finally, the HelloWorld's dialogDismissed method is called when the user dismisses the dialog box which we create in response to a click on the About button. In our application, all that this method does is regain the focus by calling the register method and redraw the screen by calling the paint method.


Compiling a CLDC application is a two step process. First you must run them through the standard J2SE compiler, specifying the CLDC class libraries instead of the standard J2SE class libraries. After that, you need to process the resulting class file with the preverify program to turn it into a Java application ready for the KVM.

The above sample program is compiled with the command line

javac -g:none -classpath %cldc_classpath% -d %temp% HelloWorld.java
preverify -classpath %cldc_classpath% -s %temp% -d .

Once it is compiled, you can run the program on your desktop machine using

kvm -classpath %cldc_classpath% HelloWorld

Now that you have a CLDC application that will run on your computer, it's time to turn it into a Palm executable.

Running it on a Palm

In order to turn a .class file developed with CLDC into a file that can actually run on a Palm OS device, you'll need to use a number of programs that are included in the j2me_cldc-1_0-src-palm_overlay.zip file. First, you have to turn your .class file into a Palm OS application. All Palm OS application have the extension .prc. You turn your .class file into a .prc file by using the palm.database.MakePalmApp class. Run this on your HelloWorld.class file with the command line

java palm.database.MakePalmApp -bootclasspath %cldc_classpath% HelloWorld

You'll now have the file HelloWorld.prc in the same directory as HelloWorld.class. This file is the Palm application. You can now use the Palm Installer software that comes with your Palm OS device to install this file. In addition to having the .prc version of your application installed on your Palm OS device, you'll also have to have kvm.prc installed. kvm.prc is an implementation of the K Virtual Machine (remember, that is the version of the Java Virtual Machine specified in the MIDL profile) for Palm OS devices.


A number of resources are available online to continue your investigation of using J2ME to build Java applications for small devices. These sites include

  • Sun's home for J2ME technology. From this site you can download the development tools, documentation, and tutorials.

  • Borland has a version of their popular Java IDE JBuilder Handheld Express geared specifically at building J2ME applications You can download a version of JBuilder Handheld Express from their site.

  • While not currently providing any specific J2ME information, the Palm OS Developers Support page is a good resource for information on building applications for Palm devices. It includes access to a knowledge base and support forums. Since Palm is an active participant in defining J2ME technology, it's a good bet that more Palm specific J2ME information will start to appear on this site as it becomes available.

Daniel A. Tauber is a technical strategist and solutions architect, has led the technical development of websites and desktop applications for Fortune 500 companies and the publishing industry.

Return to ONJava.com.