J2ME
Java 2 Mobile edition in something a bit smaller than a nutshell
Ok. Lets write a quick J2ME app
Get the J2SE toolkit.
Download Sun's J2SE sdk
and install. This gives you a java compiler.
Get the J2ME wireless toolkit
Get this from Sun's J2ME
downloads page. This gives you the classes to create a J2ME app. I have
installed mine in c:\j2me. You will need to set up the path and environment
Write yourself a MIDLET
Applets, Servlets and now MIDlets. A MIDLet is a Mobile Information Device
Profile application. Think of it as a mini applet that runs on a mini Java
Virtual Machine, that in this case will run on a phone.
Here is our example, WellHello.java:
import javax.microedition.midlet.* ;
import javax.microedition.lcdui.* ;
//Spuggy Software midlet example 12/12/03
//Author: Monkey-Programmer-Boy
public class WellHello extends MIDlet implements CommandListener {
private Command cmdExit ;
private Command cmdSayHello ;
private TextBox textBox ;
private Display display ;
public WellHello() {
//create the command buttons to control the app
cmdExit = new Command("Exit", Command.EXIT, 1);
cmdSayHello = new Command("Hello" , Command.SCREEN ,1) ;
// Create a text box for out intial message
textBox = new TextBox("Hello MIDlet", "It's a Spuggy Software Test MIDlet
thing..
groovy",256, TextField.ANY | TextField.UNEDITABLE);
}
public void startApp() {
//Add command objects and set listeners
textBox.addCommand(cmdExit);
textBox.addCommand(cmdSayHello) ;
textBox.setCommandListener(this);
// Set the MIDlet's display to its initial screen
display = Display.getDisplay(this);
display.setCurrent(textBox);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
public void commandAction(Command command, Displayable screen) {
//clicked the exit button so exit dammit.
if (command == cmdExit) {
destroyApp(false);
notifyDestroyed();
}
//clicked the hello button so change the text to a hello message
if (command == cmdSayHello) {
textBox.setString("Hello there computer nitwits.") ;
}
}
}
Compile the MIDlet
Compile the MIDLet with the command
javac -classpath c:\j2me\midp\classes -d c:\j2medev\tmpclasses c:\j2medev\WellHello\WellHello.java
This will create WellHello.class in the c:\j2medev\tmpclasses directory
Preverify the MIDLet
The preverifier creates new .class files that contain additional information
that the MIDP Reference Implementation requires. Do it with this command:
c:\j2me\midp\bin\codeverify -classpath c:\j2me\midp\classes c:\j2medev\tmpclasses
The file will appear in c:\j2medev\output.
Create your self a JAR file containing the MIDLet
First create a manifest file like this:
Manifest-Version: 1.0
MIDlet-Name: WellHello
Created-By: 1.4.2_01 (Sun Microsystems Inc.)
MIDlet-Vendor: Spuggy Software
MIDlet-1: WellHello,,WellHello
MIDlet-Version: 1.0
MIDlet-Description: A J2ME example.
I saved mine as c:\j2medev\output\WellHello.mf.
Create a jar file with the WellHello.mf and WellHello.class in it, like
so:
c:\j2medev\output\jar -cmf WellHello.mf WellHello.jar WellHello.class
Create a JAD file
Create a Java Application Descriptor file. Like so:
MIDlet-1: WellHello,, WellHello
MIDlet-Data-Size: 0
MIDlet-Description: A J2ME example.
MIDlet-Name: WellHello
MIDlet-Vendor: Spuggy Software
MIDlet-Version: 1.0
MicroEdition-Configuration: CLDC-1.0
MicroEdition-Profile: MIDP-1.0
MIDlet-Jar-URL: http://www.spuggy.co.uk/WellHello.jar
MIDlet-Jar-Size: 1398
It is very important that the common values in the mf and .jad files are
dentical. Save this as WellHello.jad. Note the size fo the JAR 1398! This
is also vital.
Create a quick WML page
Create a simple wml page so we can download the MIDlet to our phone, like
so:
<?xml version="1.0" ?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml"
>
<wml>
<template>
<do type="codev" label="Back">
</do>
</template>
<card id="card1" title="Example Page" newcontext="true">
<p align="center">
Example Page<br/>
<a href="http://www.spuggy.co.uk/WellHello.jad">Well Hello</a><br/>
</p>
</card>
</wml>
I called with m.wml to save typing on the phone!
Upload files to web server
Upload the folowing to the web server:
m.wml
WellHello.jad
WellHello.jar
Make sure your web server is configured for JAD files
Make sure your web server is configured to handle JAD and JAR and WML files.
To do this add the following lines to the .htaccess
AddType text/vnd.sun.j2me.app-descriptor jad
AddType application/java-archive jar
for Notes add the following to httpd.cnf:
#
# WAP Formats
#
AddType .wml text/vnd.wap.wml 8bit # WML
AddType .wmlc application/vnd.wap.wmlc # Compiled WML
AddType .wmls text/vnd.wap.wmlscript 8bit # WML Scripts
AddType .wmlsc application/vnd.wap.wmlscriptc # Compiled WML Scripts
AddType .wbmp image/vnd.wap.wbmp # Wireless Bitmap Images
#
# J2ME Stuff
#
AddType .wml text/vnd.wap.wml 8bit # WML
AddType .jad text/vnd.sun.j2me.app-descriptor for .jad files
AddType .jar application/java-archive for .jar files.
Load the emulator and download the MIDLet
Load the emulator, by typing MIDP
and open the URL to the app.
In this case: http://www.spuggy.co.uk/m.wml
Summary
So you just create jar file, jad file and wml file and you are away; The over the air install is realy cool too.
In further articles I will go into the J2me developemnt environment and featureset in more detail. Yaba yaba.