Thursday, March 1, 2007

RE: Hello World! [Eclipse Rocks!]

Using the EclipseME plugin I was easily able to build the proverbial "Hello World!" application :) In addition to the development setup I mentioned earlier, I also needed a little bit of configuration before I could start coding, which I will detail below.

Steps to configure the EclipseME plugin:
  1. Got to "Window > Preferences > J2ME".
  2. Set the WTK Root to the root folder of your wireless toolkit installation.
  3. Under J2ME, select "Device Management".
  4. In this tab, click on "Import".
  5. Specify the wireless toolkit root folder again in the "Specify search directory" text box.
  6. Now, click "Refresh" which searches the directory for installed devices. (I'm using the Sun Wireless Toolkit 2.5)
  7. Import all the devices in the list & click "Finish".
  8. Select a default device and click "OK".
And that's it :) We are done!!

Now, make a new J2ME Midlet Suite whose JAD file should be changed to make CLDC version equal to 1.0 and the MIDP version to 2.0. Then make a J2ME Midlet which I named "HelloWorld", containing the following code:

import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.TextBox;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;


public class HelloWorld extends MIDlet {

private Display display = null;
private TextBox textBox = null;

public HelloWorld() {
}

protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
}

protected void pauseApp() {
}

protected void startApp() throws MIDletStateChangeException {
display = Display.getDisplay(this);
textBox = new TextBox("Simple", "Hello World!", 100, 0);
display.setCurrent(textBox);
}
}

Right click on your project, go to "J2ME > Create Package". This will create a "deployed" folder which contains a .jad and .jar file.

You could now copy these files to your phone and then open the JAD file via your phone. It should launch the "HelloWorld!" app! :)

You can also make a new "Run" configuration to execute the jar file from the Sun Wireless Toolkit emulator. This can be done from the "Run > Run" menu by making a new configuration under "Wireless Toolkit Emulator". Once created you can launch it easily!

Some points to note:
  1. For the Emulator configuration choose the "Midlet" form of executable rather than the "Over the Air" option.
  2. When you make a new J2ME Midlet Suite, in the screen for Java Settings, if you see a "J2ME Library (failed to get library information)" do not worry cause once you click finish, that particular problem goes away.

2 comments:

Cristina said...

When you make a new J2ME Midlet Suite, in the screen for Java Settings, if you see a "J2ME Library (failed to get library information)" do not worry cause once you click finish, that particular problem DOESN'T GO away

Hi i am a Spanish java learner. Any idea of why it doesn't it work?

email: estono

Eva said...

There few things you need to do:
1. Make sure you install Sun WTK.
2. In Eclipse, you need to configure the Device Management (under Window-->Preference) using WTK.
3. Chose a device from the WTK device list (under Project-->Properties J2ME tag).