Writing, compiling, and running your first Java application (for Mac users)

This tutorial assumes that you have already completed the software install. If you haven't done this, go back and do so now.

Organization of files

Before doing anything else, go to "Documents" (or wherever you normally store your files) and create a folder called CS120. All of your work for the course will go inside subdirectories within this folder. Inside your CS120 folder, create another folder called hwk0. Whenever you start a new program, create a new folder for it inside CS120, to keep your files well-organized. Here is an article on creating folders and organizing files on Mac OS X.

Creating your .java program file

Open up BBEdit with a new document. Go to the hwk0 folder you previously created, and save the document as HelloWorld.java (there should be no spaces, and uppercase versus lowercase has to be exact). Type in the source code given below exactly. Java is case sensitive, meaning you need to be careful to have uppercase and lowercase precisely as I have it below. The indentation on the left hand side is done using the tab key (one tab on the "public static" line, two tabs on the "System.out" line).

public class HelloWorld {
	public static void main(String[] args) {
		System.out.println("Hello world!");
	}
}

Next you will compile and run your program.

Compiling and running your first Java program

  1. Start by making sure to save the most recent version of your .java file from BBEdit.
  2. Open up a Terminal window from BBEdit. Do this by going to the view menu of BBEdit and selecting Terminal (the shortcut for this is holding down the 2 buttons to the left of the space bar and pressing the letter O key).
  3. To compile from the Terminal window, type in javac HelloWorld.java, since HelloWorld.java is the name of your Java source code file. If you ever have multiple files to compile, you can use javac *.java to compile everything in that directory.
  4. (Note: If you window pops up saying that the Java JDK is not installed, then click on the "more info..." option. The Java download website will pop up. Scroll down, download the "macOS Installer" option, and install it. You will only ever have to do this once.)
  5. If there are syntax errors, they will be reported. Fix the first error that appears (you may need to scroll up), and recompile. If there are no syntax errors, it won't say anything.
  6. Once it compiles without errors, run your program by entering the command java HelloWorld, and notice that we don't put in the .java part this time.
  7. (Future note: Keep the terminal window open as long as you are still working on your program. This way you can quickly compile and run by using the up and down arrows to select previously typed in commands in the terminal window.)

Once your program runs, you should see some text that greets you. There's your first Java program! Next you will need to hand it in.

Handing it in

  1. Navigate to Homework 0 in Educat.
  2. Upload HelloWorld.java, which is your source code file (not HelloWorld.class).