December 18, 2007

Writing J2ME applications in Linux

Author: Simos Xenitellis

If you want to make the most out of your Java-enabled handheld device, you can write Java 2 Micro Edition (J2ME) applications (also called midlets) on Linux and run them on your mobile platform. Here's how easy it is to get started; we'll write a HelloWorld application you can run on your cell phone.

Most cell phones allow you to install and run Java applications; to verify if your phone is supported, consult your documentation or search for the specs at the GSMArena Web site.

To get started, you need to have the Java Development Kit (JDK) and the Sun Java Wireless Toolkit (WTK) installed on your system. For example purposes we'll use a Ubuntu Linux system; the instructions for other distributions should be similar.

Installing the JDK is straightforward, as it has been open source for several months, and should be already available in your distribution's repository. For Ubuntu, the package is sun-java6-jdk. If you are running a 64-bit version of Linux, there is a small complication -- the current WTK has been compiled for a 32-bit version of Linux, so you'll want to download and install the 32-bit (i586) JDK as provided at the Sun Java Standard Edition (SE) Downloads page. You can install the JDK manually under /usr/java/. If you're not sure whehter you are actually running a 64-bit version of Linux, type uname -m and look for 64.

Follow the Web site's instructions to install the WTK, which contains the necessary files to compile applications for the J2ME platform, and also has an emulator so that you can test your programs without having to install them repeatedly on your cell phone. For this article we'll install the WTK in /usr/local/WTK2.5.2/. At the end of the installation, the installer shows the full path to the project management tool of WTK, called ktoolbar. This tool has a graphical user interface (GUI) to help create projects, configure settings, and compile and create packages for distribution.

Contrary to what the name may suggest, ktoolbar is a Java application. The WTK installer does not place an entry in the Applications menu of your distribution; to do so, right-click on Applications, then click on Edit Menu.... Find the Programming submenu and add a new menu item in it. Use Sun Java Wireless Toolkit for the name and specify the full path for the ktoolbar command the installer reported. You can also pick a custom icon, such as /usr/local/WTK2.5.2/wtklib/images/Midlets.png.

122050-thumb.png

You can test the installation of WTK by running one of the demos inside the emulator. Click to open an existing project in WTK and select the Games sample. Click Build, then Run. The phone emulator should appear, and you can run the application in the virtual phone.

When opening one of the existing sample projects, the WTK copies the project files into your home directory under the ~/j2mewtk/2.5.2/apps/ subdirectory. That is your workspace, and the WTK assumes that you will be using your own text editor or environment to edit the source code. The WTK does not provide a development environment; it provides a building environment.

Writing HelloWorld

Start the WTK and create a new project. For both Project Name and MIDlet Class Name type HelloWorld, and click to create the project. In the next dialog, choose the target platform. The default option, MSA, defines the profile MIDP 2.1, which is common to very recent phones only. If your phone does not support MIDP 2.1, it will not be able to run such a midlet, so you should select Custom for the target platform and choose a profile that your phone supports. I checked the specification page for my cell phone at GSMArena, and therefore chose the MIDP 2.0 profile. It is important to get this right, because quite often, if you get it wrong, the phone simply reports the midlet is invalid without further explanation. Keep the default settings for the rest of the options.

WTK creates the project skeleton in ~/j2mewtk/2.5.2/apps/HelloWorld/. To build the project, you need to put the source code file in the src/ subdirectory. Create the file src/HelloWorld.java with the following code:

    // Sample adapted from http://developers.sun.com/mobility/midp/articles/wtoolkit/
    import javax.microedition.lcdui.*;
    import javax.microedition.midlet.*;
    public class HelloWorld
    extends MIDlet 
    implements CommandListener 
    {
    private Form mMainForm;
    public HelloWorld() 
    {
    mMainForm = new Form("HelloWorld");
    mMainForm.append(new StringItem(null, "Hello, World!"));
    mMainForm.addCommand(new Command("Exit", Command.EXIT, 0));
    mMainForm.setCommandListener(this);
    }
    public void startApp() 
    { Display.getDisplay(this).setCurrent(mMainForm); }
    public void pauseApp() {}
    public void destroyApp(boolean unconditional) {}
    public void commandAction(Command c, Displayable s) 
    { notifyDestroyed(); }
    }
    

Return to the WTK, click Build, and then Run. In the emulator, click on the button to launch the midlet. You should see the string Hello, World! on the cell phone emulator's screen.

Adding polish

To assign a program icon to your midlet when you view it on your cell phone, you do not need to modify the code. In the Project settings, select the Midlets tab and edit the entry. Change the Icon path from the default HelloWorld.png to /images/HelloWorld.png. Use the GIMP to create a PNG image (indexed, 256 colors, 12x12 pixels should be OK) and save it as res/images/HelloWorld.png. If you rebuild and rerun the midlet, the icon should appear before the midlet name.

Before you upload the mildet to the phone, you must put it in a package. Click on Project -> Package -> Create Package. WTK creates the files bin/HelloWorld.jar and bin/HelloWorld.jad, which you can give to people to upload to their devices.

If both your computer and phone support Bluetooth, you can upload the midlet through Bluetooth. In Ubuntu, the package gnome-vfs-obexftp provides Bluetooth integration with Nautilus, so you can view your phone filesystem in Nautilus. Right-click on the Bluetooth icon on the panel and select Browse Device... to open a Nautilus window showing the filesystem of your phone. You can copy the .jar file to the application folder, then run the midlet from your phone. You may need to associate your phone with your computer first so that a tranfer can take place; see your phone documentation for more.

If you can't install the midlet directly to your phone (because it lacks Bluetooth, or you have no USB cable for direct connection), you can place the relevant files on your Web site and use your phone's browser to download and install from the Internet. You need to put both files (.jar and .jad) on your Web server, and use the URL to the .jad file from your phone in order to install the application. For example, you can download this application from http://simos.info/j2me/HelloWorld.jad.

Further work

This tutorial should get you through the basic hurdles of creating and installing midlets on Linux handheld devices. Once you mastered the basics, you can develop more applications, such as midlets that communicate with other Bluetooth devices or connect to Web sites to retrieve information. An example of such an interesting program is BluePad, an application that makes your Bluetooth phone act as a remote control with which you can control your Linux desktop wirelessly.

Further reading

Categories:

  • Programming
  • Java
  • Handheld Devices