This document instructs you on how to use the Mac OS X Terminal with Java.
You will use the Java compiler javac to compile your Java programs and the Java interpreter java to run them. To check if Apple's implementation of Java 2 Standard Edition (J2SE 5.0) is already installed:
You will type commands in an application called the Terminal.
machine:~ username$
Then typemachine:~ username$ java -version java version "1.5.0_13" Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_13-b05-237) Java HotSpot(TM) Client VM (build 1.5.0_13-119, mixed mode, sharing)
machine:~ username$ javac -version javac 1.5.0_13 javac: no source files ...
Now you are ready to write your first Java program.
public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World"); } }
You will use the javac command to convert your Java program into a form more amenable for execution on a computer.
machine:~ username$ cd introcs/hello machine:~/introcs/hello username$
If everything went well, 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.
If all goes well, you should see the output of the program - Hello, World.machine:~/introcs/hello username$ java HelloWorld Hello, World!
Input and Output |
If your program gets stuck in an infinite loop, type Ctrl-c to break out.
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
If this works, your classpath is set incorrectly. Ask the teaching staff for help.machine:~/introcs/hello wayne$ java -cp ./ HelloWorld
How do I get the menu to display at the top of the screen instead of at the top of the frame? Execute with java -Dapple.laf.useScreenMenuBar=true
Where can I learn more about the command line? Here is a short tutorial on the command-line.