Modern mobile devices come in more flavors than maybe spiders on earth and
are increasingly creating webs of their own by connecting with each other and to
the Internet through technologies such as WLAN, GPRS/EDGE and 3G/UMTS. This
diversity is a huge challenge for application developers, who have to modify or
port their applications to make them run on various mobile platforms.
Direct Hit! |
Applies To: Mobile app developers USP: Learn to use the power of Java ME for mobile apps Primary Link: http://java.sun.com/ javame/technology/ Keywords: J2ME |
But didn't Java eliminate that problem years back?
In the desktop world, Java and various other languages have been able to make
applications independent of the platform, but what about mobile platforms? The
same technologies are also available in the embedded world of mobile devices,
but with modifications, as mobile devices have some inherent limitations:
1. They have slower processors than their present day desktop cousins,
however, compared to PCs of the mid-90s, they are still way ahead.
2. They have limited amount of primary memory (RAM) and secondary storage
(Flash), but are lucky they dont need to deal with memory hogs such as Windows
Vista.
Java Platform, Micro Edition
One of the leading technologies in this area, which is widely supported, is the
Java Platform--Micro Edition (Java ME). Formerly, this was known as J2ME. The
advantage of using this platform is the same as that associated with the
standard Java technology — platform independent applications. The programming
language remains Java, but there are limitations and modifications. The Java ME
platform is layered and modularized as follows (see Figure 1):
1. The Virtual Machine (VM): This is the core of the Java runtime
environment and is responsible for executing the Java code, independent of the
underlying OS and processor. For the Micro Edition, the VM is modified to run in
resource constraint devices and is often called the KVM or kilo-virtual-machine
due to its smaller size. For less resource constrained devices such as Digital
TVs or Smartphones, the standard Java Virtual Machine (JVM) is used.
2. The Configuration: This is the most basic set of libraries for
broad range of devices. There are two configurations defined by the Java ME
platform:
a. Connected Limited Device Configuration (CLDC): This is a set of
libraries for the most resource constrained devices such as low and mid range
mobile phones.
b. Connected Device Configuration (CDC): This is a set of libraries for
devices with more processing capabilities such as high end mobile phones (smartphones)
and digital TVs.
3. The Profile: The profile is a set of APIs specific to devices such
as a Mobile Phone or a Digital TV. For example, the common profile for the
mobile device is called the Mobile Information Device Profile (MIDP) and runs on
CLDC. On the other hand, the profiles available for the CDC are the Foundation
Profile, the Personal Basis Profile and the Personal Profile.
4. Optional Packages: These are additional libraries for richer user
experience such as multimedia and 3D.
The layered architecture allows the device manufacturers an easy way to
configure the capabilities provided by Java ME on their mobile platforms.
Figure 1: Java ME Architecture |
Developing a Java ME application
In this article we will go through a typical installation of the Micro Edition
tools and develop a sample application which can be tested on PC and deployed on
a mobile device. We will use the Windows XP/Vista PC as our development platform
and write an application using the J2ME tools with CLDC and MIDP. These
applications are called MIDlets.
Requirements and Downloads
1. Minimum Hardware Requirements:
a. 100 MB HDD space, 128 MB RAM
b. 800 MHz Pentium III CPU
2. Software Requirements:
a. Microsoft Windows XP or Microsoft Windows Vista (I tested on Vista Ultimate)
b. Java 2 Platform, Standard Edition (Java SE SDK), version 1.5.0l URL: http://java.sun.com/
javase/downloads/ index_jdk5.jsp
l Download the item named “JDK 5.0 Update 16”
c. Sun Java Wireless Toolkit 2.5.2 for CLDC URL: http://java.sun.com/ products/
sjwtoolkit/ download.html
l Scroll down for the 'Download' button
Figure 2: WTK Folders |
Installation
Install the JDK first (the sequence is important) followed by the Wireless
Toolkit and you are all set to create your first MIDlet. Your installation (by
default) would have created the folders in C:\WTK2.5.2 as shown in Figure 2.
The Toolkit
You can start the toolkit from
Windows Start Button
Programs-> Sun Java Wireless Toolkit 2.5.2 for CLDC->Wireless Toolkit 2.5.2
The WTK Window will come up as shown in Figure 3
Figure 3: WTK Application |
Creating a New Project for Hello World MIDlet
Click on the 'New Project' button. Now, enter the following details and click on
'Create Project'.
Project Name: HelloWorldApp
MIDlet Class Name: com.j2me.part1. HelloWorldApp
This will bring up the API Selection Window shown in Figure 5. Make the
following changes and click OK:
Target Platform: JTWI
Configurations: CLDC 1.1
Optional: None (deselect Mobile Media API)
(Only the relevant part of the window is shown here).
Figure 4: API Selection |
The toolkit will create the project and show messages as in Figure 6. This
shows the paths where you have to create the source files.
Your source file path will be different from the one shown in Figure 6. You
have to use the path as shown in your messages. You can mark and select the
first path shown (source file path) and copy it using Ctrl+C. This will be
required in the next step.
Creating the source file for Hello World Application
Open Notepad and enter the following source code:
package com.j2me.part1;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.Display;
import javax.microedition.midlet.MIDlet;
public class HelloWorldApp extends MIDlet {
Alert myAlert;
public HelloWorldApp() {
myAlert = new Alert("Alert!");
myAlert.setString("Hello World");
}
public void startApp() {
Display.getDisplay(this).setCurrent(myAlert);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
}
Save this file as HelloWorldApp.java in the fully qualified source path,
which is the path you copied in the previous step plus 'com\j2me\part1'. In my
case the path isD:\Users\Sudipto Chanda\j2mewtk\ 2.5.2\apps\ HelloWorldApp\src\com\
j2me\part1
Important: Make sure that you select 'All Files', while saving from
Notepad, otherwise, Notepad may append a '.txt' extension. It is a good idea to
verify the file name to be HelloWorldApp.java.
Running the Hello World Application
Now come back to the WTK window and from the Menu select Project->Run. If there
are no errors, then this will Compile, Preverify, Package, and run the
HelloWorldApp in the Emulator. The Emulator Window is shown in Figure 7.
To run the Hello World Application, you have to click on the button below the
'Launch' item (marked with a red box).
This will execute the application and you will see the message “Hello World”.
Figure 5: Messages |
Deploying the Hello World App on a Mobile Device
Before continuing, please refer to the Manual of your Mobile Device for details
of support of J2ME, method for connecting your device to your PC to copy files
and also check the method for installing J2ME Applications on your device.
You can create the package for deploying to the Mobile Device by selecting
from the Menu Project->Package->Create Package.
This will create the JAR file and the JAD file in a bin folder (at the same
level as the src folder). In my case this is
D:\Users\Sudipto Chanda\j2mewtk\2.5.2\apps\HelloWorldApp\bin
Now copy the files (HelloWorldApp.jar and HelloWorldApp.jad) to your mobile
device using the USB/IrDA/WLAN or whichever connectivity method given for your
mobile. On the device, locate the folder where you copied the files and click on
the JAD file to install the Hello World MIDlet on your Mobile. Now you can run
the MIDlet on your Mobile by clicking on it or as per the procedure given in
your manual.
Figure 5: Emulator |
Conclusion
As you can see, it is really easy to create MIDlets and test them on the PC and
deploy on mobile devices. The applications will run on most modern mobile
phones. The Java ME platform also provides multimedia and 3D Graphics support
for rich applications likes Games. There are many demo applications you can play
around with to get the hang of the tools. You'll find them in C:\WTK2.5.2\apps.