After setting
up the development environment and configuring Eclipse, you can start creating the Hello World MIDlet. The MIDlet consists
of two classes: HelloWorldMIDlet
, which is the main
class and defines the basic functionality of the MIDlet, and HelloScreen
, which defines the HelloScreen
object used by the main class to display the text "Hello, world!"
on the device screen.
To create the Hello World MIDlet:
Select File > New > MIDlet Project.
Tip: If you do not see MIDlet Project listed in the menu, select File > New > Other instead, select Java ME > MIDlet Project, and click Next.
In the Project name field, enter "HelloWorld".
In the Configurations section, select the device platform for which you want to create the MIDlet:
For Series 40 devices, select a Nokia SDK for Java.
For Symbian devices, select SymbianEmulator.
If the platform you want to use is not listed:
Next to the Configurations section, click Add.
In the SDK drop-down menu, select the SDK you want to use.
In the Device drop-down menu, select the device definition you want to use as the platform. For a Symbian device, select SymbianEmulator.
Click Finish.
Now that you have added the platform to the Configurations section, select it.
Click Next.
Figure: Creating a new MIDlet project
In the MIDlet Name field, enter "HelloWorld".
In the MIDlet Vendor field, enter your name.
In the Microedition Configuration drop-down menu, select Connected Limited Device Configuration (1.1).
In the Microedition Profile drop-down menu, select Mobile Information Device Profile (2.1).
Click Finish. Eclipse sets up the MIDlet project.
Figure: Configuring the MIDlet project
If prompted, click Yes to open the Java ME perspective.
To create the main class for the MIDlet, select File > New > Java ME MIDlet.
In the Source folder field, make sure that the value reads "HelloWorld/src".
In the Name field, enter "HelloWorldMIDlet".
Click Finish. The HelloWorldMIDlet
class is created in the default
package.
Figure: Creating the MIDlet main class
Delete the contents
of the HelloWorldMIDlet
class and paste in the following
source code:
import javax.microedition.midlet.*; import javax.microedition.lcdui.*; public class HelloWorldMIDlet extends MIDlet { public HelloWorldMIDlet() { } // Sets the MIDlet's current Display to a HelloScreen object. public void startApp() { Displayable current = Display.getDisplay(this).getCurrent(); if (current == null) { HelloScreen helloScreen = new HelloScreen(this, "Hello, world!"); Display.getDisplay(this).setCurrent(helloScreen); } } public void pauseApp() { } public void destroyApp(boolean unconditional) { } }
To create the HelloScreen
class, select File > New > Class.
In the Source folder field, make sure that the value reads "HelloWorld/src".
In the Name field, enter "HelloScreen".
Click Finish. The HelloScreen
class is created in the default
package.
Figure: Creating the HelloScreen class
Delete the contents
of the HelloScreen
class and paste in the following
source code:
import javax.microedition.lcdui.*; class HelloScreen extends Form implements CommandListener { private final HelloWorldMIDlet midlet; private final Command exitCommand; // Exit command for closing the MIDlet in the device UI. public HelloScreen(HelloWorldMIDlet midlet, String string) { super(""); StringItem helloText = new StringItem("", string); super.append(helloText); this.midlet = midlet; exitCommand = new Command("Exit", Command.EXIT, 1); addCommand(exitCommand); setCommandListener(this); } public void commandAction(Command command, Displayable displayable) { if (command == exitCommand) { midlet.notifyDestroyed(); } } }
Save the project by selecting File > Save All.
In the Package Explorer pane, right-click HelloWorld and select Export.
Select Java ME > Export MIDlet Package and click Next.
If the MIDlet configuration includes multiple device platforms, check the platforms for which you want to package the MIDlet.
In the Destination Directory section, check Use deployment directory. This instructs Eclipse to create the deployable MIDlet packages in the default deployment folder under the project folder.
Tip: If you want to use a different folder than the default folder, enter the path of the folder in the Directory field, or click Browse and select the folder. Note that you can only use a folder that already exists.
Figure: Exporting the MIDlet to the default deployment folder
Click Finish.
Eclipse builds the MIDlet and creates the JAR and JAD files used for deploying the MIDlet to a device. If you selected the default deployment folder, you can find the files in the Package Explorer pane under the corresponding folder.
Figure: HelloWorld project viewed in the Package Explorer pane
The MIDlet has been created. You can now deploy it to a device.