UNIT II - Chapter 1 : How should I program in J2ME?

Now that we have discussed the introductory aspects of J2ME we should come to the most important question that is coming in your mind – ‘How should I write programs in J2ME?’ In this unit we will discuss the answers to this question. Before coming to the specific aspects of programming, we will discuss the alternatives available to us in terms of the development environments which we can use to aid us in programming using J2ME.

The available alternatives

As a Java programmer many of you must be familiar with the command line tools available for programming Java applications. These tools are also available for the Java 2 Platform, Micro Edition (J2ME). But we are not going to discuss such ugly looking and low productivity tools. We will discuss some of the tools which have been provided to us free of cost by Sun Microsystems and the mobile phone manufacturers for developing applications. These tools have been built keeping in mind the needs of the programmers. Thus we are able to steer into J2ME at a fast pace from day one. Some of the alternatives available to us in terms of development kits are:

a.     Sun ONE Studio 4 update 1, Mobile Edition

Sun ONE Studio 4 update 1, Mobile Edition is an integrated development environment (IDE) which has been provided to us by the Sun Microsystems Inc. This software allows us to develop, edit, customize, compile, build, run and test the code. This is the best alternative that is available to us if we want to develop applications in J2ME without spending any money on buying a software development kit. We will discuss the workings of Sun ONE Studio 4 update 1, Mobile Edition in detail within this chapter.

Screen Shot: Sun ONE Studio 4 update 1, Mobile Edition

b.    J2ME Wireless Toolkit

J2ME Wireless Toolkit is also a free alternative which has been made available to us by the Sun Microsystems Inc. This is a toolkit which provides us the facilities for developing and running J2ME applications. This toolkit has many versions which include 1.0.4_01, 2.0_01, 2.1 and 2.2. This toolkit also provides us with some of the basic emulators which are very helpful in testing the applications on the PC itself. J2ME Wireless Toolkit is an absolute must for any J2ME programmer. In fact even Sun ONE Studio 4 update 1, Mobile Edition includes J2ME Wireless Toolkit and uses its emulators for executing the applications. We will also look at J2ME Wireless Toolkit 2.2 in this book in detail.

Screen Shot: J2ME Wireless Toolkit

c.     Nokia Developer’s Suite for J2ME

Nokia has also provided us with a good development tool for building J2ME applications. Although applications developed through Nokia Developer’s Studio can run on non – Nokia phones, this development toolkit is better suited for the development of software for Nokia phones. Also this development toolkit has an option of integrating with the Sun ONE Studio 4 update 1, Mobile Edition. Thus you can enjoy the benefits of this development kit from Sun ONE Studio 4 update 1, Mobile Edition itself. Some of the newer versions of the Nokia Developer’s Suite contain the facilities for deploying the applications, signing the JAR files and even converting the audio files to formats suitable for mobile phones.

Screen Shot: Nokia Developer’s Suite for J2ME

d.    Sony Ericsson J2ME SDK (Beta)

SonyEricsson’s J2ME Software Development Toolkit is modeled on the lines of the J2ME Wireless Toolkit. In fact some of the parts of this toolkit are common with the J2ME Wireless Toolkit. KToolbar contained in this toolkit is a good example of this. This toolkit provides us the facilities for executing and testing applications on the SonyEricsson phone emulators. Emulators for T610 and P800 are particularly supported. 

Screen Shot: Sony Ericsson J2ME SDK

e.     Motorola iDEN SDK

Motorola iDEN Software Development Kit provides us with the emulators and tools for testing for Motorola iDEN phones. It is one of the better toolkits among the toolkits of handset manufacturers. The versions of this toolkit have been updated quite speedily in the past. I will suggest you to download the latest version from Motorola’s developer website.

f.      Siemens Mobility Toolkit

Like the toolkits of all the other mobile manufacturers the Siemens Mobility Toolkit has been specifically build to facilitate the testing of mobile applications for Siemens phones. We are provided with the support for testing applications on Siemens phone emulators. This toolkit also supports the integration with the various versions of Borland JBuilder.

Screen Shot: Siemens Mobility Toolkit Manager

Any experienced programmer will vouch for the sharp increase in productivity that occurs when he/she uses a good integrated development environment (IDE) for writing, compiling and debugging programs. Therefore in the section below I have discussed the workings of Sun ONE Studio, Mobile Edition. I will personally recommend you to use Sun ONE Studio 4 update 1, Mobile Edition if you want a good development kit for free. Otherwise J2ME Wireless Toolkit 2.2 is also a good alternative. Also J2ME Wireless Toolkit 2.2 supports most of the new APIs discussed in this book. Thus J2ME Wireless Toolkit 2.2 should also be used if you want to fully utilize this book. 

The Workings of Sun ONE Studio 4 update 1, Mobile Edition

Sun ONE Studio 4 update 1, Mobile Edition is a good integrated development environment available to us for developing industrial scale J2ME applications. In fact at SKJ Technologies this is the predominant development tool in use along with J2ME Wireless Toolkit 2.1 and J2ME Wireless Toolkit 2.2. Also if you have used Visual Studio for developing desktop applications then you will find it the most convenient to use development tool out of all the tools discussed so far. We will now discuss the inner workings of the Sun ONE Studio 4 update 1, Mobile Edition in detail. Please remember during all the discussions below that we have discussed the development of applications using Sun ONE Studio within the MS Windows desktop environment.

 

Procedure for creating a project:

  • Create a new folder in the Windows file system.
  • Open Sun One Studio 4 update 1, Mobile Edition.
  • Select ‘Project Manager’ from ‘Project’ menu.
  • Select ‘New’ in the ‘Project Manager’.

Screen Shot: Project Manager

  • Enter the project name.

Screen Shot: Create New Project Dilaog

  • The ‘Project Configuration’ screen will be displayed.

Screen Shot: Project Configuration Wizard

  • 2 Choices will be given in it:
    • Java 2 Standard Edition (J2SE)
    • Mobile Information Device Profile (CLDC/MIDP)
  • Select radio button for the Mobile Information Device Profile (CLDC/MIDP).
  • Click ‘Finish’.

You have created a new project in Sun ONE Studio 4 update 1, Mobile Edition. This new project remembers all the compiler properties for you. Every time you open the project the properties set by you for compiling and building the applications automatically get set in the IDE. You can also add folders and files for editing in a project. Below I have discussed the procedure for mounting a directory for storing code of the project.

Procedure for mounting a directory:

  • Right click on Filesystems in Explorer [Filesystems].

Screen Shot: Explorer [Filesystems] 
  • Select Mount > Local Directory.
Screen Shot: Mounting a local directory from Explorer [Filesystems] 
  • Select your project folder and click ‘Finish’.

Screen Shot: Mounting a local directory

Sun has introduced a concept called the MIDletSuite. This MIDletSuite is the container which can contain multiple MIDlets. The MIDletSuite also has some properties which are used by the Sun 1 Studio for automatically generating the JAD manifest and the JAR file. Now we will discuss the procedure for creating a MIDletSuite.

 

Procedure for creating a MIDletSuite:

  • Right click on the mounted folder in the Explorer (Filesystems).
  • Select new > MIDP > MIDletSuite.
  • Enter the name of the MIDlet suite.
  • The ‘Add MIDlet’ screen will come.
  • Choose the ‘Create New MIDlet’ option in the present scenario. If your mounted directory already contains a MIDlet then choose ‘Use Existing MIDlet’ option.
  • Give the package and class name for you MIDlet.
  • If you chose the ‘Create New MIDlet’ option, select the appropriate template for the new MIDlet. You should encounter the following options:
    • HelloMIDlet

This template creates a MIDlet which displays text on the screen of the mobile device. I would recoomed you to choose this option for your first MIDlet.

    • MIDlet

This template constructs a simple MIDlet.

  • On pressing the ‘Next’ button the ‘MIDlet Properties’ dialog will be displayed on the screen.
  • Select the displayable name of the MIDlet and the icon for the MIDlet. Do remember that these will be displayed to the end user of your application.
  • On pressing the ‘Next’ button the ‘Choose Emulator’ dialog will be displayed on the screen.
  • This option lets you choose the emulator in which your code will run when you execute it.
  • The icons for the MIDletSuite and the MIDlet both will now be added in the Explorer (Filesystems).

In the image above you are viewing the Explorer [Filesystems] which contains the icon for a MIDletSuite named ‘CrimeSuite’.

  • Double click on the name of the MIDlet and start editing your code.

Procedure for creating a new MIDlet:

  • Select New > MIDP > MIDlet.
  • Enter the MIDlet name.
  • On pressing next the ‘MIDlet Properties’ dialog comes.
  • Select the MIDletSuite for the MIDlet. Also choose the MIDlet displayable name and the icon for the MIDlet.
  • Click ‘Finish’ and a new MIDlet is created and its icon appended in the Explorer (Filesystems).

Procedure for creating a sub class of MIDPCanvas:

  • Select New > MIDP > MIDPCanvas
  • Enter the name for the class.
  • Click ‘Finish’ and a subclass of Canvas is created and its icon appended in the Explorer (Filesystems).

Procedure for creating a Class:

  • Select New > MIDP > Class
  • Enter the name for the class.
  • Click ‘Finish’ and a new class is created and its icon appended in the Explorer (Filesystems).

Procedure for editing the contents of a MIDletSuite:

  • Right click on the name of MIDletSuite in the Explorer (Filesystems).
  • The ‘Edit Suite’ dialog will appear.
  • You can add, duplicate and remove the MIDlets from the MIDletSuite in this dialog.
  • You can also change or set the MIDlet name, class and icon from within this dialog.
  • For looking or editing the contents of the JAR file choose the ‘JAR Contents’ tab.
  • You can add or remove the files that you want to get packaged in the JAR file from here.
  • Pressing OK will give effect to the changes in JAR file made by you, if any.
  • If you have made any changes the ‘Synchronize Content’ dialog will come. Click the ‘Yes’ buuton.

The contents of your MIDletSuite and its corresponding JAR file have been changed.

Procedure for viewing the Manifest/JAD file:

  • Right click on the MIDletSuite name in the Explorer (Filesystems).
  • Choose the ‘View Manifest/JAD’ menu item.

Procedure for viewing and editing the properties of the MIDletsuite:

  • Right click on the MIDletSuite name in the Explorer (Filesystems).
  • Choose the ‘Properties’ menu item.
  • The ‘Properties of MIDlet [Properties of MIDletSuite]’ window open.
  • It has the following tabs:
    • Properties
    • Execution
    • Required
    • Optional
    • Text

Alternate procedure for viewing and editing the properties of the MIDletsuite:

  • Left click on the MIDletSuite icon in the Explorer [Filesystems].
  • Below you will see 5 tabs, namely:
    • Properties
    • Execution
    • Required
    • Optional
    • Text

MIDletsuite property tabs:

  • ‘Properties’ Tab

This tab contains the ‘Compression Level’ and ‘File Filter’ options. I would recommend you to give 9 as the ‘Compression Level’. 

Screen Shot: ‘Properties’ Tab

  • ‘Execution’ Tab

This tab helps you choose the emulator which can be used for running the application.

Screen Shot: ‘Execution’ Tab

  • ‘Required’ Tab

This tab has the following fields:

o    Jar Size

This is calculated and populated automatically by the Sun 1 Studio.

o    Jar URL

Enter the location of the Jar file on the server from which the application is to be downloaded. If you want the application to be downloaded from just the PC through Infra Red or Bluetooth port then only the name of the JAR file.

o    MicroEdition Configuration

CLDC-1.0 will automatically be filled by the Sun 1 Studio although you can change it.

o    MicroEdition Profile

MIDP-1.0 will automatically be filled by the Sun 1 Studio although you can change it.

o    MIDlet-Name

The name of the MIDletSuite is set as the value for this field automatically although you can change it.

o    Vendor

The name of your organization can be entered here. Sun 1 Studio will fill this field with name of the user account you are working with under Windows.

o    Version

The version number can be entered here. By default the version number is 1.0.

Screen Shot: ‘Required’ Tab

  • ‘Optional’ Tab

The ‘Optional Tab’ has the following fields under it:

    • Data Size
    • Description

You can enter a short description of you application here. This can be accessed by the mobile application portals for explaining you application.

    • Icon

The path and name of the image file of icon of your application can be entered here.

    • Info URL

The URL of the page which contains more information about the application can be entered here. This helps the user to learn more about your application.

    • User Defined

User defined variables can be entered here.

Screen Shot: ‘Optional’ Tab

  • Text Tab

This field contains only one field which is ‘Encoding’.

 

Procedure for adding classes in a project:

 

  • Right click on the icon of the class you want to add to the project.
  • Select Tools > Add to Project
  • This adds the specified file to the current project.

Screen Shot: Adding a class to the project

  • This is necessary if you want to use ‘Compile Project’ and ‘Build Project’ menu options.

Procedure for executing an application

Although clicking on the ‘Play’ button will execute the application; out of experience I will recommend you the following practice.

  • Save all the files.
  • Select the root application folder in the Explorer (Filesystems).
  • Select Build Menu > Clean All

Screen Shot: Build menu

  • Select Project Menu > Compile Project (Remember to add all the code files to the project)
  • Select Project Menu > Build Project
  • Select Project Menu > Execute Project or press Ctrl+Shift-F6 to execute the project.

Procedure for opening an existing project:

  • Select Project Menu > Project Manager

 

Screen Shot: Project menu

  • Choose one of the listed projects
  • Click ‘Open’.

Procedure for deleting an existing project:

  • Select Project Menu > Project Manager

Screen Shot: Project Manager

  • Choose one of the listed projects
  • Click ‘Delete’.

The Workings of J2ME Wireless Toolkit 2.2

The Sun Microsystems Inc. has launched its newest version of J2ME Wireless Toolkit as a set of tools for creating Java applications that run on devices compliant with the Java Technology for the Wireless Industry (JTWI, JSR 185) specification. The J2ME Wireless Toolkit 2.2 contains the software development tools, utilities, and 4 device emulators.

The J2ME Wireless Toolkit 2.2 contains the following features among others such as:

ü  4 Inbuilt Emulators

o    DefaultColorPhone

o    DefaultGrayPhone

o    MediaControlSkin

o    QwertyDevice

ü  Obfuscation support ( Discussed later)

ü  MIDlet signing

ü  Certificate management

ü  Integrated over-the-air (OTA) emulation

ü  Screens for customizing the MIDletSuite settings in great detail

ü  Push registry emulation

The J2ME Wireless Toolkit 2.2 helps us develop the J2ME applications using the following APIs:

  • Connected Limited Device Configuration (CLDC) 1.0 (JSR 30)
  • Connected Limited Device Configuration (CLDC) 1.1 (JSR 139)
  • Mobile Information Device Profile (MIDP) 1.0 (JSR 37)
  • Mobile Information Device Profile (MIDP) 2.0 (JSR 118)
  • Java Technology for the Wireless Industry (JTWI) 1.0 (JSR 185)
  • Wireless Messaging API (WMA) 1.0 (JSR 120)
  • Wireless Messaging API (WMA) 2.0 (JSR 205)
  • Mobile Media API (MMAPI) 1.1 (JSR 135)
  • PDA Optional Packages for the J2ME Platform (JSR 75)
  • Java APIs for Bluetooth (JSR 82)
  • J2ME Web Services Specification (JSR 172)
  • Mobile 3D Graphics API for J2ME (JSR 184)
 

System Requirements for installing J2ME Wireless Toolkit 2.2 

Hardware

Minimum hardware requirements for installing J2ME WTK 2.2 are:

  • 50 MB hard disk space
  • 128 MB system RAM
  • 800 MHz Pentium III CPU

Software

  • Microsoft Windows XP or (unsupported) Linux-x86
  • If you plan to do actual development - JavaTM 2 SDK, Standard Edition (J2SE SDK), version 1.4.2
  • If you only plan to run the demonstration applications - JavaTM 2, Standard Edition Runtime Environment (JRE), version 1.4.2

Procedure for Installation

To install the J2ME Wireless Toolkit 2.2 on Windows PC run the installer file, j2me_wireless_toolkit-2_2-windows.exe. Then follow the instructions provided by the installer.

To install the J2ME Wireless Toolkit 2.2 on Linux, run the self-extracting installer j2me_wireless_toolkit-2_2-bin-linuxi386.bin.

Downloading J2ME Wireless Toolkit 2.2

For downloading J2ME Wireless Toolkit 2.2 please visit the following web page:

http://java.sun.com/products/j2mewtoolkit/download-2_2.html

To download the SDK or JRE you can visit http://java.sun.com/j2se/downloads.html. 

Procedure for creating a new project:

ü  Click on the ‘New Project…’ button in the KToolbar or choose ‘New Project…’ menu item from the File menu in the KToolbar for opening the ‘New Project’ dialog.

ü  Give the name of your project.

ü  Give the name of your MIDlet’s class.

ü  A new project will be created.  A folder dedicated to the code, resources, library files and application for the project will be created in the ‘apps’ folder of the directory in which the J2ME WTK 2.2 has been installed. You can write your code in any text editor program like NOTEPAD, WORDPAD, EDITPLUS etc. and save that in the ‘src’ folder within the project’s folder.

 

Procedure for opening an existing project:

ü  Click on the ‘Open Project…’ button in the KToolbar or choose ‘Open Project…’ menu item from the File menu in the KToolbar for opening the ‘Open Project’ dialog.

ü  Select the name of your project.

ü  The selected project will be loaded in the KToolbar.

 

Procedure for building a project:

ü  Click on the ‘Build’ button in the KToolbar or choose ‘Build’ menu item from the Project menu in the KToolbar for building your project.

Procedure for executing a project:

ü  Click on the ‘Run’ button in the KToolbar or choose ‘Run’ menu item from the Project menu in the KToolbar for building your project.

Given below is a screen shot of a project executing within an emulator.

Procedure for packaging the JAR file of the project:

ü  Choose the Project ->Package -> Create Package menu item in the KToolbar for packaging a Java Archive File (JAR file) of your project.

Procedure for packaging an obfuscated JAR file of the project:

ü  Choose the Project ->Package -> Create Obfuscated Package menu item in the KToolbar for packaging an obfuscated Java Archive File (JAR file) of your project.

 

What is Obfuscation?

 

Obfuscation refers to a procedure where the Java bytecode of the classes in the JAR file are so changed that they can not be reverse engineered easily. In doing obfuscation the obfuscation programs also shrink the size of the code. Please remember to obfuscate your application if you want to market them to third parties.

 

How to install an Obfuscator in J2ME WTK 2.2?

 

ü  Visit http://proguard.sourceforge.net/.

ü  Select "Downloads" on this page.

ü  Choose the latest version of proguard.zip and save it to your computer.

ü  Extract the proguard.jar file from proguard.zip into the bin subdirectory of the directory in which J2ME WTK 2.2 has been installed.

Procedure for changing the settings in a project:

J2ME WTK 2.2 gives us an option to tinker with the following types of settings:

ü  API Selection

ü  Required Fields

ü  Optional Fields

ü  User Defined Fields

ü  MIDlet management information

ü  Push Registry

ü  Permissions

Just click on the ‘Settings…’ button in KToolbar or ‘Settings…’ menu item in the Project menu in the KToolbar for opening the ‘Settings’ dialog. Choose your desired settings and press OK button.

Procedure for choosing between the emulators in KToolbar:

ü  Click on the device combo box in the KToolbar and select the needed emulator.

Procedure for adding new emulators:

ü  Open the directory in which the J2ME WTK 2.2 is installed.

ü  Open the ‘wtklib’ directory within that directory.

ü  Open the ‘devices’ directory within the ‘wtklib’ directory.

ü  Copy the folder containing the data for the emulator in this directory.

Now open the KToolbar. You will see your emulator added in the ‘Device’ combo box within the KToolbar. In case your emulator does not appear than you must have not copied the correct files for that emulator from the internet.
Comments