3.1 Terminal

The revolutionary thing about Mac OS X for a developer is a single, boring window with a blinking cursor. Double-click on the main icon for your computer on the desktop, navigate into the Applications directory, and then into Utilities. Inside this folder, you'll see a Terminal icon. Double-click on this icon to open the application.

You'll then see a "Welcome to Darwin!" message and a localhost prompt. The shell is the Unix standard tcsh, which stands for "Tenex csh." csh was the default shell for BSD Unix through the 1980s (and Mac OS X is based on BSD Unix). tcsh is an upward-compatible enhancement of csh, which includes "command completion" borrowed from an early 1970s experimental operating system called Tenex.

If you're an emacs user, turn on emulation of the Meta (alt) key in the "Terminal Preferences... Emulation" screen (or by selecting "Shell Inspector... Emulation" for a per-window modification).

3.1.1 Basic Terminal Commands

Learning to use the Terminal is far beyond the scope of this text, but a few basic commands are required for basic system navigation. These common commands are listed here:


Change directory.


List the contents of the current working directory.


Print the current working directory.


Enter the manual (documentation) system.


Format output to display a page at a time.

When you first launch the Terminal, you are presented with a blinking cursor. Type pwd and press return. You will have started in your specific user's home directory (for example, my username is wiverson, so my Terminal starts in /Users/wiverson).

Now type ls. This will print a directory listing to your screen. Then type cd Desktop and hit return. Type pwd, and you will see that you have changed your current working directory to /Users/[username]/Desktop. Type ls and you will see files on your Desktop. Type cd ~ (on many English QWERTY keyboards, this is the shifted version of the key to the left of the number "1"). This command will return you to the home directory.

3.1.2 A Simple Java Class

Now enter pico HelloWorld.java. This command launches the pico application, a simple terminal-based text editor. The editor is now ready to work on a file called HelloWorld.java. Enter the text shown in Example 3-1.

Example 3-1. A simple HelloWorld class
class HelloWorld
     public static void main(String[] args)
          System.out.println("Hello World!");

Press Control-O to save the file (or "WriteOut", as the command is labeled). Then use Control-X to quit pico.

Type javac *.java at the command line and hit return. It may take a moment, but assuming there are no errors, the HelloWorld.java file will compile and a HelloWorld.class file will appear (use ls to confirm the new file in your working directory). If you have problems, type pico HelloWorld.java again to open the file and make changes.

If you've installed JDK 1.4.1, the system will have updated the command-line tools to point to the JDK 1.4.1 versions of the tools such as javac. If you want to use the JDK 1.3.1 tools after you've installed JDK 1.4.1, you'll need to refer to them using their full path in /System/Library/Frameworks/JavaVM.framework/Versions/1.3.1/Commands.

Once the file compiles, type java HelloWorld from the command line (don't add the .class extension). It should now print out "Hello World!" You can take this HelloWorld.class file, and any computer that has a Java Virtual Machine should be able to run it.

This section has reviewed a very basic set of operations. If you need more information, consult one of several excellent texts on Java and Unix:

  • Learning Unix for Mac OS X, by Dave Taylor and Jerry Peek (O'Reilly)

  • Mac OS X for Unix Geeks, by Brian Jepson and Ernest E. Rothman (O'Reilly)

3.1.3 Environment Variables

You may want to define some common environment variables for use in later sessions. Assuming you're sticking to the standard tsch shell, you can create a file called .tcshrc in your home directory. The contents of this file will be executed when you create a new tsch shell (for example, by opening a new Terminal window). Remember that files beginning with a period (.) will not appear in Finder windows or in ls views by default. Use the ls -a command to see all files, including those with periods.

One common convention is to set the JAVA_HOME environment variable, which on a Mac OS X machine is particularly relevant, considering that there is a single standard JVM directory. To set the JAVA_HOME environment variable, put the following contents in the .tcshrc file:

setenv JAVA_HOME /Library/Java/Home

If you use a different shell, then the .tcshrc file won't execute. You can check your shell's documentation or use .login, which almost all shells execute on startup.

A common bugaboo is the CLASSPATH environment variable, a source of much Java heartache. Whenever possible, I recommend putting CLASSPATH environment variable settings into shell scripts specific to each application. Doing so will keep your single JDK installation healthier and more pristine. Debugging CLASSPATH problems is one of the most thankless tasks around, and is best avoided whenever possible.