Java Command Line Tools

The command line Java tools are important because we use them for grading.  If your code works in your IDE but not from the command line (which is possible), then you will lose points.  But using the command line tools can be difficult: you may find this guide will come in handy.

Note that everything here assumes you are running Windows 2000 or XP.  If you aren't, see the general command line notes.
 The Command Line
The command line, formerly called the DOS prompt, is a text-based interface to the computer's software.  To access the prompt on Windows systems, go to Start->Programs->Accessories and choose Command Prompt.  When you install the free JDK, you automatically get a set of command line-based tools for compiling and running Java programs. 

First, you must change the current directory of the command prompt.  To navigate to the folder your Java code is in: 
cd "drive:\folder\folder\..." 
For example: 
cd "c:\My Documents\cs211\a1" 
Note: If you can't get the commands below to work, see the last section, Finding the Java Tools

  To compile a single Java file: 
To compile all your Java files at once: 
  javac *.java  

When javac successfully compiles a .java file, it produces a corresponding .class file.  Keep these files for running the program, but do not submit them on CMS! 

Remember: the command line tools are stricter than some development environments' compilers, so if your code compiled in your IDE but won't on the command line, you are probably breaking one of these Java technicalities: 

  • Only one class per file
  • The name of the file must match the class in it
    • class MyClass {...} goes in
  • When referencing a static class member, make sure everything to the left of the last dot is the name of a class
    • Use CS211In.WORD, not f.WORD
  To run the main() method in a particular class: 
  java NameOfClass 
To supply command line arguments: 
  java NameOfClass Arg1 Arg2 ... 

Note that you do not include the file extension .class

If your program freezes while running you can usually hit Control+C to stop it. 

 The ClassPath
  Something called CLASSPATH frequently comes up in discussions of using the Java command line tools.  If your tools are properly configured, however, you normally shouldn't have to worry about this.  The CLASSPATH is usually important only when you are running Java code where the compiled .class files reside in several different folders. 
 Finding the Java Tools
On some computers, the command prompt has trouble locating the Java tools when you try to run them.  You will see a message like "Bad command or file name" or "Not recognized as a command." 

To fix this, first find out where the Java tools are installed.  Go to your C: drive in My Computer and look for a folder named something like "jdk1.3.1"; if you can't find it, try looking in Program Files or using the Windows search tool.  Ignore folders with "JRE" in their names---Java Runtime Edition doesn't include javac.  Now that you know where Java is, you can set up a shortcut for yourself. 

  1. Right-click My Computer and choose Properties
  2. On the Advanced tab, click the Environment Variables button at the bottom
  3. Is the lower of set of New/Edit/Delete buttons disabled?
    • Yes, grayed out:
      1. Click the top New button.
      2. For Variable name, enter java
      3. For Variable value, enter the full path to the folder you located above, plus \bin.  For example: c:\jdk1.3.1\bin
      4. To run Java tools on the command prompt, prefix them with %java%\ (e.g., %java%\javac *.java)
    • No, not grayed out:
      1. In the lower list box, select the entry for Path, then click Edit
      2. Move the cursor to the end of the Variable value field
      3. Carefully add to the end: semicolon + full path to the folder you located above + \bin.  For example, add: ;c:\jdk1.3.1\bin
      4. You can run Java tools on the command prompt normally, as described on the rest of this page 

Peter Flynn