provided by: 
Originally published at Internet.comJava Programming Notes # 2572 * Preface * Viewing tip * Figures * Listings * Supplementary material * General background information * Preview * Discussion and sample code * Command-line input * Capturing output in a child process * Run the program * Summary * What's next? * Resources * Complete program listings * Copyright * About the author -----------------------------------
Preface
This is the second lesson in a series of tutorial lessons designed to teach you how to write programs using the Sun Java Wireless Toolkit for CLDC. The first lesson was titled Getting Started with MIDlets and the Sun Java Wireless Toolkit for CLDC (see Resources). In that lesson I provided and explained a Java programming framework that makes it relatively east to experiment with MIDlet programming.
A side trip
In this lesson, I will take a side trip into the world of Runtime.getRuntime().exec(cmdString). I will teach you how to capture and display the standard output and the error output produced by programs executing in a child process resulting from a call to that method.
Upgrade the framework
Then I will apply that knowledge to upgrade the MIDlet development framework that I presented in the previous lesson. This will make it possible to display compile time errors when the framework is used to compile a MIDlet and will also make it possible to display information written to standard output or error output by MIDlets being tested in the Sun cell phone emulator.
Not peculiar to MIDlets
Although I am including this material in a series primarily dedicated to MIDlets, the programming techniques that I will teach you in this lesson are not peculiar to the development of MIDlets. Rather, these techniques represent a general-purpose programming capability that is useful in a wide variety of applications in which the exec method is called for the purpose of executing another program in a child process.
Another minor upgrade to the framework
While I am at it, I will also show you how to upgrade the framework program to make it responsive to command-line parameters.
Viewing tip
I recommend that you open another copy of this document in a separate browser window and use the following links to easily find and view the figures and listings while you are reading about them.
Figures
* Figure 1. User interface for the framework program. * Figure 2. Framework output for a MIDlet with a programming error. * Figure 3. Partial description of the Process class. * Figure 4. Methods of the Process class. * Figure 5. Framework output for a MIDlet that produces standard output in the cell phone emulator.
Listings
* Listing 1. Contents of a simple batch file. * Listing 2. Beginning of the class and the main method. * Listing 3. Upgraded compile method. * Listing 4. The method named getChildOutput. * Listing 5. The class named ChildDataHandler. * Listing 6. The upgraded framework program named WTKFramework02. * Listing 7. The MIDlet named LifeCycle01.
Supplementary material
I recommend that you also study the other lessons in my extensive collection of online Java tutorials. You will find a consolidated index at www.DickBaldwin.com.
General background information
Referring back to the previous lesson (see Resources), The following steps are required to produce a MIDlet and to get it running in a cell phone: 1. Design the MIDlet (similar to designing any Java program) 2. Write the program code (similar to coding any Java program) 3. Compile the code (targeted to a Java v1.4 virtual machine, a specific version of the CLDC, and a specific version of the MIDP) 4. Pre-verify the compiled code 5. Create a manifest file 6. Create a Java Archive (JAR) file 7. Create a Java Application Descriptor (JAD) file 8. Test the MIDlet in a cell phone emulator 9. Deploy the MIDlet into a cell phone
The Java framework program that I presented in the previous lesson handles items 3 through 8 in the above list with the single click of a button. As mentioned earlier, I will teach you how to upgrade the framework program so that it will capture and display the standard output and error output produced by programs executing in a child process resulting from a call to the Runtime.getRuntime().exec(cmdString) method.
I will also show you how to upgrade the framework program to make it responsive to command-line parameters.
Preview
The user interface
Referring once more to the previous lesson, Figure 1 shows the user interface for the framework.
Figure 1. User interface for the framework program.
One change will impact the user interface
One of the changes that I will make to the framework in this lesson will impact the contents of the upper right text field shown in Figure 1. This text field contains the default program name. I will make it possible to change the default program name by entering a different program name on the command line when the framework program is started.
Why make this change?
This change makes it possible to create a batch file to start the framework with a different default program name. This is a very convenient procedure for repeatedly
Author: Richard G. Baldwin
Read article at Internet.com site