Running Java programs in CodeWarrior

 

1. After launching Metrowerks IDE (Integrated Development Environment), select New under the file menu.

2. In the dialog box that appears, highlight Java Stationery. Now enter a project name. 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.) Now click OK.

3. In the next dialog box, click on the triangle next to JDK 1.1, and then select Java Application. In the future, you will select CS136Application.

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 Trivial Application.java, and a folder whose name is obtained by appending "data" to the end of the project name. 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" 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. You will often also add Bailey's structure.jar file to Classes. Note that if you had selected CS136 Application rather than Java Application in the previous step, both packages would have been included for you.

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 application. 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 a number 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 application 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 "down-arrow" 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.

13. Between successive runs of your program, you will need to quit the Metrowerks Java application. You can do so by selecting Quit under the apple menu.

Of course there is much more to learn about using CodeWarrior. The best way to learn is by doing. You will find yourself quickly becoming accustomed to following the steps above. If you would like to learn even more about CodeWarrior, there is plenty of on-line documentation. You can find it under the Help menu when Metrowerks IDE is running.