Running Java programs in CodeWarrior

CS 136
Data Structures and Advanced Programming

Spring, 1998

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, select "CSCI136Application" 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 ".mcp". For example a program whose main class is named "Test" will have a project named Test.mcp. (Note that the "mcp" is only there to be compatible with Metrowerks on Windows.)

  4. At this point the computer will create a folder with the name of your main class (e.g., "Test" if you typed "Test.mcp" in the previous step). In the folder will be the project file (Test.mcp), a program file named TrivialApplication.java, and a folder whose name is obtained by appending "data" to the end of the project name (e.g., "Test.mcp data"). Don't touch anything in the data folder.

  5. A project window will pop up. It contains directories "Sources" and "Classes". Click on the triangle to the left of the directory names to see their contents. "TrivialApplication.java" will be in Sources while "classes.zip" and "structure.zip" will be in Classes. As you might expect, the first folder holds your source code, while the second contains pre-compiled packages. The package classes.zip includes classes necessary to run all Java programs, while structure.zip contains all of the classes from Bailey's structure library.

  6. Double click on TrivialApplication.java. Change the class name to the name of your main class (e.g., "Test"). Under file menu, select "save as" and save as "Test.java". 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.java. It is important that your Java file name correspond to the name of the class stored in the file. Metrowerks will usually be able to handle it if you don't do this, but most other Java compilers will get confused. Go back to the finder and throw away TrivialApplication.java as it is no longer needed and simply clutters up your directory.

  7. Unfortunately, you must change another of other items in order to get the system straightened out. Under the Edit menu, select "Java Application Settings". Under "Target Settings", set the "Target Name" to the name you you wish the executable to have (usually the same name as the main class). Under "Java Target", set the "Main Class". To be the name of the class you wish to begin executing as the main program (e.g., "Test"). Finally, under "Linker output", set the name of the output to be the name of your main class followed by ".jar" (e.g., "Test.jar"). Technically, only the middle step is required (setting "Main Class" under Java Target). The other two could be ignored or given any other name you like, but it is convenient to change all three. Save these settings and close the window.

  8. 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 either ".zip" or ".jar" 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.

  9. 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. Buttons on the project window can also be used to "make" or "run" your program. The button which looks like "writing a list" does the "make", while the "right arrow" does the "run".

  10. 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.

  11. At the top of an IDE editor window holding a Java class definition is a menu labelled "{}". 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.

  12. When you run a Java program, the Metrowerks IDE application launches a separate application, Metrowerks Java, to interpret your compiled program. If you forgot to change the Java Target you may get an error message that it could not find your main class. If this occurs, go back to the IDE, fix the Java Target, and select Run again.

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 Targeting Java (either QV or pdf format) and CW IDE user's guide for MacOS (either QV or pdf) 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 Targeting Java and then branching out to the other documentation.