Programming with J2ME (Micro Edition)







The J2ME API is located in /local/apps/Java/j2me_cldc/

Programs written with this API do not run in a standard JRE, instead they run on the KVM (Kilobyte Virtual Machine) which can be run on PalmOS or a desktop simulator.

Just like an applet is based on the java.applet.Applet class, a KVM app's superclass always extends com.sun.kjava.Spotlet

A Spotlet handles basic event handling and can be thought of as the one and only "window" that your application runs within. The Spotlet class has no support for AWT or SWING. However, there are many useful widjets such as Button, Bitmap, List, and Dialog in the com.sun.kjava.* classes.

Documentation for the GUI API is located here: /local/apps/Java/j2me_cldc/docs/KJavaAPI/index.html

Documentation for all other classs is located here: /local/apps/Java/j2me_cldc/docs/CLDCAPI/index.html

Here is some sample code to get you started:

import com.sun.kjava.*;

public class sampleGUI extends Spotlet{ 
        //The Graphics object is always a singleton
        static Graphics g = Graphics.getGraphics(); 

        private Button quitButton;

        //Create the Spotlet, register event handlers 
        public static void main(String[] args){ 
                (new sampleGUI()).register(NO_EVENT_OPTIONS);
        }

        public sampleGUI(){
                quitButton = new Button("Quit",140,0);  
                paint();
        }

        public void paint(){
                g.clearScreen(); g.drawString("Sample GUI",50,50);
                quitButton.paint();
        }

        public void penDown (int x, int y){ 
                //Does (x,y) fall within the buttons boundaries? 
                if(quitButton.pressed(x,y)){ 
                        System.exit(0); 
                }
        }
}
 

 
 
 
 
 
 
 
 

To compile:

You must always comile your j2me code with the classpath and bootclasspath directed to the j2me API. You don't want to change this in your .cshrc file because you don't want your other java programs looking here for the standard API.

javac -g:none -classpath /local/apps/Java/j2me_cldc/bin/api/classes -bootclasspath /local/apps/Java/j2me_cldc/bin/api/classes sampleGUI.java
 
 

Preverificatioin:

Once your code is compiled you must preverify the class file before it can run in the KVM. Because the KVM runs in a limited environment, it is better to do as much verification as possible before running your application in the KVM. You must again direct the classpath to the j2me API and also to the class that you are prevrifying.

/local/apps/Java/j2me_cldc/bin/preverify -classpath /local/apps/Java/j2me_cldc/bin/api/classes:/nfs/home/users/<your_user_name>/<your_working_dirctory> sampleGUI

This will create a directory called "output" within your working directory. Your preverified class file will be placed within this directory.
 
 

Test your application:

Once your code is compiled and preverified you can test it on a desktop KVM which simulates the PalmOS. Again you must direct the classpath to the j2me API and to your class.

/local/apps/Java/j2me_cldc/bin/kvm -classpath /local/apps/Java/j2me_cldc/bin/api/classes:/nfs/home/users/<your_user_name>/<your_working_directory>/output sampleGUI

Since these commands are so long it is nice to place them all into one compile script.
 

Make a palm executable:

Once you have tested your application on the simulator, you can make a Palm executable by running the MakePalmApp program on your class file:
 

/local/apps/Java/j2me_cldc/bin/api/classes/palm/database/MakePalmApp -bootclasspath /local/apps/Java/j2me_cldc/bin/api/classes-classpath /nfs/home/users/<your_user_name>/<your_working_directory>/output sampleGUI
 
This will create a .prc file that can then be executed on a Palm device.

Caveat: the MakePalmApp program is not yet available on the department system. It needs to be compiled from the orignal source code and placed into the appropriate directory. We have asked system staff to do this. Keep checking.
 
 

Notes on hotsyncing:

It is possible to build Java conduits that will exchange data for your application between a PC and a Palm device when the hotsync command is executed.  Data needs to be written to a database which resides on the Palm and the PC.  The database will synchronize so that all records match everytime hotsync is performed.

You can download the Java Conduit Developement Kit (CDK) from Palm:  http://www.palmos.com/dev/tech/tools/cdk/java/

 
There is a tutorial that comes with the CDK that explains how conduits work and gives sample code of some common conduits.  You can use this code as a starting point for your own conduit.
I am still working on conduit code that will work for this project.  Some issues are:
My database experience is very limited.  These may be very easy issues for students with database experience to conquer.  There is some sample code with the CDK that could be modified to work with this project once the database issues are resolved.

Other Notes

We do have some example J2ME code for using the IR port on the handspring. See Steve if you want to explore this.

Somewhat old links to J2ME articles.

Sun's peer-to-peer effort is called JXTA. It is an open source project that is just getting off the ground. (We hope to cover open source systems in future lectures.) As such, we don't think it is suited for project 2. If your group is still interested in P2P, see Steve.