Running Java as a Command Line Application

  1. Install the Java SDK as described in other instructions.
  1. To develop a program in Java, you will typically write several files, one for each class.  Set up a directory and store all your Java files for one program in this directory.
  1. Write each class and store it in a file with the same name as the class, including case, with a .java extension.  For example, the class defined by

public class ThisClass

would be stored in the file

These program files can be written in any editor and should be stored as text with the .java extension.

  1. To compile and run, open a command prompt window or DOS window.  There is usually an icon for this on the Start menu, either on the main menu or under accessories.  It is called DOS on Win95 and Win98 and it is called Command Prompt on Win2000 and WinXP. Under unix or linux systems you simply use the command interface.
  1. In your command line window, change directory to the directory where your programs are located to do your work.
  1. To compile the code in a file like the example above.


  1. You can only run a file as an application that contains the method static void main.  To run the main method from such a class, type.

java ThisClass

You can do much of your Java programming in this way. There is one additional setup process if you want to use libraries that are stored in a different directory from the one where you do your work.  These libraries may be supplied with a textbook or may be part of something like the AP CS case study.  If you want to store such libraries and utility code in one place, follow the set of instructions for setting up your own library wiht jar files found here or for setting the CLASSPATH environment variable.


Chris Nevison
June 2002