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:
|
|
Compiling | |
To compile a single Java file:
javac FileName.java 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:
|
|
Running | |
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.
|