Running the Emulator Within a Visual Environment
Although it is certainly beneficial and relatively painless to execute the J2ME emulator at the command line, you can opt to use a visual development environment for MIDlet construction, in which case you will probably want to launch the emulator directly from within the visual tool. Fortunately, this functionality is supported in the current crop of J2ME visual development environments. The next couple of sections explain how to utilize the emulator from within the KToolBar and Forte for Java visual development environments.
Both the KToolBar and Forte for Java development environments are designed for use with the standard emulator included with the J2ME Wireless Toolkit, which means that you can't currently use them to invoke the emulator included in the Motorola SDK for J2ME.
The KToolBar development environment makes it very easy to invoke the J2ME emulator on a MIDlet project to test a MIDlet. To use the emulator, you must first open an existing project or create a new one. To open the samle MIDlet suite included with the J2ME Wireless Toolkit, click the Open Project button on the KToolBar main toolbar and then click example in the list of projects. Clicking the Open Project Button in the dialog box selects the sample project and opens it. To execute the MIDlet suite in the J2ME emulator, click the Run button on the toolbar. The J2ME emulator will then be executed just as if you had run it from the command line using the run.bat script. In fact, the information typically displayed in the command-line window upon executing the emulator now appears directly in the KToolBar application window. Figure 3.7 shows the output of the emulator as it appears in the KToolBar application window after exiting the emulator.
Figure 3.7 The output that results from running the J2ME emulator appears directly in the ktoolbar application window.
Don't forget that you can also compile a MIDlet project by clicking Build on the KToolBar toolbar.
In addition to running the emulator from within the KToolBar application, you can also tweak its properties through a visual interface. To alter these properties, select Preferences from the Edit menu. Figure 3.8 shows the Emulator Preferences dialog box that is displayed after selecting the Preferences command.
Figure 3.8 The ktoolbar aplication enables you to alter emulator properties through a visual interface.
The most useful property you will likely want to change is the target device, which is easily modified by clicking the drop-down list of available devices. As an alternative to using the Emulator Preferences dialog box, you can also select the target device for the project using the drop-down list that appears on the main toolbar.
Forte for Java
If you're looking for a more comprehensive development environment than KToolBar has to offer, you might be considering Forte for Java. Similar to KToolBar, Forte for Java allows you to test MIDlets by invoking the J2ME emulator from within the visual environment without ever having to enter commands at the command line. To test a MIDlet in the emulator using Forte for Java, you must first open a MIDlet project. Because Forte for Java expects to find all projects in the Development directory beneath the main installation directory, you might want to copy the entire example directory from the J2ME Wireless Toolkit into the Development directory of Forte for Java. This will make the J2ME Wireless Toolkit example MIDlets available from within Forte for Java.
When you first run Forte for Java, it will prompt you to use the J2ME Wireless Compiler with the current project (Figure 3.9). You should click Yes to accept this compiler because you will need it to compile and build MIDlets in Forte for Java.
If you aren't prompted to use the J2ME Wireless Compiler when you first run Forte for Java, it probably wasn't installed correctly and doesn't recognize the J2ME Wireless Toolkit. It is very important that the J2ME Wireless Toolkit be installed after you install Forte for Java. Otherwise, the J2ME add-on module won't be properly integrated with Forte for Java and you won't be able to compile or test MIDlets from within Forte for Java. If you've already installed the toolkits in the wrong order, you will need to uninstall them and then reinstall them in the correct orderfirst Forte for Java, and then the J2ME Wireless Toolkit.
After accepting the J2ME Wireless Compiler for the current project, Forte for Java continues to load and a list of directories will appear in the Forte Explorer window. Click to open the midp folder and then the uidemo folder. At the bottom of the file list you will see a file named uidemo. This is the JAD file for the project, and is used to execute the MIDlet suite. Right-click the file in the list and select Execute from the pop-up menu. Forte for Java will then compile and pre-verify the project if necessary, and then launch the UIDemo MIDlets in the J2ME emulator, as shown in Figure 3.10.
Figure 3.9 Upon first running forte for JAVA, you are prompted to use the J2ME wireless compiler.
Figure 3.10 The videmo midlet suite is executed in the J2ME emulator from within forte for JAVA.
Similar to KToolbar, Forte for Java enables you to change the J2ME emulator properties through a visual interface. To access the emulator properties from within Forte for Java, you must first select Settings from the main Project menu. You will then be presented with the Project Settings dialog box, which contains a list of expandable project settings. Click next to Execution Types to view the different types of execution supported for the project. Then click Emulator Executor to reveal the KVM Emulator entry. Finally, right-click KVM Emulator and select Customize from the drop-down list to display a Customizer Dialog that contains the emulator properties ( Figure 3.11).
Figure 3.11 The customizer dialog in forte for JAVA provides access to J2ME emulator properties.
These emulator properties should now be familiar to you because you've seen them a few times throughout the lesson. If you need a refresher, refer to Table 3.2.