This documents instructs you on how to setup a Java programming
environment for your Mac OS X computer and provides a step-by-step
guide to creating, compiling, and executing a Java program.
We assume you are running at least OS X 10.2.3.
All of the software is freely available on the Web.
|
You will use the Java compiler javac to compile your Java programs and the Java interpreter java to run them. Mac OS X includes implementation of Java 2 Standard Edition (J2SE) 1.4.1, so there is nothing to do in this step.
|
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.
[username:] ~>
If Java is installed, you should see something like:[username:] ~> java -version
Now, check that the Java compiler is accessible.java version "1.4.1_01" Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.1_01-69.1) Java HotSpot(TM) Client VM (build 1.4.1_01-24, mixed mode)
If it is installed, you should see something like[username:] ~> which javac
If not, install the Developers Tools CD that came with your Mac and use Software Update to make sure you have the latest version./usr/bin/javac
The mkdir command creates a new directory; the cd command changes the current working directory. After executing these commands, your working directory is the newly created ~username/introcs/hello/. All of your files for Assignment 0 will go here. Don't be scared by the Terminal - you will only need to use a few basic commands. Keep this window open since you will need it later in the assignment.[username:] ~> mkdir introcs [username:] ~> cd introcs [username:] ~/introcs> mkdir hello [username:] ~/introcs> cd hello
|
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. It's even written in Java.
|
Now you are ready to write your first Java program.
public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World"); } }
|
It is now time to convert your Java program into a form more amenable for executing on a computer.
If javac complains in some way, you mistyped something, and you should check your program carefully. Ask for help if you can't see the mistake.[username:] ~/introcs/hello> javac HelloWorld.java
Silence is golden in computer science.[username:] ~/introcs/hello> javac HelloWorld.java [username:] ~/introcs/hello>
|
Now it is time to run your program. This is the fun part.
[username:] ~/introcs/hello> java HelloWorld
[username:] ~/introcs/hello> java HelloWorld Hello, World
|
When I try to run java I get: Exception in thread "main" java.lang.NoClassDefFoundError Your CLASSPATH may have been set by some other program so that it no longer includes the current working directory in the search path. Try running your program with the command line
[username:] ~/introcs/hello> java -classpath ./ HelloWorld