Java Command-Line Essentials



IMPORTANT NOTE:

All of the following commands assume the Java compiler (javac) and interpreter (java) are installed in an accessible directory in the current operating environment. This is true on CSL lab machines, and will be true when Java is properly installed on other machines.

Compiling Java Source from the Command Line

  1. Start an operating system command shell, e.g., a UNIX terminal or a Windows Command Prompt.
  2. Change directory (cd) to the location of the .java source file to be compiled.
  3. Enter the following command,
    javac file.java
    where "file".java is the name of the Java source file.

Compiling Multiple Source Files

  1. Open a command shell.
  2. Change directory to the location of the source files.
  3. Enter the following command:
    javac file1.java file2.java . . . filen.java
    where "filei.java" are the names of the Java source files. The names may contain any wildcard characters recognized by the operating system, for example the following command compiles all files with the .java extension in the current directory:
    javac *.java

Running a Compiled Java Program

  1. Open a command shell.
  2. Change directory to the location of the compiled file(s).
  3. Enter the following command:
    java classname
    where classname is the name of Java class containing the main method of the program to be executed.

Further Information

The Sun Java site has complete documentation on javac, java, and other Java tools and utilities. The location is

http://java.sun.com/javase/6/docs/technotes/tools/




index | info | lectures | labs | programs | solutions | examples