Java and the Linux Command Line

This document instructs you on how to use the Linux shell with Java. For a light-hearted essasy on the command line, you might enjoy Neal Stephenson's light-hearted essay In the Beginning was the Command Line.


You will use the Java compiler javac to compile your Java programs and the Java interpreter java to run them. We'll assume you have already installed these.

Command-line interface

You will type commands in an application known as the shell. Since you're using Linux, we assume you're somewhat familiar with it.

Compile the program

You will use the javac command to convert your Java program into a form more amenable for execution on a computer.

Execute the program

You will use the java command to execute your program.

Input and Output

If your program gets stuck in an infinite loop, type Ctrl-c to break out.

If you are entering input from the keyboard, you can signify to your program that there is no more data by typing Ctrl-d for EOF (end of file). You should type this character on its own line.


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. Also, try running your program with the command line

[wayne] ~/introcs/hello> java -cp ./ HelloWorld
If this works, your classpath is set incorrectly.

How do I check that my PATH variable is set correctly? Type echo $PATH.

Where can I learn more about the command line? Here is a short tutorial on the command-line.