Developing J2Me Applications With Eclipseme: Section 1. Before You Start
Developing J2Me Applications With Eclipseme: Section 1. Before You Start
Developing J2Me Applications With Eclipseme: Section 1. Before You Start
Skill Level: Introductory Michael Juntao Yuan Writer, Analyst, and Developer
30 Nov 2004 This tutorial demonstrates how to develop J2ME applications using the Eclipse IDE and the open source EclipseME plug-in.
Prerequisites
To complete the tasks and run the sample code in this tutorial, you must have the Eclipse IDE Version 2.1.x or 3.x installed. You also need to download and install the free J2ME Wireless Toolkit from Sun Microsystems (http://java.sun.com/products/j2mewtoolkit/).
Section 2. Introduction
Developing J2ME applications with EclipseME Copyright IBM Corporation 1994, 2008. All rights reserved.
Page 1 of 41
developerWorks
ibm.com/developerWorks
What is J2ME?
Java 2 Platform, Micro Edition (J2ME) is the Java platform that runs on small mobile devices. It supports the standard Java programming language and contains a subset of APIs from the Java 2 Platform, Standard Edition (J2SE) with the addition of device-specific APIs. The Mobile Information Device Profile (MIDP) is a flavor of J2ME that runs on mobile phones. Available on more than 250 million handsets, the MIDP is the most pervasive computing platform available today. In this tutorial, you focus on MIDP application development. For more information about J2ME and MIDP, see the J2ME 101 series of tutorials published by developerWorks (see Resources).
ibm.com/developerWorks
developerWorks
editors, refactoring, unit testing, debugging, and project management. Leading Java IDEs, such as Borland JBuilder and Sun Java Studio, try to attract J2ME developers by supporting integration with the WTK. However, even as the very popular open source IDE, Eclipse does not integrate with the WTK out of the box. This has been a source of frustration for both J2ME developers who want to leverage the powerful Eclipse IDE and Eclipse developers who want to get into device development. Fortunately, the Eclipse IDE is designed to be extendable. The EclipseME is an open source plug-in that enables the integration between Eclipse and the WTK.
Developing J2ME applications with EclipseME Copyright IBM Corporation 1994, 2008. All rights reserved.
Page 3 of 41
developerWorks
ibm.com/developerWorks
ibm.com/developerWorks
developerWorks
Select the path where the WTK is installed on your computer. Figure 3. Select the WTK path
Developing J2ME applications with EclipseME Copyright IBM Corporation 1994, 2008. All rights reserved.
Page 5 of 41
developerWorks
ibm.com/developerWorks
After the WTK is integrated, the available J2ME configurations, profiles, and libraries are shown in the window. Figure 4. WTK integration
ibm.com/developerWorks
developerWorks
Note that SDKs from device vendors, such as Nokia and Sony Ericsson, can also be added here. Additionally, you can add a custom platform profile by combining MIDP standard libraries with custom APIs stored in third-party JAR files. This way, it's easy to add any device-proprietary APIs in the development environment.
Developing J2ME applications with EclipseME Copyright IBM Corporation 1994, 2008. All rights reserved.
Page 7 of 41
developerWorks
ibm.com/developerWorks
ibm.com/developerWorks
developerWorks
Now, you have installed and configured the EclipseME tools. Let's go through the entire process of building, testing, and packaging an MIDP application using those tools.
Developing J2ME applications with EclipseME Copyright IBM Corporation 1994, 2008. All rights reserved.
Page 9 of 41
developerWorks
ibm.com/developerWorks
Specify a name and a root directory for the project. Figure 8. Project name
ibm.com/developerWorks
developerWorks
Then, choose a platform to develop against, if multiple WTKs and system libraries are available. The WTK provides MIDP 1.0 and MIDP 2.0 profiles. Figure 9. Choose a platform
Developing J2ME applications with EclipseME Copyright IBM Corporation 1994, 2008. All rights reserved.
Page 11 of 41
developerWorks
ibm.com/developerWorks
ibm.com/developerWorks
developerWorks
By default, the project's classpath includes the J2ME API libraries in the WTK. You can add any external libraries into the classpath. However, if you add external JAR libraries to the project that are not available on the physical device, you will have to package them into the distribution JAR file manually. Otherwise, the MIDlet throws a Class not found exception when you deploy it on the device.
Developing J2ME applications with EclipseME Copyright IBM Corporation 1994, 2008. All rights reserved.
Page 13 of 41
developerWorks
ibm.com/developerWorks
ibm.com/developerWorks
developerWorks
It automatically inherits from the javax.microedition.midlet.MIDlet class. In this example, the MIDlet implements the CommandListener interface to handle user input events. Figure 13. New MIDlet
Developing J2ME applications with EclipseME Copyright IBM Corporation 1994, 2008. All rights reserved.
Page 15 of 41
developerWorks
ibm.com/developerWorks
The skeleton Java source code file generated from the wizard is as follows in Figure 14. Figure 14. Generated source code
ibm.com/developerWorks
developerWorks
Developing J2ME applications with EclipseME Copyright IBM Corporation 1994, 2008. All rights reserved.
Page 17 of 41
developerWorks
ibm.com/developerWorks
implements CommandListener { Display display; Command greetCommand; Command exitCommand; Command clearCommand; Command backCommand; WelcomeScreen welcomeScreen; HelloScreen helloScreen; // instantiate the internal variables public TutorialMidlet () { display = Display.getDisplay(this); greetCommand = new Command ("Greet", Command.OK, 0); exitCommand = new Command ("Exit", Command.EXIT, 0); clearCommand = new Command ("Clear", Command.CANCEL, 1); backCommand = new Command ("Back", Command.SCREEN, 1); welcomeScreen = new WelcomeScreen (); welcomeScreen.addCommand (greetCommand); welcomeScreen.addCommand (clearCommand); welcomeScreen.setCommandListener (this); helloScreen = new HelloScreen (); helloScreen.addCommand (exitCommand); helloScreen.addCommand (backCommand); helloScreen.setCommandListener (this); } // Called when the MIDlet is started by the AMS protected void startApp () { display.setCurrent (welcomeScreen); } protected void pauseApp () { // Do nothing } protected void destroyApp (boolean unconditional) { notifyDestroyed (); } public void commandAction (Command c, Displayable d) { if (c == greetCommand) { String name = welcomeScreen.getName (); helloScreen.setName(name); display.setCurrent (helloScreen); } else if (c == clearCommand) { welcomeScreen.setName(""); display.setCurrent(welcomeScreen); } else if (c == backCommand) { display.setCurrent (welcomeScreen); } else if (c == exitCommand) { destroyApp (true); } } }
ibm.com/developerWorks
developerWorks
you to this inconsistency by flagging the relevant parts of the code with red bars. If you place the mouse over the red line, the editor pops up with an explanation box informing you why it thinks it detects an error. Realtime syntax checking allows developers to take advantage of the consistency checking capabilities of the Java compiler without actually waiting for the compiling to complete. Figure 15. Real-time error checking
If the "Build automatically" option in Eclipse's "Project" menu is selected, Eclipse tries to continuously build the project in the background whenever the project is updated. In this case, the Package Explorer also shows the compiling errors detected in the building process. s Figure 16. Automatic build errors
Developing J2ME applications with EclipseME Copyright IBM Corporation 1994, 2008. All rights reserved.
Page 19 of 41
developerWorks
ibm.com/developerWorks
ibm.com/developerWorks
developerWorks
As you have seen, the soft key commands and event handlers of the WelcomeScreen are added in the TutorialMidlet class. The data in the nameField UI component can be accessed from outside this class using Java Beans style getter and setter methods. The image used on the form is from an external PNG image file, welcome.png. The file must reside in the midlet's runtime classpath in order to be accessible.
package tutorial; import javax.microedition.lcdui.*; public class WelcomeScreen extends Form {
Developing J2ME applications with EclipseME Copyright IBM Corporation 1994, 2008. All rights reserved.
Page 21 of 41
developerWorks
ibm.com/developerWorks
private TextField nameField; public WelcomeScreen () { super ("Welcome"); Image img; // Construct the image from the media file try { img = Image.createImage("/welcome.png"); } catch (Exception e) { e.printStackTrace (); img = null; } ImageItem imageItem = new ImageItem ("", img, ImageItem.LAYOUT_CENTER, "Welcome"); nameField = new TextField ("Please enter your name", "", 10, TextField.ANY); append (imageItem); append (nameField); } public void setName (String n) { nameField.setString (n); } public String getName () { return nameField.getString (); } }
ibm.com/developerWorks
developerWorks
img = null; } } public void setName (String n) { name = n; } // Paint the screen based on the name protected void paint (Graphics g) { g.setColor(0xffffff); g.fillRect(0, 0, width, height); g.setColor(0x000000); g.drawImage (img, width / 2, height / 4, Graphics.VCENTER | Graphics.HCENTER); g.setFont(Font.getFont( Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_LARGE)); g.drawString (name, width / 2, height * 3/4, Graphics.BASELINE | Graphics.HCENTER); } }
No more errors
Now, with all the source code typed in, Eclipse shows no errors or inconsistencies anymore. You are ready to run the midlet! Figure 18. No more errors
Developing J2ME applications with EclipseME Copyright IBM Corporation 1994, 2008. All rights reserved.
Page 23 of 41
developerWorks
ibm.com/developerWorks
ibm.com/developerWorks
developerWorks
Developing J2ME applications with EclipseME Copyright IBM Corporation 1994, 2008. All rights reserved.
Page 25 of 41
developerWorks
ibm.com/developerWorks
Emulator in action
The WTK emulator is started, and you can test the MIDlet. Figure 21. Emulator
ibm.com/developerWorks
developerWorks
Developing J2ME applications with EclipseME Copyright IBM Corporation 1994, 2008. All rights reserved.
Page 27 of 41
developerWorks
ibm.com/developerWorks
ibm.com/developerWorks
developerWorks
Developing J2ME applications with EclipseME Copyright IBM Corporation 1994, 2008. All rights reserved.
Page 29 of 41
developerWorks
ibm.com/developerWorks
ibm.com/developerWorks
developerWorks
The emulator would otherwise start normally, but when you hit the break point (in this case, by pressing a soft key to invoke the command handler commandAction() method), Eclipse changes its display from the Java perspective to the debugging perspective. It displays the call stack, the current break point, and the values of the variables in the stack. After reviewing that data, you can resume execution using the "Run" menu. Figure 25. The debugging perspective
Developing J2ME applications with EclipseME Copyright IBM Corporation 1994, 2008. All rights reserved.
Page 31 of 41
developerWorks
ibm.com/developerWorks
ibm.com/developerWorks
developerWorks
If you have multiple MIDlets in the project, you can select which MIDlet classes to include in the MIDlet suite defined in this JAD file. Figure 27. MIDlet attributes
Developing J2ME applications with EclipseME Copyright IBM Corporation 1994, 2008. All rights reserved.
Page 33 of 41
developerWorks
ibm.com/developerWorks
You can also specify attributes that provide optional information about the MIDlet suite. Figure 28. Optional attributes
ibm.com/developerWorks
developerWorks
The over-the-air (OTA) properties can be used to send notifications back to the deployment server when the MIDlet suite is installed or deleted. Figure 29. OTA attributes
Developing J2ME applications with EclipseME Copyright IBM Corporation 1994, 2008. All rights reserved.
Page 35 of 41
developerWorks
ibm.com/developerWorks
The user-defined attributes can make deploy-time information available to the MIDlet at runtime. (We do not have any user-defined attributes here.) Figure 30. User defined attributes
ibm.com/developerWorks
developerWorks
Developing J2ME applications with EclipseME Copyright IBM Corporation 1994, 2008. All rights reserved.
Page 37 of 41
developerWorks
ibm.com/developerWorks
The JAR file and a copy of the JAD file are placed in the "deployed" directory as you have configured. Now you are ready to copy those files to an OTA server or deploy them directly to devices through local connections. Figure 32. The deployment package
ibm.com/developerWorks
developerWorks
Developing J2ME applications with EclipseME Copyright IBM Corporation 1994, 2008. All rights reserved.
Page 39 of 41
developerWorks
ibm.com/developerWorks
Downloads
Description
Code sample Information about download methods
Name
Size
Download method
HTTP
wi-nokiasource.zip KB 28
ibm.com/developerWorks
developerWorks
Resources
You can download the Eclipse IDE from the Eclipse Web site. The site also has documentation on how to use the IDE and how to develop plug-ins. The Sun J2ME Wireless Toolkit is freely available for download. The EclipseME project is an open source Eclipse plug-in that supports J2ME/MIDP development. The J2ME 101 series of tutorials on IBM developerWorks is a good place to get started with J2ME and MIDP programming: "J2ME 101, Part 1: Introduction to MIDP's high-level user interface" (developerWorks, November 2003). "J2ME 101, Part 2: Introduction to MIDP's low-level UI" (developerWorks, December 2003). "J2ME 101, Part 3: Inside the Record Management System" (developerWorks, December 2003). "J2ME 101, Part 4: The Generic Connection Framework" (developerWorks, January 2004).
Developing J2ME applications with EclipseME Copyright IBM Corporation 1994, 2008. All rights reserved.
Page 41 of 41