Writing MIDlets is an example of cross-compiling, where you compile code on one platform and run it on another. In this case, you'll be compiling a MIDlet using J2SE on your desktop computer. The MIDlet itself will run on a mobile phone, pager, or other mobile information device that supports the MIDP.
Normally, when you're compiling source code, the CLASSPATH environment variable points to all the classes that your source code needs to know about. When you use javac to compile a file, there are some implied APIs that get included, like the classes in java.lang. With MIDlets, however, the situation is a little more complicated. Say that you use the java.lang.System class in your MIDlet. How do you let the compiler know that you want to use the MIDP version of this class, not the J2SE version?
The answer is a command line option, -bootclasspath. This option lets you point to a classpath that describes the fundamental APIs against which you will be compiling your source code. In our case, this option should be used to specify the classes directory in the MIDP reference implementation installation. On the command line, it looks like this:
javac -bootclasspath \midp\classes Jargoneer.java
(You will need to adjust the path to classes if you installed the MIDP software in a different location.)
Try this at the command line. The compiler will attempt to compile your source code, referencing the MIDP classes as needed. Fix up any errors you might have until it compiles.
If you're using the J2ME Wireless Toolkit, you don't have to worry about the details of -bootclasspath. Start up the toolkit, called KToolbar. Choose New Project... from the toolbar, and create a new project. J2MEWTK asks you for the name of the project and the MIDlet class name; use "Jargoneer" for both. J2MEWTK represents projects as subdirectories of the apps directory. The following diagram shows the contents of the Jargoneer directory after the new project is created.
<J2MEWTK directory> apps example Jargoneer bin classes res src tmpclasses UIDemo bin doc lib nojam
Save the source code for Jargoneer in the project's src directory. You can simply click the Build button in the J2MEWTK tool bar to compile the open project.
Team-Fly |