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.
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 the Series 40 SDK.
For Symbian devices, select S60Emulator.
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 S60Emulator.
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 content 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 content 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 Mobile Tools for Java > Create Package.
Eclipse builds the MIDlet and creates the JAR and JAD files used for deploying the MIDlet to a device. You can find the files in the Package Explorer pane under deployed.
If the MIDlet configurations include multiple platforms (step 3), Eclipse prompts you whether to package the MIDlet only for the selected ("active") platform or for all listed platforms. You can choose either option.
Figure: HelloWorld project viewed in the Package Explorer pane
The MIDlet has been created. You can now deploy it to a device.