Sunday, October 2, 2011

J2ME development environment (Eclipse, J2ME, EclipseME, Netbeans)


Purpose

This page describes the installation, configuration of a J2ME development environment.

Overview

  1. Required Software
  2. Installation
  3. Configuration
  4. First J2ME project
  5. Deployment
  6. JAD test
  7. Mobile test
  8. Netbeans 4.1
  9. Links
  10. HelloWorld code

Step 1: required software

The following software will be used:

Step 2: Installation

  • Install J2SDK as usual /rpm ...)
  • Install Eclipse as usual (un-tar to /opt)
  • Install J2ME Wireless Toolkit
    The JWT comes as executable. Make it executable (chmod +x) and start it. Agree to licenses, set path to j2sdk bin-directory, enter path to install directory.
  • Install EclipseME plugin: Beginning with version 0.5.5, EclipseME is provided as an Eclipse "archive site.". Follow the instruction found here.

Step 3: Configuration

Check that Eclipse->Windows-Preferences contains "J2ME" entry.
Choose platform components, right click and select "Wireless Toolkits".
Select the path to your WTK directory. Check that J2ME WTK, pltform definitions, profiles and configurations have been added.
Check that WTK works: start /usr/java/WTK2.2/bin/ktoolbar, load some sample projects and 'run' them.

Step 4: Project

Select File->New->Project. Select "show all wizards". Choose "J2ME Midlet Suite". Set path and project name.
One the project, select New->Other, (show all wizards) and select "J2ME Midlet". Give it a name (HelloWorld).

Copy and paste the HelloWorld code into your class.
Compile it, choose "Run as-> Emulated J2ME Midlet".
Congratulation, your first Midlet is running.



Step 5: Deployment

Modify JAD file entries: double click on jad file, select midlets tab, add middlet. Give your middlet a name and select the class ("HelloWorld", selection may be broken, so just type the name of the class).

Right click on the project, select J2ME->create package.
A new sub-directory (deployed) will appear. It contains a jar files (which contains your classes) and a jad file (which contains the description of your midlet).

ProGuard
Alternatively you can also use ProGuard, a java class file shrinker and obfuscator. Using it, class files will be smaller (important for J2ME devices, the example HelloWorld.class shrinks for 12k to 10k) and harder to reverse-engineer.
Download and unzip ProGuard. In Eclipse, go to Window->Preferences->J2ME. Set the proguard root directory.
Now, instead of creating a package, right click the project, select J2ME->"create obfuscated packages".

Step 6: JAD test

Simulator
Call $WTK_HOME/bin/runmidlet [jad-file].
  • $WTK_HOME is the directory in which you have installed Wireless Toolkit
  • jad file ist the deployed jad file, should be in [project_dir]/deployed

The simualtor should open and you can select the midlet (Hello World) that you want to execute.

Step 7: Mobile test

First you need a web server. Just upload your deployed jad and jar file into a directory of the web server.
The web server must know the mime type of jad and jar files. For this, if you have control over the web server, add the following lines to apache httpd.conf file:
addtype application/java-archive jar

addtype text/vnd.sun.j2me.app-descriptor jad 
      
If you are like me and use a foreign web server, than add a .htaccess file which contains the above 2 files to the directory in which are the jad/jar files.
On your mobile, open the adress of your midlet.jad file: http://server/dir/[jad-file].

Netbeans 4.1

Now (Mai 2005) that Netbeans 4.1 is out, I can recommend you to use it as J2ME development platform instead of writing everything by yourself (which of course is good to know the basics).
I will not write a new tutorial for Netbeans J2ME development, as there are some good tutorials out there.
Some screenshots from my first tests (click for larger images):
netbeans midp screenshot
netbeans midp screenshot 2

Links and further information


HelloWorld Code

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

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

    public HelloWorld() {
        exitCommand = new Command("Exit", Command.EXIT, 1);
        tbox = new TextBox("Hello world MIDlet", "Hello World!", 25, 0);
        tbox.addCommand(exitCommand);
        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 == exitCommand) {
            destroyApp(false);
            notifyDestroyed();
        }
    }
}

No comments: