CS 136
Data Structures and Advanced Programming

Spring, 1997

Running Java Programs in CodeWarrior

Here are some simple instructions to get you going in writing Java code in CodeWarrior. They do not cover using the debugger. More detailed references are cited at the end.
  1. After launching Metrowerks IDE (Integrated Development Environment), select New Project under the file menu.

  2. In the dialog box that appears click on the triangle next to Java, highlight "applet", and click OK.

  3. When the next dialog box pops up, navigate to where you wish to save your program and then type the name of your project. It is generally best to select the name of the main class of your program and add ".u". (The u - pronounced "mu" can be typed by holding down the option key while typing "m".) For example a program whose main class is named "Test" will have a project named Test.u.

  4. At this point the computer will create a folder with the name of your main class (e.g., Test if you typed "Test.u" in the previous step). In the folder will be the project file (Test.u), a program file named TrivialApp.java, and a file with "html" commands named TrivialApp.html. The latter two are listed in the project window along with classes.zip which includes necessary classes to run all Java programs.

  5. Double click on TrivialApplet.html. Change all 3 occurrences of TrivialApplet to the name of your main class ("Test"). Under file menu, select "save as" and save as Test.html. Note be careful to save in folder Test. Unfortunately, saving without navigating to the right folder will save it in the wrong place, screwing you up royally when you try to run your applet. Note that when you use "Save as" it will change the file in the project to Test.html.

  6. Double click on TrivialApplet.java and change the name of the class in the code from TrivialApplet to Test. Save it as Test.java. Go back to the finder and throw away TrivialApplet.html and TrivialApplet.java as they are no longer used.

  7. Fill in appropriate code in Test.java. Don't forget to put in import statements if you need classes from any packages (such as graphics, structures, or java.awt). You will also need to add ".java" and ".zip" files to the project for any classes or libraries needed in your program. You can add these to the project by either selecting Add Files from the project menu or Add Window from the same menu if the class you wish to add is from the topmost window on the screen.

  8. Programs can be compiled and run using the project menu. Selecting "compile" will compile the class in the topmost window (and any classes it depends on). Selecting "make" will compile all classes in the project which have been changed since their last compilation. Selecting "run" will compile all of these classes and launch an applet viewer for running your program. (Classes that need to

  9. Of course, you are likely to have syntax errors when you compile Java code. If so, a window "Errors and Warnings" will pop up with a list of all of your errors. The first error will be highlighted and a listing of the class containing the error will be displayed with a red arrow pointing to the line with the error. You can fix errors directly in this listing and those changes will be recorded in the original file. To move to the next error, either click once on the error or hold down the command (apple) and option keys and click on the "[[arrowdown]]" key. If you double-click on an error message it will bring up the original file at the point of the error. When you have fixed all of the errors just try compiling again (the system will generally save all of the changes you have made automatically). Much of the time you will receive another wave of error messages to respond to. This simply reflects the fact that the compiler got farther before giving up the compilation.

  10. At the top of an IDE editor window holding a Java class definition is a menu labeled "{}". If you pull this down you will see a list of all of the methods defined in the class. Select any one to go immediately to its definition.

  11. When you run a Java program, the Metrowerks IDE application launches a separate application, Metrowerks Java, to read the html file and run your program. When you finish running your program and wish to go back and make changes, you must quit Metrowerks Java. If you don't, then the next time you try to run your program, you will execute the old rather than new version of your program. You will know this has happened if you get a second applet window showing on the screen and see writing in the output window from the previous run. If this happens, quit Metrowerks Java and select "run" all over again from Metrowerks IDE.

    UPDATE: There is an alternative to this procedure. You can let Metrowerks Java keep running and go back and edit your program. However, when ready to run it again, select "Make" rather than "Run" from the Project menu. This will compile and link your program, but not run it. Now go back to Metrowerks Java and select "Reload" under the Applet menu. Not surprisingly, this loads the new version of the program into the applet viewer. At this point you can run as usual!

Of course there is much more to learn about using CodeWarrior. The best way to learn about CodeWarrior is to read the on-line documentation. I liked the CW Targeting Java QV and CW IDE user's guide QV (both available through the CS136 folder by going into Metrowerks/CodeWarrior Documentation/ CW QuickView Reference) for getting information on how to use the Metrowerks IDE, Metrowerks Java, and the debugger. The Apple guides for Metrowerks (available under the "?" menu when Metrowerks IDE is running) can also be quite helpful in providing instruction on setting up projects and also have reasonable documentation on the Java language. I recommend starting with CW Targeting Java QV and then branching out to the other documentation.