Hello World in Java on Mac OS X
This document instructs you on how to set up our Java programming environment for your Mac OS X computer. It also provides a step-by-step guide for creating, compiling, and executing a Java program using either DrJava or the Terminal. All of the software used is freely available on the Web.
These instructions apply to Mac OS X 10.4.11 and higher.
You can defer steps 4–6 until Section 1.5 of the textbook.
We strongly recommend using the Mac OS X installer described below; however, we do have manual instructions.
|
|
Our installer downloads, installs, and configures
the Java programming environment you will be using,
including Java SE 6, DrJava, and the standard libraries from our textbook.
- Log in to the user account in which you will be programming. Your account must have Administrator privileges and you must be connected to the Internet.
-
You will need a Java runtime, which is installed by default on all versions of
Mac OS X prior to 10.7 Lion.
If you are running Lion and don't have a Java runtime,
install the Java runtime.
Relaunch your browser for the changes to take effect.
-
To install,
- Download introcs.zip.
- Double-click it to unzip it.
- Double-click the introcs.app to perform the installation. If you receive a warning that introcs.app was downloaded from the Internet, click Open.
- Enter your password when prompted.
-
If the installation succeeds, you will see the following:
- A terminal window containing approximately this execution log.
- A Standard Draw window containing a red bullseye and a textbook graphic.
- Delete introcs.zip and introcs.app.
|
|
Now you are ready to write your first Java program.
You will develop your Java programs in an application called DrJava.
DrJava features many specialized programming tools including syntax highlighting,
bracket matching, auto indenting, and line numbering.
- The installer creates a shortcut to DrJava on the desktop. Double-click it to launch DrJava. If you receive a warning about incoming network connections, click Allow.
-
In the main DrJava window, type the Java program
HelloWorld.java exactly as it appears below. If you omit even a semicolon,
the program won't work.
As you type, DrJava does the indenting for you.public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World"); } } - Finally, click the Save button to save the file. Use DrJava to create the folder /Users/username/introcs/hello and name the file HelloWorld.java. The file name is case sensitive and must exactly match the name of the class in the Java program.
|
|
It is now time to convert your Java program into a form more amenable for
execution on a computer. To do this, click the Compile button.
If all goes well, you should see the following message in the Compiler Output
pane at the bottom:
Compilation completed.
If DrJava complains in some way, you mistyped something. Check your program carefully, using the error messages in the Compiler Output pane as a guide.
|
|
Now it is time to run your program. This is the fun part.
-
Type the following in the Interactions pane at the bottom.
By convention, we highlight the text you type in boldface.
If all goes well, you should see the following message:> java HelloWorld
Welcome to DrJava. Working directory is /Users/username/introcs/hello > java HelloWorld Hello, World
- You may need to repeat this edit-compile-execute cycle a few times before it works. Ask for help if you can't see the mistake.
|
|
The command-line provides capabilities beyond those available in DrJava,
including redirection and piping.
You will type commands in an application called the Terminal.
-
The installer creates a shortcut on the desktop to the Terminal.
Double-click it to launch the Terminal.
You should see something like:
The ~ is shorthand for your home directory /Users/username.machine:~ username$
-
To confirm that the Java compiler is installed,
type the command in boldface below and check that the results match:
It's important that you see the number 1.6 or 1.5 for the Java version number, but the rest is not critical.machine:~ username$ javac -version javac 1.6.0_26
-
To confirm that you the Java interpreter is installed, type
the command in boldface below and check that the results match:
Again, it's important that you see the number 1.6 or 1.5 for the Java version number, but the rest is not critical.machine:~ username$ java -version java version "1.6.0_26" Java(TM) SE Runtime Environment (build 1.6.0_26-b03-383-11A511) Java HotSpot(TM) 64-Bit Server VM (build 20.1-b02-383, mixed mode)
|
|
You will use the javac command to convert your Java program into a form more amenable for execution on a computer.
-
From the Terminal, navigate to the directory containing HelloWorld.java,
say /Users/username/intros/hello,
by typing the cd (change directory) commands below:
machine:~ username$ cd introcs machine:~/introcs username$ cd hello machine:~/introcs/hello username$
-
Compile it by typing the javac command below:
Assuming the file HelloWorld.java is in the current working directory, you should see no error messages.machine:~/introcs/hello username$ javac HelloWorld.java machine:~/introcs/hello username$
|
|
You will use the java command to execute your program.
-
From the Terminal, type the java command below.
You should see the output of the program.machine:~/introcs/hello username$ java HelloWorld Hello, World
|
|
The installer didn't work on my machine. What should I do?
First, please contact a staff member to identify what went wrong.
What does the installer do? In short, it downloads, installs, and configures Java3D, Java OpenGL, our standard libraries, Checkstyle, Findbugs, and DrJava. Here is a more detailed list:
- Checks that Java is installed.
- Downloads and installs Java3D 1.5.2 from j3d.zip and Java OpenGL 1.1a from jogl.zip.
- Downloads the textbook standard libraries from stdlib.jar.
- Downloads and installs Checkstyle 5.4 from checkstyle.zip. Downloads our checkstyle configuration file checkstyle.xml and execution script checkstyle.
- Downloads and installs Findbugs 1.3.9 from findbugs.zip. Downloads our findbugs configuration file findbugs.xml and execution script findbugs.
- Downloads and installs the latest stable version of DrJava, from drjava-osx.tar.gz. Creates a shortcut to DrJava on the desktop. Downloads and installs the DrJava configuration file from drjava-config.txt to /Users/username/.drjava. Note that this will overwrite any existing .drjava configuration file.
- Tests that the installation succeeded by compiling and executing TestIntroCS.java.
Why does the installer need my password? The installer copies a few files into the /Users/username/Library/Java/Extensions directory, which requires superuser privileges.
How do I completely uninstall introcs.app?
- Delete the directory /Users/username/introcs (but save any of the .java files you created, if desired).
- To uninstall OpenGL and Java3D, remove the following nine files:
- /Users/username/Library/Java/Extensions/jogl.jar
- /Users/username/Library/Java/Extensions/vecmath.jar
- /Users/username/Library/Java/Extensions/j3dcore.jar
- /Users/username/Library/Java/Extensions/j3dutils.jar
- /Users/username/Library/Java/Extensions/gluegen-rt.jar
- /Users/username/Library/Java/Extensions/libjogl_cg.jnilib
- /Users/username/Library/Java/Extensions/libjogl_awt.jnilib
- /Users/username/Library/Java/Extensions/libjogl.jnilib
- /Users/username/Library/Java/Extensions/libgluegen-rt.jnilib
- To uninstall our textbook libraries, delete the following two files:
- /Users/username/Library/Java/Extensions/stdlib.jar
- /Users/username/Library/Java/Extensions/algs4.jar
- To uninstall DrJava, delete the following two files:
- /Applications/DrJava.app.
- /Users/username/.drjava.
- To uninstall Checkstyle and Findbugs, delete the following two files:
- /usr/local/bin/checkstyle
- /usr/local/bin/findbugs
- Delete the shortcut to DrJava and Terminal on the desktop.
What happens if I rerun the installer? It will re-download, install, and configure Java 3D, Java OpenGL, our standard libraries, Checkstyle, Findbugs, and DrJava.
I am using a different version of Java in DrJava and the Terminal. Is this a problem? Yes, they should both be either 1.5 or 1.6; otherwise, you may get a "class file has wrong version 50.0, should be 49.0" error. If the versions of Java are different, go to /Applications/Utility/Java Preferences.app and verify that the Java SE 6 (64-bit) entry is checked and at the top of the list; if not, drag to change the preferred order.
What should I do if I have previously installed DrJava in another location? We suggest deleting it and using the version in /Users/username/introcs by using the newly created shortcut to DrJava on the desktop.
Why Java 6 instead of Java 7? Mac OS X and DrJava do not currently support Java 7.
Can I use a different version of Java? Yes, any version of Java 5 or Java 6 should work fine.
Can I update DrJava to use a newer version? Yes, but you do not need to do so.
Can I use a different IDE? Yes you can use another IDE (such as Eclipse) but you will have to configure the IDE properties yourself (such as the classpath).
How do I break out of an infinite loop? In DrJava, click the Reset button in the menubar or select the menu option Tools -> Reset Interactions. From the Terminal, type Ctrl-c.
When using standard input, how do I signify that there is no more data? If you are entering input from the keyboard, type Ctrl-d for EOF (end of file).
I successfully compiled HelloWorld.java with javac, but, when I execute, I get the error message "Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld". What am I doing wrong? First, verify that the file HelloWorld.class is in the current directory. Be sure to type java HelloWorld without a trailing .class or .java.
When I compile or execute a program in Terminal that uses one of the textbook libraries, I get an error that it cannot find the library. How can I fix this? Verify that the following file is in the specified location:
- /Users/username/Library/Java/Extensions/stdlib.jar
Which shell should I use in the Terminal? Bash is the default shell on Mac OS X Lion, but feel free to use whichever one you prefer.
Where can I learn more about the command line?
Here is a short tutorial on the
command line.
