Writing CodeWarrior Java Applications

Bowdoin College Department of Computer Science
revised September 4, 1997




CodeWarrior is a software environment that supports Pascal, C, C++, Java, and Assembly language programming. This document describes how to use the current version of CodeWarrior, called CodeWarrior Professional, to write and run Java application programs. For using CodeWarrior to develop programs in other languages, readers should consult other documentation.


Files and Folders Used by CodeWarrior

CodeWarrior allows large programs to be written as one or more files. When you first start a program in CodeWarrior there are three different kinds of files to keep in mind: These files are all kept in a "project folder," which has the same name as the project file (except for the suffix ".µ"). For example, "Lab1" might be the name of your project folder for lab 1, while "Lab1.µ" is the name of the corresponding project file. The project folder is a regular Macintosh folder that holds all the required files for a particular programming project.

When you write a Java program, you will need to first create a project and then create one or more files that contain the text of that program. These files are all kept in the project folder. After you complete the development of your program, there will be additional files inside the project folder:


Part 1 - Find CodeWarrior and Start It Up

CodeWarrior is on the hard drive of the Macs in the Adams 208 lab and other Mac labs. Look in the CodeWarrior folder on the hard drive and double click on the CodeWarrior IDE 2.0 icon. It looks like this:

(Once you have a project defined and saved, you can just double click on that project file (with the .µ suffix) to restart CodeWarrior; this step is only necessary the first time you use CodeWarrior).

When CodeWarrior starts, the screen should have a menu bar that looks like this:

Optionally, you may open the "toolbar" by selecting Show floating toolbar under the Window/Toolbar menu. The toolbar contains shortcuts for many of the menu items, and it looks like this:

You might want to look at some of the items available on the pulldown menus, just to familiarize yourself with them.


Part 2 - Creating a New Java Project

In the menu bar, select the New Project... option under the File menu. This creates a dialog box like this:

Now click once on the right-pointing triangle next to Java, click once on Java Application to select it, and then hit the OK button, as shown below:

Now you'll get a dialog box similar to the following:

Be sure that your own diskette has been inserted in the drive on the Mac. Now use the folder names at the top of the window in the figure above to select your diskette (the one shown above is named "untitled"), where you want your work to be saved. A common mistake is to leave your work on the computer's own disk drive instead of taking it away on your own diskette -- be careful to avoid this.

Now name the project (the one shown above is named Lab1.µ, and the character µ is formed by typing option-M on the keyboard).

Save the project by clicking the Save button. After you've done this, a window like the one below will appear:

The figure above also shows that the newly created project has two parts, called Sources and Classes. If you click on the triangles on the left of these two parts, you willsee that the Java source program "TrivialApplication.java" and the libraries "classes.zip" and "System.in.zip" are prepackaged inside this project. Later, we will see how your own program can replace this Java source program

Just to make sure you understand the difference between a project folder, a project file, and a Java program, take a little detour to look at the contents of your floppy disk. It should contain a project folder called "Lab1" and double-clicking on that folder should show the following files:


Part 3 - Create Your Own Program for This Project

You now need to create a separate file that contains the program for your project. Under the File menu, select the New option. This will open a new file with the heading untitled. Use the Save As command under the File menu to give the program file a name and store it inside the Lab1 folder on your diskette. The dialog box is illustrated below.

You must choose a program name that ends with the suffix ".java " (like "myfirst.java" in the picture shown above).

Now we must add the program file we just created to the project Lab1.µ, and remove the TrivialApplication.java program that is currently there as a placeholder.

To add your program file, select TrivialApplication.java in the project window, and then select the Add Files... command in the Project menu. A dialog box like the one shown below will appear, and the name of the program myfirst.java will appear in the list. Clicking Add and then Done will add this program file to the project.

Now select TrivialApplication.java and click Remove Selected Items in the Project menu to delete this sample program that came with the project originally. Finally, select TrivialApplication Settings under the Edit menu, and a settings window will appear. You should make three changes in this window:

  • Select Java Target and type "MyFirst" as the Main Class to identify that the class that will execute when your program is run will be the class MyFirst (instead of the class TrivialApplication, which your program has just replaced).
  • Change Target Settings -> Target Name to "MyFirst".
  • Change Linker Output -> Type to "Droplet" from its current setting. The resulting project window should now look like the following:

    Typing the Program

    Go back to the program window by double clicking on the file name "myfirst.java". You are now ready to type a program using the editor, such as the following simple Java program. (While typing is better for the soul, you may choose to copy and paste it directly off this Web page if you are in a hurry.)
    import java.io.*;
    import com.mw.io.*;
    
    public class MyFirst {
    
        public static void main(String args[]) {
    	 SystemInput sysIn = new SystemInput();    // open the input window   
    	 try {
    	    ReadStream r = new ReadStream();       // create an input stream
    	    int n = 0;
    	    System.out.println( "Enter an integer in the System.in window");
    	    n = r.readInt();                       // read an input integer
    	    for (int i=n; i>0; i--)
    	      System.out.println(i);
    		System.out.println( "Welcome to CodeWarrior Java!" );
    	    sysIn.dispose();                       // close the input window
    	 } 
    	   catch (Exception e) {
    	    System.out.println("Exception " + e.toString() + " occurred");
    	    sysIn.dispose();
    	   }
        }
    
    }
    
    If this is your first program, be very careful to copy all the punctuation, tabs, and spacing exactly as they are shown.

    As you type, notice that some program elements appear in different colors (comments appear in red and reserved words appear in blue); this is often helpful in finding and correcting syntax errors, and generally makes the program more readable.

    It is important to know that Java is a case-sensitive language, so that capital letters are different from their lowercase counterparts. Ror example, the variable names MYNAME, MyName, and myname have distinct meanings in a program if they all appear. Reserved words in Java (the words that appeared in blue in the program you just typed) are always written in lowercase. Comments can have any mixture of upper and lowercase characters, and begin with the characters //.

    You should periodically use the Save command under the File menu while you are typing a program. This precaution gives you added insurance against unexpected (though infrequent) system failures. When you are finished, save the complete program.


    Part 4 - Compile and Run the Program

    Once you have completed and saved your program, it needs to be compiled (checked for syntactic errors) along with the other modules in the project. This is done by selecting the Make option in the Project menu. If your program contains any syntactic errors (errors in its grammatical structure), a window will open and identify the nature and location of each error in your program.

    To run the program, first double-click the System.in.zip line in the project window. Second, select the Run command under the Project menu. (Note that a speedy way to make and run a program is to use the cloverleaf-m and cloverleaf-r commands for Make and Run, respectively.) These two steps start the separate application Metrowerks Java, which will in turn open the two new windows shown below:

    The upper window, System.in is for typing input to the program. The other, Java Output, is for displaying output by the program. Notice that each of these windows can be resized in the usual manner (use the mouse to grab and drag the lower righthand corner).

    For this particular example, you need to type an integer into the System.in window in order for the program to complete. For instance, if you choose to type the integer 5 in the System.in window, the Java Output window will look like the following when the program run is completed:


    Part 5 - Errors

    There are many ways to make errors while writing and running Java programs. A common error occurs when you make changes to a program and then recompile it and run it again. In this case, you may see an error message like the one below:

    This means that the MetroWerks Java application is still running from your previous test run of the program, and must first be terminated (select Quit under File) before restarting it for your next run.

    Try returning to the editor and creating an error in the text of your program, and try compiling and running the program again. Notice that a window with an error messages appears, and that double-clicking on the error message in that window takes you to the line in your program where the error occurs (the underlined word locates the position within that line of the error itself).

    Can you figure out the meaning of the error message in light of the change you made to the program? The error you have introduced into your program is an example of a syntax error; the semantics (meaning) of the program may be as you intended, but your program does not follow all the Java syntactic requirements that are needed for the compiler to translate the program into an exacutable machine language program.


    Part 6 - To Print the Program

    To print, make sure that the window you want to print is at the front of the screen. This will usually be the program window, but sometimes you might want to print an output window. Then go to the File menu and select Print. This will print a hard copy of your program text on the printer at the back of the lab in 208 Adams (or whatever other printer has been selected in the Chooser).

    Before you print, you might want to check the Chooser (from the apple menu) to see that the printer is set correctly to print in the lab you're working in. In many labs, it's also a good idea to make sure that the printer itself is turned on before you try to print.

    Some printers will allow you to change the print format to save some paper. Before you print, go to the File menu, and select Page Setup. The 2up layout will print your program using half as much paper (but in a smaller font). You can also use the Page Setup menu to print in landscape orientation (rotated 90 degrees), which can be helpful if your program has very long lines.


    Part 7 - Leave CodeWarrior and Turn In Your Program

    To end your CodeWarrior session, select Quit under the File menu. Be sure that the latest changes in your program are saved before quitting.

    If you've been working on the desktop, don't forget to copy your files onto your own floppy disk.

    To submit your program electronically, open the appropriate course folder for your class and drag your program file to the Drop Box folder that appears there. To find the course folder, look in the Courseware folder on the harddrive. (Opening one of the course folders causes a new icon to appear on your desktop.)