Java 2ME and MIDP Development
Pages: 1, 2
Installation Checklist
The PATH should now look something like the following (not including any entries that may already be a part of your path):
PATH=c:\jdk1.3\bin; c:\j2me\j2me_cldc\bin;c:\j2me\midp-fcs\bin;
You can check to see what the PATH variable contains by going to a command (DOS) prompt and entering:
set PATH
If you look in Windows Explorer, the directory hierarchy should now include:
c:\jdk\1.3\bin
c:\j2me
|
j2me_cldc
|
midp-fcs
Test Your Installation
Before writing a MIDlet, let's verify that all the software was installed correctly.
- Go to a command prompt
- To test the CLDC installation, type in
preverifyand press enter. You should see a screen that looks similar to the following:

- To test the MIDP installation, type in
midpand press enter. You should see a window pop up that looks like the image below:

- To test the JDK installation, type in
java -versionand press enter. A screen like the following should appear:

Troubleshooting
Here are a few things to look for if any of these steps fails:
Make sure each program is installed correctly. Use Windows Explorer to find the program causing the problem. For example, locate the program midp.exe. For my installation this in the directory:
C:\j2me\midp-fcs\bin. Double click the program and it should start. If you cannot locate the file, or it doesn't run, you may need to uninstall and reinstall the application.If it runs but fails when called from the command line, you know that something is incorrect in PATH. Remember, the PATH variable tells Windows where to located executable files.
Verify the directory paths in the PATH environment variable are correct. Once again, using Windows Explorer, locate the directory of the executable as shown above, and look in the "Address" inside Explorer (see below). This is the full directory path to the program. You can copy and paste this directory into the PATH variable.
Your First MIDlet
Let's write a very simple MIDlet that will create a textbox with a message. We'll also add a command button to exit the MIDlet.
Writing the Java Source Code
Create a new directory to
hold your MIDlet, for example, c:\midlets
Within this directory create directory called
firstMIDlet. Using any text editor, create a file called
simpleMIDlet.java and type in the Java source code shown
below. Save the file in the directory
c:\midlets\firstMIDlet (or whatever path you choose). The
full path to the MIDlet source file should now be something like
c:\midlets\firstMIDlet\simpleMIDlet.java.
simpleMIDlet.java
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class simpleMIDlet extends MIDlet implements CommandListener
{
private Display display; // Reference to Display object for this MIDlet
private TextBox tbxMain; // Textbox to display a message
private Command cmdExit; // Button to exit the MIDlet
// MIDlet constructor
public simpleMIDlet()
{
display = Display.getDisplay(this);
cmdExit = new Command("Exit", Command.SCREEN, 1);
tbxMain = new TextBox("Simple MIDlet", "Welcome ", 50, 0);
tbxMain.addCommand(cmdExit);
tbxMain.setCommandListener(this);
}
// Called by application manager to start the MIDlet.
public void startApp()
{
display.setCurrent(tbxMain);
}
// A required method
public void pauseApp()
{ }
// A required method
public void destroyApp(boolean unconditional)
{ }
// Check to see if Exit command was selected
public void commandAction(Command c, Displayable s)
{
if (c == cmdExit)
{
destroyApp(false);
notifyDestroyed();
}
}
}
At this point, don't worry about what each line of code is doing. Focus instead on the development cycle. In a future article we will cover all the code, from top to bottom.
Compile and Preverify
Now you must compile the java
source file and preverify the resulting class file.
| What is preverification? Checking the integrity of class files is not a trivial operation. On the standard Java Virtual Machine (J2SE) the verifier code takes a minimum of 50 kilobytes, not including heap space requirements and processing time. To reduce the system requirements, and spread the work load, class file verification has been broken into two steps. One step done during development (as shown above) and one on the device itself. |
Compile the Source Code
Go to a command prompt. Change to the project directory where you saved the file. Compile the program by giving a command like
javac -bootclasspath c:\j2me\midp-fcs\classes simpleMIDlet.javaThe option
-bootclasspath c:\j2me\midp-fcs\classesspecifies were to locate the Java bootstrap (startup) class files. We must point to the midp classes otherwise the JDK classes will be used.The file firstMIDlet.class will be created (by default, in the same directory as the Java source file) after successfully compiling
firstMIDlet.javaPreverify the class file
preverify -classpath c:\j2me\midp-fcs\classes;. -d . firstMIDletThe option
-classpath c:\j2me\midp-fcs\classes;.specifies where to locate the class files for preverification. This includes the MIDP classes that are needed as part of the verification process (specified byc:\j2me\midp-fcs\classes) and your class file, which is located in the current directory (specified by ".").The option
-d .informs the preverifier where to put the verified class files. The "." specifies the current directory, the same location as the original class file.
Note: The preceding preverify command line options will overwrite the original class file with a new, preverified class file. You may want to separate class files into two directories, those created by compiling, and those created by preverifying. Class files that are not preverified cannot be loaded by the application manager.
Run the MIDlet
We are now ready to run the MIDlet inside the mobile device emulator. While still at the command prompt, enter
midp firstMIDlet
You should see the following output.
There it is, your first MIDlet. Once you've been through the
development of one MIDlet, the process doesn't change much. I like to
put all the steps (compile, preverify, etc.) into a DOS batch
file. Then I can run a single program to accomplish all the steps. For
example, here is a simple batch (cc.bat) file that I used
to create and test this MIDlet.
javac -bootclasspath c:\j2me\midp-fcs\classes firstMIDLet.java
pause
preverify -classpath c:\j2me\midp-fcs\classes;. -d . firstMIDlet
midp firstMIDlet
The pause command will wait for a keypress before
executing the next command. If you have an error in your Java source
file, you can press Control-C to exit the batch file, skipping the
preverification and launching of the MIDlet.
Summary
With the exception of the software requirements -- installing CLDC and MIDP -- creating a MIDlet is really no different than writing any standard Java application. The CLDC provides a slimmed down version of the J2SE, while MIDP provides specific libraries for developing mobile device applications.
In the next article, we will look into packaging multiple MIDlets using a Java archive file (jar) and Java application descriptor file (jad). We'll also show an example MIDlet that uses Java Packages, and we'll discuss how this affects the jar and jad files. We'll also show you how to upload and preview your MIDlet(s) from a web server.
John W. Muchow is an expert Java Developer and Trainer specializing in J2ME.
Return to ONJava.com.