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.
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.
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.
- Open a terminal window. You can find this under
Go -> Applications -> Utilities. Drag the Terminal to your dock since
you will be using it frequently.
- You should now have a Terminal window somewhere on the screen.
It will have a prompt that looks something like:
machine:~ username$
- To check that you have the right version of Java installed, type
the commands in boldface below.
machine:~ username$ java -version
If Java is installed, you should see something like:
java version "1.5.0_06"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_06-112)
Java HotSpot(TM) Client VM (build 1.5.0_06-64, mixed mode, sharing)
Then type
machine:~ username$ javac -version
You should see something like:
javac 1.5.0_06
javac: no source files
...
-
Now, create a directory to store your Java programs.
In the Terminal, type the commands in boldface below:
machine:~ username$ mkdir introcs
machine:~ username$ cd introcs
machine:~/introcs username$ mkdir hello
machine:~/introcs username$ cd hello
machine:~/introcs/hello username$
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
~/introcs/hello. All of your
files for Assignment 0 will go here.
Don't be intimidated 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.
-
Since you will be using the Terminal frequently, you may want to
customize the default window settings (e.g.,
Monaco 13pt font with antialiasing).
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.
-
Download
the latest stable version of JEdit using the MacOSX package link.
-
We strongly recommend the following JEdit customizations.
-
Enable Mac OS style menubar in
Plugins -> Plugin Options -> Enable Mac OS Menubar
- Change the default indentation to 4 spaces via
Utilities -> Global Options -> Editing -> Indent Width,
Utilities -> Global Options -> Editing -> Tab Width, and
Utilities -> Global Options -> Editing -> Soft (emulated with spaces)
tabs.
- Change the default edit mode to Java using
Utilities -> Global Options -> Editing -> Default edit mode
-> Java.
- Change the default tab width when printing to four spaces via
Utilities -> Global Options -> Printing -> Tab width when printing
- Add line numbers by checking Utilities -> Global Options -> Gutter -> Line Numbering
- Allow at most 80 characters per line by
Utilities -> Global Options -> Editing -> Word wrap -> hard and
Utilities -> Global Options -> Editing -> Wrap margin -> 80.
- Change the default line separator to Unix via
Utilities -> Global Options -> General -> Default Line Separator
- Remove the annoying . that marks the end of a line by unchecking
Utilities -> Global Options -> Text Area -> End of Line Markers
-
To associate .java files with JEdit so that when you double-click a .java file,
it opens it in JEdit:
- Right click a .java file.
- Choose Open With...
- Select JEdit from the list of programs or locate JEdit by selecting Other
if necessary.
- Check the Always use this program to open this file type box
Apple purists may prefer Project Builder which can be found in the Finder
via Computer -> OS X Partition -> Developer -> Applications -> Project Builder.
Now you are ready to write your first Java program.
-
Launch JEdit Start -> jEdit.
Begin by saving the empty document: Select
File -> Save As, choose the directory /Users/username/introcs/hello
and type in the file name HelloWorld.java.
The file name is case sensitive and must exactly match the name of the class
in the Java program. Don't forget to include the .java extension in the
file name. By doing this, JEdit will know you are working on a Java
file, and it will format it appropriately.
-
Now, in the JEdit window, type the Java program
HelloWorld.java
exactly as it appears below.
If you omit even a semicolon, the program won't work.
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World");
}
}
-
Finally, save the file using
File -> Save.
It is now time to convert your Java program into a form more
amenable for execution on a computer.
-
From the Terminal, type
machine:~/introcs/hello username$ javac HelloWorld.java
- If everything went well, you should see the following
in the Terminal:
machine:~/introcs/hello username$
Silence is golden in computer science.
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.
Now it is time to run your program. This is the fun part.
-
At the Terminal, type
machine:~/introcs/hello username$ java HelloWorld
-
If all goes well, you should see
machine:~/introcs/hello username$ java HelloWorld
Hello, World
- You may need to repeat this
edit-compile-execute cycle a few times before it all goes smoothly.
Use the up and down arrow keys to repeat previous commands and avoid extra typing.
Congratulations, you are now a Java programmer!
To continue, return to to the
Introduction to Computer Science booksite.
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