Author:
Jonathan Knudsen, Dana Nourie
2, 12, 2002
Translation: Wang Jinfu Scale Code: Hellomidlet.java
This article contains all how you need to understand how to start developing in the Java2 micro version environment. You will learn how to install development tools, how to write your first J2ME app, how to compile it, and learn to test it on a simulator. You will create a MIDlet, which is an app running on the Mobile Information Device PROFILE, and the subset is an implementation of the J2ME specification. (To understand the background of wireless Java technology, see the introduction of wireless Java technology)
Most MIDlets have to connect some type of network service, so the second part of this tutorial describes how to build a servlet development environment and how to write, compile and test a servlet. The final step in this tutorial is to create a MIDlet and connect to the servlet.
You need to prepare
System requirements for a MIDP development environment depends on a simple choice: Do you use the FORTE [TM] for Java Public Edition? This is a free integrated development environment (IDE) that automatically completes many developments. Forte for Java has additional requirements for your processor, available memory, and disk space.
You will need:
166MHz or faster processor (if you use Forte for Java, you need 300MHz or higher) at least 64 megabulary (if it is Forte for Java, at least 128 megabytes) at least 30 megabytes available disk space (if ready Installing Forte for Java, at least 140 megabits)
The MIDP development tools have different versions of Linux, Solarise [TM] and Windows operating systems.
The development environment has the following three parts of software:
Java [TM] 2 Standard version of the development package version 1.3 or higher. J2ME Wireless Development Kit (J2MEWTK). This is a toolkit containing compilation and testing MIDlets. A text editor. It can be a text editing tool that is extremely simple in a notebook (Windows platform) or a text editing tool as Jedit.
Of course, what kind of editor is completely dependent on you. On the UNIX system, Emacs or VIs are popular choices. Some Windows developers use Notepad, but if you want to do a lot of development, you may want to adopt a more complex development tool. Jedit is a powerful editor running in a Java2 environment and on a different system, such as Windows2000 and Mac OS X work well. Forte for Java has its own editor.
J2ME Wireless Development Kit (J2MEWTK) can be used independently or as part of Forte for Java.
Install J2SE development package
No matter whether you use Forte for Java, you need a J2SE development package. (You will listen to the developer called JDK, or Java developer tool set, but it is now a J2SE development package) You can download the current version from http://java.sun.com/j2se/. The current version is 1.4.0. The J2SE development package has versions of different platforms such as Linux, Solaris and Windows.
How does the J2SE development package help you develop wireless applications? First, it provides a J2ME wireless development tool to Run the Java platform. Second, it contains a Java compiler and some other tools for J2MEWTK to build your project.
Once you have downloaded J2SE, you need to install it. In the Windows environment, run the file you just downloaded. The installation tool will ask you some questions and install J2SE. If you accept the default, J2SE will be installed in a directory of icon C: /JDK1.3.1 or C: /JDK1.3.1_01. You also want to modify the autoexec.bat file (Windows 95/98) or system properties (Windows NT / 2000) to add BIN subdirectory to your path. The following autoexec.bat file sessions show you how to add the bin sub-directory of the J2SE development package to your Windows98 directory: Path% PATH%; C: /JDK1.3.1/bin
To test if your installation is successful, open a command window, (if you have modified the autoexec.bat file, you need to restart your computer first) Enter the command java -version look at what will happen. The following is the output on my computer:
C: /> java -version
Java Version "1.3.1_01"
Java (TM) 2 Runtime Environment, Standard Edition (Build 1.3.1_01)
Java Hotspot (TM) Client VM (Build 1.3.1_01, Mixed Mode)
C: />
Install J2ME Wireless Development Kit
The next step is to install J2MEWTK - a toolset that simplifies development and testing the MIDP application. (You can think J2meWTK is a miniature integrated development environment; it automatically completes several tasks related to creating MIDP applications.)
Let's download J2MEWTK first from http://java.sun.com/products/j2mewtoolkit/. Then perform the installation file. Installation Tool will try to find your J2se SDK; if there is a problem, check and make sure you point it to your J2SE SDK installation directory. You also need to indicate that J2MEWTK is running independently or integrated into the Forte for Java. This article assumes that you will use J2MEWTK's independent operation mode. Unless you specify another directory, J2MEWTK's file will be installed under the directory C: / J2MEWTK, and the installation tool will create shortcuts to each of the different parts of the kit. (The weird behavior of letters in the Windows environment may display the directory name into J2mewtk; but this little exception will not bring problems.)
To run the kit separately, select the Ktoolbar shortcut. You will see the following interface.
Open interface of wireless development kits (click to look at big map)
J2MewTK works on the project, the last result of each project is a MIDlet kit. The development kit can only handle one project at a time. You can modify the properties of the project, compile the project and run this project on a device emulator. There are several sample items that have been installed on your machine with the development kit; we will see these examples later.
Let us start creating a new project now. Click the New Project button in the toolbar. J2mewtk will prompt you to enter the name of a MIDlet class in the project name and project. Enter hellosuite and hellomidlet as shown in the image below.
Create a new project
Once you enter the name of the project name and the first MIDlet, J2MewTK will give you an opportunity to modify the project properties. We first accept the default value; click the OK button to complete the creation of a new project. In the J2mewtk's text output box, you can see a number of useful messages displays the storage location of your project source file. On my machine, these news is as follows:
Creating Project "Hellosuite"
Place Java Source Files in
"c: / j2mewtk / apps / hellosuite / src"
Place Application Resource Files in
"C: / J2MewTK / Apps / Hellosuite / Res" Place Application Library Files in
"c: / j2mewtk / apps / hellosuite / lib"
J2MEWTK stores each item in a subdirectory in the apps directory. The name of the subdirectory is the name of the project. Here, J2MewTK has created a new directory, C: / J2mewtk / Apps / Hellosuite. Every project directory has a standard structure:
Project catalog structure
The bin directory contains a compiled MIDlet kit (one .jar file) and MIDlet suite descriptor (one .jad file). The lib directory is that you need to include the location of the other JAR files in your project. RES is the location of the resource file, such as images and text files, which are packaged with your MIDlet kit. Finally, the src directory is where you save the source code. The standard rules for packages and directories are equally applicable here; for example, the source code of class users.root will be stored in file src / users / root.java.
When you use KToolbar to compile projects, there are several additional directory being created:
Compiled project catalog structure
As you can see, J2mewtk created a ClassS, TMPClasses, and several directories of TMPLIB. Most of you can not consider these directories; they are used inside J2mewtk.
Create a MIDLET
In order to help you start MIDLET Development, let's write a simple MIDlet. Select a text editor, type, cut copies, or download the following code.
Import javax.microedition.lcdui. *;
Import javax.microedition.midlet. *;
Public class hellomidlet
Extends MIDlet
Implements commandListener {
Private form mmainform;
Public hellomidlet () {
Mmainform = New form ("Hellomidlet");
Mmainform.Append (New StringItem (Null, "Hello, MIDP!");
Mmainform.Addcommand (New Command ("EXIT", Command.exit, 0);
Mmainform.SetCommandListener (this);
}
Public void startapp () {
Display.getdisplay (this) .Setcurrent (mmainform);
}
Public void pauseapp () {}
Public void destroyApp (boolean unconditional) {}
Public void CommandAction (Command C, Displayable S) {
NotifyDestroyed ();
}
}
Save the above code in your project SRC directory to Hellomidlet.java. In my computer, the file is saved in c: /j2mewtk/apps/hellosuite/src/hellomidlet.java.
Next, click the Build button in the toolbar. J2mewtk will try to compile your project. If you have a compilation error, you will see them in the KToolbar's text output box. Correct compilation errors until the project is compiled successfully.
Now you can prepare to test your MIDlet kit. Click the RUN button. You will see there is a mobile simulator pop-up:
Hellosuite running on the simulator
The simulator displays a list of MIDlets included in the MIDET kit. This example only shows a MIDLET. Although the name shown here is Hellosuite, but the actual operation is Hellomidlet. To figure out where the mapping occurs, you can return to KToolbar and click the Settings ... button. Then click on the MIDlets page to see the MIDlet list included in this item. Back to the simulator, click the soft button under Launch to start the MIDlet. The simulator will display a simple screen. Click the EXIT button to close the MIDlet. Turn off the simulator window or click the ESCAPE button to end the simulator session.
Hellomidlet running on the simulator
The simulator you just run is called Defaultgrayphone. J2mewtk has other simulators, including a pager, a colorful phone and other simulator based on real devices such as Motorola I85S and RIM BlackBerry. Try to run the Hellomidlet on other devices to see what the user interface will become. (MIDP apps using the Palmos simulator can only be run after you have installed the Palm OS emulator, the simulator is called POSE, you can download another download from this drop-down list box from KToolbar, you need to select a simulator you like , Then click RUN again.
If you have tired of your hellomidlet, you may want to see several other items brought together by J2mewtk. You can open the Demos, Games, and Photoalbum sample items at will.
Details
Now, you have found some fun, let's go back to see what J2mewtk has done something for us. Although it doesn't have a complex work, it has completed several potentially dangerous steps for us and simplifies them into a simple buttons.
First, what happened when you clicked the build button? J2MewTK found all the .java files and compile them in the SRC directory of the project. This is not an ordinary compilation process, because these source files must be compiled in the MIDP environment rather than the J2EE environment. To understand this difference, consider using the java.lang.system class MIDlet. This class has a different API in J2SE and MIDP. When J2mewtk recompores your MIDlet class, you want it to use MIDP's java.lang.system instead of its J2SE version.
You can make your own choice by using the Javac command with the -bootclasspath parameter, but let J2mewtk consider these things easier for you.
Before running the MIDP class on the MIDP device, the MIDP class must also be pre-verified. You may remember that the J2SE has a byte code verifier to check them before the .class file is loaded. In the MIDP environment, verification is divided into two steps. The toolkit will perform an initial verification when compiling, then when the device is loaded, their running environment system performs a second verification.
You can use the command line tool preverify from others to perform the first step, but leave it to J2MewTK will be easier.
Finally, all MIDlet will be packaged into a MIDlet kit to publish it to a real device. This process also includes the class files, resource files of the MIDlet suite, and writes additional information into the JAR declaration file. Similarly, this cumbersome work is best to leave J2MEWTK. To pack your MIDlet kit, select Menu Project | Package. The system will generate .jad and .jar files for the MIDlet suite in the bin directory of the project.
Good play is still behind
You have now mastered the initial MIDP development knowledge and has software that develops your own MIDlet kit. Although this is already great, the MIDP client programming is just half of our beautiful drawings. In addition to some exceptions, most interesting MIDlets are MIDlets that connect some web services. In the second part of this article, you will learn how to install, configure, and run a server environment. You will write a Java Small Server (servlet) and modify the MIDlet described above so that it can be connected to this Java small service program. At that time, the whole world is really belonging to you. related resources
The User Guidance Manual included with J2Mewtk provides a useful information about the application development cycle, MIDlet property, each installation directory, device type, and portability. It also contains guidance on configuring the simulator and uses the wireless development kit and uses the development package in the Forte for Java public version.
If you have just started learning MIDP programming, you may find that the following article will help you:
Wireless development technology introduces the use of MID protocol subsets for wireless programming all MIDP articles
About the author: Jonathan Knudsen [e-mail] [home page] is the author of several books, including Wireless Java, The Unofficial Guide to LEGO MINDSTORMS Robots, Learning Java, and Java 2D Graphics Jonathan especially in Java and Lego robots made a lot of respect. Copular, including a online monthly column called Bite-Size Java and a article written for JavaWorld, Exe, NZZ Folio, and The O'Reilly Network. Jonathan has a mechanical engineering degree from Princeton University.
Dana Nourie is a JDC Web developer and writer. She likes to study embedded Java technology such as J2ME and develop Internet applications using JDBC, Servlet and JavaServer Pages [TM] technology. In addition, she has added to the Java newbie program center and welcomes the growth of developers to share thoughts and comments.