Quick Compiling Example

This is a quick example of one way that you can compile and run your programs. For this example we will be typing the program on Microsoft Word and compiling it in unix. You don't need to do it this way, and if you look around there are other programs out there that you might like more, this is just one possibility.

Writing the program
The first step is typing the program into the computer. For this example we will be using Microsoft Word. Open a new word document and save it as a plain text document named MyProgram.java, where MyProgram is the name of the class you are going to type. If you are using word the next thing that you may want to do is to turn off the autocorrect features so that it doesn't try to change the capitalization of something while you are typing. Once you are done typing in your class, and making sure that you have saved any changes that you have made recently you may go onto the next step.

Compiling the Program

For this example we are going to assume that you are on the labs in the intro computing lab, however this works exactly the same if you're program is on your andrew space and you are compiling it there.

Open a Terminal Window, if you are on the macs you can do this by going to Finder -> MacintoshHD -> Applications -> Terminal, if you are on your computer at home open up a telnet window and connect to unix.andrew.cmu.edu. Now that you have the window open you will want to go to the folder that your program is kept in. If you have saved your program on the Desktop on the mac change directories to the Desktop by typing 'cd Desktop' into the terminal window. If you have saved your program on your andrew space chage directories into your andrew space by typing "cd /afs/andrew/usr/" and then moving into whatever folder you saved it in. At any point you can type "ls" to see the contents of the directory you are currently in, or "pwd" to list the directory that you are currently in.

Now that you are in the directory containing your program you are ready to compile it. To compile type "javac MyProgram.java" and pressing enter, where MyProgram is once again the name of your program. Any messages returned are compile errors and they need to be fixed in order for your program to compile and run. If it doesn't return any messages to you then that means that your program has successfully compiled, and you can run your program. If you are having trouble getting your program to compile at the bottom of the page are listed some of the basic compiler errors and some tecniques to try and correct them.

Running Your Program

Once your program has successfully compiled you can run it. To run it all you have to do is type in "java MyProgram" and press enter, where MyProgram is the name of your class that has the main method in it.

Alternative Strategies for Compiling and Running Programs

As I mentioned earlier there are other ways to be able to write, compile and run your program. For example if you want to type your program in the unix environment, which can make it easier to run from home, you can type in one of several text editors, which include emacs, pico, and vi (which is used in class). While I won't tell you how to use these programs here, there is a lot of information about them on the web if you want to teach yourself how to use them,.

If you don't want to compile the code on unix at all, then you can use one of several programs to do it for you. These programs include VisualBasic, and MetroWorks CodeWarrior, which is used in many of the other intro programming classes.

Simple Compiler Errors

Fixing compiler errors is a long and time consuming process that only gets easier with a lot of practice. Here are a couple of the simpler compile errors and some of the things that cause them.

Bracket expected

   test.java:6: '}' expected
This error means that you are missing a bracket on line 6 in your class test. In this case it wants you to add a closing bracket to line 6. You have to be careful when fixing this error message though. The java compiler doesn't always know what you want your program to do, it only knows that you have a opening bracket without a closing bracket. So when adding the bracket make sure that you add it to the area of the program where you want it to be, by going through the program and checking where each set of brackets opens and closes.

An error closely related to this one is when java tells you that your program is missing a ';'. This usually means that you forgot to put a semicolon at the end of one of your lines of code, os just go back and add it in. Unrecognized Symbol

test.java:7: cannot resolve symbol
symbol  : class wrongName
location: class test
         wrongName a;

This error means that java couldn't understand the work "wrongName" that was on line 7. Some ways to fix the error are to make sure that you spelled it right, and capitalized it correctly and make sure that you compiled the class that you are trying to use.