Miniware logo
Articles / Introduction to J2ME programming

Introduction to J2ME programming

This article describes the steps that are needed in order to develop and deploy a really simple application for a java compatible mobile phone.

The tools that we are going to use is the free, open sourced and cross platform NetBeans IDE with mobility pack which can be obtained from http://www.netbeans.org

Download and install the IDE as well as the Mobility Pack. After installing it execute it.  From the File menu select New Project , from the left panel in the window that open select Mobile and on the right select Mobile Application, just like the picture below

new project

Press Next and select the desired name and project location (note that you have to remember that location) deselect the Set as main Project and Create Hello Midlet and press Finish


The main window of Netbeans will appear. In the left panel you can see you projects list and on the right you can see the source code of a selected class. Right click on your projects name on the left panel and choose New, File/Folder, in the new window that appears select from the left panel MIDP and on the right MIDlet, just as in the picture below

new file

Press Next  and and as a MIDlet Class Name enter HelloWorld.

Now on the right window you can see the source code of the class of your midlet. Modify the code to be as the following

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

public class HelloWorld extends MIDlet implements CommandListener {
    private Command exit;
    private TextBox tbox;

    public HelloWorld() {
        exit = new Command("Exit", Command.EXIT, 1);
        tbox = new TextBox("Message", "Hello World!", 25, 0);
        tbox.addCommand(exit);
        tbox.setCommandListener(this);
    }

    protected void startApp() {
        Display.getDisplay(this).setCurrent(tbox);
    }

    protected void pauseApp() {}
    protected void destroyApp(boolean bool) {}

    public void commandAction(Command cmd, Displayable disp) {
        if (cmd == exit) {
            destroyApp(false);
            notifyDestroyed();
        }
                              
  }
}

After modifying the code, right click on your projects name on the left panel and select Build. Now your project is ready to be deployed. If you want to test it before deploying it you can right click again on your projects name on the left panel and select run. An emulator will pop up and you will see the results of your code!

Now exit the IDE and browse the folder in which your project has been saved. There you will see four folders, named build, dist, src and nbproject. Open the dist folder and you will see two files, a .jad file and a .jar file. The /jar file is the actual application that needs to be deployed to the mobile phone.

Nokia hone owners have to install the Nokia PC suite (which is usually accompanies their mobile phone or it can be downloaded for free from Nokia's web site) and use the application installer in order to install the application on their mobile phone.

Sony Ericsson phone owners - and probably most of the other mobile phones brands - have simply to send the file to their mobile phone using bluetooth or infrared and the mobile phone will handle the file by itself.

The content of this page can be reproduced as long as the author and the source are mentioned. For questions please use the forum. Nikos Fotiou
Comments
(Post new comment)

2008-01-17 15:27:46 Dave wrote:
I'm using Sony Ericsson M600i. I Upload my Jar and it won't work. To make it work I need to deploy both JAR and JAD files to my phone, same directory. Why do I need to have the JAD as well. I'd like to distribute my midlets using the JAR file only. Second questoin: When I try the same thing on my Sony Ericsson T630....it won't accept either or. Neither the JAR nor both of them will work on T630. Any idea why?