J2ME: Hello World simple Mobile application

feature helloworld

In this tutorial we will be showing you what you need and how you will create a basic java application for mobile phone using what it is called (MIDlets) in java 2 Microedition. We will be creating a simple Hello word j2me application that would display a text « Hello world » on a mobile device.

1.Necessary resources

      We will use:

       -The Sun Java wireless tool kit version 2.5.2_01: It is an open source set of tools to creat Java applications that run on Mobile devices compliant with the Java Technology for the Wireless Industry (JTWI, JSR 185) specification and the Mobile Service Architecture (MSA, JSR 248) specification. It offers you a build tools and utilities and a device emulator ( that help you to test your mobile application as if it were on a real mobile phone.

    – And a simple java text editor like Textpad, that you can download a free version online 

         Before starting,  download the Sun Java wireless tool kit version 2.5.2_01 or a higher version here and download the SDK( java Standard Edition SDK), or JRE(Java Runtime Environment ) here (go to ). We will choose to install because JDK (Java Development Kit) because it includes the JRE and is one important to be able to develop Java applications and applets. Where as JRE is only necessary on the system to run Java applications and applets. Windows even comes with its own JRE.

         Then download Textpad here  if you don’t have it.

 

2. Check if JDK is already install

 To check if the JDK is already install on your computer, go to the prompt and type the command line: java -version  like in the screen shot below:

testing-if-jdk-is-install

If you got the same message with or not the same version number like below it means that you have JDK already installed on your computer. You don’t need to reinstall it, but only to make sure that the path throught the java.exe file has been specified in the classpath. If not, you must install it.

 

3.Installation

       Start by installing the JDK

       Now that you are sure that your jdk is not yet installed, double click on the downloaded file and start the installation by following the steps show by the installer.

       Update of the environment variable classpath

       Followed the step here at Oracle website ,

       Then install the java sun wireless toolkit

        Click on the .exe file of sun java wireless toolkit and after specify the path to install ( for us we install at C:\WTK2.5.2_01) and accepte to have a shortcut on your desktop, the installation will begin and you will see a window like the one below.

 

 

Sun java wireless toolkit installation

Sun java wireless toolkit installation

 

 

4.Set up of the Mobile application that show a Hello world message

            To start, launch the java wireless toolkit from your desktop, then it window will appear like this:

 

 java wireless toolkit  window at launch

      a-Set up of  the project that will content the source code of our Midlet

                          Click on the new project button beside the menu like below

 

 

how to create a new project

how to create a new project


    A windows will appear, type the project name and the Midlet className, like in the window below,  we choose

project name: HelloworldProject

Midlet class name: Helloworldmidlet

                       

project in creation

project in creation

     

Note: As in every java code java language is case sensitive so HelloWorldMidlet is different to Helloworldmidlet, so pay attention to the the case.

           Then another window called API selected will be show , for this project we have nothing to change there , just make sure that you the profil that is selected there, offer the most higher version of Midlet as Profile, and the CLDC as configuration, for this tutorials , we choose the MSA as target profiles that give us Midlet 2.1 as profile and the CLDC1.1 as configuration. This window look like below. So choose combination ( CLDC-1.1 MIDP-2.1) is valid for more than 75% of mobile devices released today (in your mobile device specifications you can find all of these) if it support Midlet implementation.

 

choice of MIDP profile and CLDC Configuration

choice of MIDP profile and CLDC Configuration

 

Then click on ok to validate and you would get this message in the sun java wirelee toolkit window:

 

project-created

project-created


      The message in the windows above, said that the project have been successfully created and the folder bin, lib, res, src have been created in the appropriate folder of the project and the project is already open at this time. You can even check if those folders actually exist :

Here is a small description of those folders:

       -Bin folder: Will content the executable file for your application(. Jar, .jad, Manifest) for a real deployment, but at this stade it content a .jad file and a MANIFEST only,

       -Lib folder: Will be where we would specify any extern library that we will use in our mobile application. It may be for ex: KXML librairy,…..

        -Src  folder: Will be where the source file will be store.

        -Res folder: Will content files like pictures that we want to include or use in our mobile application.

 

b-Create the source code file  

      Now open your textpad editor. Create a new file and start typing the content of your java file in. Because your class will inherit from a Midlet class, you must prior import the library that content the definition of the Midlet like this .

        import javax.microedition.midlet.*;

      Then you must declare your java class like this:


public class HelloWorldmidlet extends MIDlet {
    public void startApp() { }

    public void pauseApp() { }

    public void destroyApp(boolean unconditional) {
    } }


-HelloWorldmidlet : It is the class name and it is a public class,

-And it overrides 3 abstract methods :

  • startApp()  which is the starter procedure of the application; it is the first function to be executed, after that the HelloWorldmidelet  instance has been created;
  • pauseApp() – This procedure is executed at the occurrence of an event that involves blocking the MIDlet application; for example if your mobile device is launching this midlet and your same mobile device receives a phone call during the execution of the application; this function will be excecute putting the midlet in a break status.
  • destroyApp() – This procedure is used to close the application; it is executed at the end of the application and contains routines used to release resources; it acts like a destructor function;

      All those 3 procedures are the minimal procedures that a midlet class should have, but at this step of the source code the Helloworldmidlet has no any visual effects because, no source code is implemented in their procedures so.

     Now save the java file that is in your  textpad editor in the src directory situated in the project directory that has been created when you created your helloworldproject from the Wireless tool kit, The way to reach this src folder look like this:

~ \j2mewtk\2.5.2\apps\HelloworldProject

      save this file with the same name like your class name,do it like in this screen-shot below:

 

 

save-java-file

save-java-file


           Because we want our Helloworldmidlet  to be able to show a hello world message, the application must have access to the graphics resources controller’s. This is done by defining a Display object which is initialized at the start of the application. like this:


public class HelloWorldmidlet extends MIDlet
 {  
 //reference to the application display manager
 Display display;
 public void startApp( ) 
 {  
  display = Display.getDisplay(this);
 }
 public void pauseApp( ) { }
 public void destroyApp(boolean unconditional) { }
}

    Contrary to a java application on a computer, a java application on a mobile device has a restriction to be display within a single form or window at a time , and also the screen of mobile devices are smalls, so the Display reference is used to manage the visual ressource and to say what form is active at a time.

     So to display our hello world message,  we need a form and a container for the string, the library which containts class and methods to manage user interface is javax.microedition.lcdui

-We must import it first like this:  import  javax.microedition.lcdui.*;

-We  must declare the text box that we will use to show the hello world message:

         private TextBox tb;

-We must implemente the constructor of our class, by  instantiating the new textbox with his message


 public MidletHelloWorld( )
 tb = new TextBox("My First MIDlet","Hello World !",100,0);
 }

  -We must activate the display with our text box in , whithin our startApp( ) procedure like below.

 

 public void startApp( ){

 display=Display.getDisplay(this);
 display.setCurrent(tb);
 }

Now our entire Helloworldmidlet java file will be like below:

 

import javax.microedition.lcdui.*;import javax.microedition.midlet.*;public class Helloworldmidlet extendsMIDlet{Display display;PrivateTextBox tb;public Helloworldmidlet(){

tb = new TextBox(“My First MIDlet”,”Hello World !”,100,0);

}

public void startApp()

{

display=Display.getDisplay(this);

display.setCurrent(tb);

}

public void pauseApp() {

}

public void destroyApp(boolean unconditional) {

}

}


       Now we are going to test our First mobile application that show helloworld.

       Launch the wireless toolkit emulator, then open your project by hitting the open project button and go through the folders where your project have been saved , then select your project  name and hit openproject button like below:

 

 

open the java2me  project

open the java2me project


     You would get a screen like the screen-hot below:

project opened

project opened


       c- Packaging

To compile and preverify the project we must go to project menu , then take package then create package, like below:

 

 

packaging the  application

packaging the application

This would:
     -First compile the java source file into a byte code file by creating a Helloworldmidlet.class file

    -Then would preverify the code before running it, the aim of the preverification is to check any error in the source code, if the preverification run good, the packaging is done by:

    • Creating a file call MANIFEST.mf containing information like Midlet name, Midlet version,Midlet vendor.
    • Creating   a JAR file,  for our case it would be helloworldmidlet.jar that is the package containing the class and the MANIFEST file.
    • Creating a JAD file, for us it is Helloworldmidlet.jad this is the file that allow to install the application on the mobile device.

     If the packaging works well you would get a window like this :

 

 

compilation and preverification made with success

compilation and preverification made with success


     If not, It means that your java file has error and it would indicate the number of the line where the error is. If everything work well then we can run our application in the emulator;

      Go to project click on run. The emulator will open, at this time the application is not yet being executed, it shows a window with the name of your project like below:

 

 

mobile emulator launch

mobile emulator launch


     To start the application , you must select the Launch button (bottom right of the emulator) to run your  mobile application Then you would get on your mobile emulator screen. a Hello World message like below:

 

 

j2me application is running

j2me application is running showing hello world message


       We have done with our Hello world mobile application using J2ME.

To test a mobile application in a real environment, your personal mobile phone, the MIDlet application must be installed on your mobile device. We will talk about it in a next tutorial.

 

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

purchase viagra