This is a read-only archive. Find the latest Linux articles, documentation, and answers at the new Linux.com!

Linux.com

Feature: Programming

Writing J2ME applications in Linux

By Simos Xenitellis on December 18, 2007 (9:00:00 AM)

Share    Print    Comments   

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.

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

Simos Xenitellis is a software engineer and information security consultant who has been working on open source software for more than 10 years.

Share    Print    Comments   

Comments

on Writing J2ME applications in Linux

Note: Comments are owned by the poster. We are not responsible for their content.

Writing J2ME applications in Linux

Posted by: Anonymous [ip: 213.39.221.184] on December 18, 2007 03:10 PM
What you really want though are vendor specific toolkits/SDKs which is a different story regarding "ease of installation" (such as Nokia's).

#

Re: Writing J2ME applications in Linux

Posted by: Simos Xenitellis on December 19, 2007 02:11 PM
I think it's simpler to show users how to use a single tool for any Java-enabled phone. It could be material for another article to discuss how phone-specific tools can be made to work with Linux.

The purpose of the article is to get to run a midlet on your phone with the smallest investment and somewhat least complexity.

#

Writing J2ME applications in Linux

Posted by: Anonymous [ip: 220.226.19.64] on December 18, 2007 05:30 PM
How about using NetBeans which actually simplifies things a lot..

#

Re: Writing J2ME applications in Linux

Posted by: Simos Xenitellis on December 19, 2007 02:14 PM
NetBeans is a good IDE. However, I think that as an IDE, NetBeans would not make it clear to the user as to how the workflow looks like. The next step in programming could be to use something like NetBeans.


[Modified by: Simos Xenitellis on January 10, 2008 05:42 PM]

#

Writing J2ME applications in Linux

Posted by: Anonymous [ip: 144.15.240.8] on December 19, 2007 02:29 AM
Should paragraph 4 line 4 which reads "If you are running a 64-bit version of Linux...... you'll want to download and install the 32-bit (i586) JDK " actually read " you'll want to download and install the 64-bit (i586) JDK "?

#

Re: Writing J2ME applications in Linux

Posted by: Simos Xenitellis on December 19, 2007 02:08 PM
The WTK is currently available only as a 32-bit application. Therefore, if you have a 64-bit Linux system, you need to match the 32-bit WTK with a 32-bit JDK. You install the 32-bit JDK just for the purposes of running the 32-bit WTK.
If you try to use the WTK (32-bit) with the 64-bit JDK, you get some annoying errors; you cannot compile midlets due to the 64-bit JDK. If you install the 32-bit JDK package and enable with something like sudo update-alternatives, then you get the WTK happy to compile but you cannot run the emulator. One would need to switch back and forward. That's why it is good for now, since we only have a 32-bit WTK, to install a custom 32-bit JDK on our 64-bit systems.

#

Writing J2ME applications in Linux

Posted by: Anonymous [ip: 217.96.6.140] on December 20, 2007 10:37 AM
but how to install Motorola Studio on Linux, wine...? Motorola Studio build from Eclipse.

#

Writing J2ME applications in Linux

Posted by: Anonymous [ip: 72.184.173.215] on December 24, 2007 01:50 AM
Ugh, Java and *anything* ... a bad combination ....

#

Writing J2ME applications in Linux

Posted by: Anonymous [ip: 87.57.16.232] on March 11, 2008 04:16 PM
Had problem with core dump from ktoolbare "export LIBXCB_ALLOW_SLOPPY_LOCK=true" helped

#

This story has been archived. Comments can no longer be posted.



 
Tableless layout Validate XHTML 1.0 Strict Validate CSS Powered by Xaraya