The Build Procedure

There are six steps in the creation of a MIDlet. These are:



Design

This is essential for even the smallest of the projects. A good design can eliminate most of the roadblocks you are going to face with “jumping into the code” style of programming. You need to keep in mind some of the elemental features of a mobile application. The User Interface in a mobile phone is substantially different from a user interface available in Microsoft Windows or Linux Gnome/KDE. In windows forms, a large amount of data can be provided to the application through a wide range of widgets available like text fields, radio buttons, combo boxes etc. due to the large screen size. In a mobile phone, the screen real estate is limited and hence you may want to limit the widgets in each screen to a minimum and take the user through a sequence of screens to get the needed data (much like a wizard in Windows). In usual commercial production environment it is always better to design the application UI in Flash first and then code it accordingly.

Code

Every MIDlet must extend the abstract class MIDlet in javax.microedition.midlet package. Then you have to override three methods of this abstract class:

· startApp()

· pauseApp()

· destroyApp()

We will come to these methods later when we discuss the MIDlet life cycle.

Remember that there will NOT be a “public static void main()” method anywhere in our program.

Compile

Similar to Java SE technique, the code has to be compiled to byte code form. This is done using the javac compiler.

Preverify

Before running any class file the JVM performs a verification of the byte code to ensure that it is structurally and conceptually correct as per the JVM specification. If the class file fails this check, it is rejected and the JVM shuts down. This is done by all JVMs including our CLDC JVM. But there is a problem. Verification is a costly process, both in turns of memory and processor cycles. And hence a normal mobile phone won’t be able to verify class files. As a solution to this, the verification process itself was divided into two: a preverification which is done in the desktop development station and a simple verification by the mobile phone JVM which checks whether preverification was done earlier or not. The preverification process adds special information to the class files that identified them as preverified. This makes the process on the device more efficient.

Package

The java application that we developed is then send to the customers in the form of two files – the JAD file and the JAR file.

The JAD (Java Application Descriptor) file is a text file, which describes the application. It contains the name of the application, the version of the application, the version of CLDC and MIDP required to run the application, the location of the jar file, the jar file size etc.

The JAR (Java Archive) file is a compressed zip file, which contains all the preverified class files together with any other resources that the application requires like audio, images etc. This JAR file can be opened with any zip file handler like winzip and studied. The JAR file contains an important file called Manifest.mf. This file also contains information about the application similar to the jad file.

Test

We can test the application using the emulator available in WTK or using a real mobile device.

Using the Wireless Toolkit

Since we have studied the build process, we will see how to perform these steps using the Wireless Toolkit. It would be a good idea now, to learn bit more about the WTK.

If you installed the WTK to C:\WTK25 then these are the folders you will find there:

appdb

This directory is the root for the emulators file system.

apps

The project directory. All project are created here.

bin

Contains the binaries for the WTK itself and for other need like the compiler, emulator etc

docs

The documentation for the WTK along with API reference for CLDC and MIDP.

lib

Contains the jar files for MIDP, CLDC and a large number of optional packages.

sessions

Folder where the networking and profiling sessions are saved.

wtklib

Contains the library files (jar files) used by the WTK itself.

Now run the Wireless ToolKit as mentioned in the Installation section and select “New Project” from the menu bar.

You will get a window as shown below:

Enter the project name as “AnoopKS” and Class Name as “HelloMidlet”; then press “Create Project”. The settings window should be displayed.


Use the default settings and click ok. You can see the various messages posted on the main window:

If you go to C:\WTK25\apps\ you can see that a folder named AnoopKS have been created. All the .java files that you write should be put into the C:\WTK25\apps\AnoopKS\src folder.

Now take Notepad and copy-paste the following code into it.

import javax.microedition.midlet.*;

import javax.microedition.lcdui.*;

public class HelloMidlet extends MIDlet {

public void startApp() {

System.out.println("Hello World");

}

public void pauseApp() {

}

public void destroyApp(boolean unconditional) {

}

}

Save it as HelloMidlet.java in the folder C:\WTK25\apps\AnoopKS\src. This code doesn’t do anything fancy. All it does is print “Hello World” on the WTK’s message area and exit.

Now select “Build” from the tool bar:

If you have typed the program correctly build should be success. Now you can run the application in an emulator by selecting “Run” from the tool bar.

The emulator will be displayed. Select “Launch” and you can see the string “Hello World” being displayed on the WTK messages area.

Go to C:\WTK25\apps\AnoopKS\bin directory and you can see that a JAD file have been created. But still there is no JAR file. To create JAR file select the menu: Project -> Package -> Create Package.