Hello, World in Java 1.5 on Mac OS X


This document instructs you on how to setup a Java programming environment for your Mac OS X computer and provides a step-by-step guide for creating, compiling, and executing a Java program. You must be using Mac OS X v 10.4.2 Tiger or later. (Previous versions do not support Java 1.5.) All of the software is freely available on the Web.

Java

You will use the Java compiler javac to compile your Java programs and the Java interpreter java to run them. Mac OS X includes Apple's implementation of Java 2 Standard Edition (J2SE) 1.5.0. so there is nothing to do in this step except to run Software Update if you're not up-to-date.

Command line interface

You will type commands in an application called the Terminal. You might enjoy reading Neal Stephenson's light-hearted essay In the Beginning was the Command Line.


Text Editor

You will type and edit your programs in a text editor called JEdit. JEdit is similar to conventional word processors like MS Word, but it features many specialized programming tools including syntax highlighting, bracket matching, auto indenting, indent shifting, line numbering, and commenting out code.

Apple purists may prefer Project Builder which can be found in the Finder via Computer -> OS X Partition -> Developer -> Applications -> Project Builder.

Create the program

Now you are ready to write your first Java program.

Compile the program

It is now time to convert your Java program into a form more amenable for execution on a computer.

Execute the program

Now it is time to run your program. This is the fun part.

Congratulations, you are now a Java programmer! To continue, return to to the Introduction to Computer Science booksite.

Troubleshooting

When I try to run java I get: Exception in thread "main" java.lang.NoClassDefFoundError. First, be sure that HelloWorld.class is in the current directory. Be sure to type java HelloWorld without a trailing .class or .java. If this was not your problem, it's possible that your CLASSPATH was set by some other program so that it no longer includes the current working directory. Try running your program with the command line

machine:~/introcs/hello username$ java -cp ./ HelloWorld