When you try to compile a program from the command line in the Windows environment, you are likely to get the following result:

The problem is that Windows does not know where to find the javac.exe program file required to produce the java byte code because the path to the JDK executable directory has not been added to the system PATH.  The PATH is an environment variable used by the operating system to locate executable files.

We can fix this problem by adding the correct directory to the system path variable.  An example of the command required to do this is:

set PATH="C:\Program Files\Java\jdk1.5.0_14\bin";%PATH%

In this case, the JDK root directory is "C:\Program Files\Java\jdk1.5.0_14" and the executable (binary) files are located in the bin subdirectory.  So, the new path being inserted at the beginning of the existing PATH environment variable is
"C:\Program Files\Java\jdk1.5.0_14\bin"
  and %PATH% represents the existing value of the PATH environment variable.  The ";" is a separator that marks the end of the new path being inserted into the existing PATH
.

Obviously, it would be tedious to do this every time you want to compile or execute programs from the command line.  An easier way to set the PATH environment variable is to use a batch file, which can be used to set the system path each time you open a new command window.  The following is an example of a batch file that would set the system path to point to the JDK executable directory.

rem Set the JAVA_HOME environment variable and insert it into the system path.
rem This will make the javac and java commands reachable from the command line.
set JAVA_HOME="C:\Program Files\Java\jdk1.5.0_14"
set PATH=%JAVA_HOME%\bin;%PATH%

In this example, the first two lines are remarks or comments.  The operating system does nothing with them.  The last two lines are Windows set commands used to create environment variables.  The first set command creates a new environment variable called JAVA_HOME that identifies the location of the JDK directory.  The second set command inserts the value of the JAVA_HOME environment variable into the current path.  These two commands together have the same effect as the single set command used above.  These commands can be saved in a simple text file, known as a batch file, in the directory that Windows opens to every time you open a new command window. 

To create a batch file, use a text editor such as Notepad, or your programming IDE.  Another option is a program called TextPad, which is a text editor that is more sophisticated than Notepad, but simpler than many of the more complicated IDEs, such as eclipse.  Save the batch file with the ".bat" file extension, which is how Windows identifies and recognizes executable batch files.  Batch files are comparable to scripts in UNIX/Linux.  In this case, we could call the batch file setenv.bat or something similar.

If we want to get more ambitious with the batch file, we can add some additional steps to end up with the following setenv.bat batch file.

@echo off
rem The first line stops Windows from echoing the commands to the command window.

rem Unset CLASSPATH in this shell to avoid configuration problems.
rem This is useful if the CLASSPATH variable has been set by another program.
rem This ensures that we will be able to compile and run programs from the current
rem directory (i.e., wherever we are at the moment).
set CLASSPATH=

rem Set the JAVA_HOME environment variable and insert it into the system path.
rem This will make the javac and java commands reachable from the command line.
set JAVA_HOME="C:\Program Files\Java\jdk1.5.0_14"
set PATH=%JAVA_HOME%\bin;%PATH% 

rem Set ICS211_HOME environment variable and insert into default path.
rem At the end of the file, we use this to change to the ICS211_HOME directory.
rem This is a convenience item, so I don't have to do it myself every time.
set ICS211_HOME="D:\UH\courses\2008.1S\ICS211"
set PATH=%ICS211_HOME%;%PATH%

rem Set an environment variable for TextPad and insert it into the default path.
rem This will allow TextPad to be reachable from the command line.
rem See separate file on using TextPad, which I like because it's easy to use.
set TEXTPAD_HOME="C:\Program Files\TextPad 5"
set PATH=%TEXTPAD_HOME%;%PATH%

rem Display environment variables for verification
echo Verify current build environment settings:
echo CLASSPATH:     %CLASSPATH%          (should be unset)
echo JAVA_HOME:     %JAVA_HOME%
echo ICS211_HOME:   %ICS211_HOME%
echo TEXTPAD_HOME:  %TEXTPAD_HOME%
echo .
echo PATH:          %PATH%
echo .

rem GoTo ICS211_HOME directory
d:
cd %ICS211_HOME%

To create a similar batch file, you should verify the full paths to any directories that you want to add to your system PATH variable.  Then you can make any necessary changes to this example code, and save it as a batch file on your system.  You can download a copy of my batch file using this URL:  setenv.bat.txt

Once you've created the batch file in your "home" directory --- i.e., whichever directory you are in when you open a command window --- you can then use the batch file by typing the filename on the command line as shown below.

After you run the setenv.bat file, you can then compile and run Java programs on the command line as shown below.

First go into the directory in which you have created your Class.java source file.  In this case I have used the HelloWorld directory for the example HelloWorld program.

D:\UH\courses\2008.1S\ICS211>dir
 Volume in drive D is Data
 Volume Serial Number is EC42-FA85 

 Directory of D:\UH\courses\2008.1S\ICS211
01/25/2008  07:40 PM    <DIR>          .
01/25/2008  07:40 PM    <DIR>          ..
01/24/2008  09:32 PM    <DIR>          assign01
01/17/2008  08:13 PM    <DIR>          ChangeDue
01/25/2008  07:03 PM    <DIR>          HelloWorld
               7 File(s)     10,508,747 bytes
               9 Dir(s)  82,628,870,144 bytes free 

D:\UH\courses\2008.1S\ICS211>cd HelloWorld
D:\UH\courses\2008.1S\ICS211\HelloWorld>

Once in the correct directory, verify that the HelloWorld.java class file is there, then compile and run from the command line.

D:\UH\courses\2008.1S\ICS211\HelloWorld>dir
 Volume in drive D is Data
 Volume Serial Number is EC42-FA85 

 Directory of D:\UH\courses\2008.1S\ICS211\HelloWorld 
01/25/2008  07:03 PM    <DIR>          .
01/25/2008  07:03 PM    <DIR>          ..
01/19/2008  01:12 PM               254 HelloWorld.java
               1 File(s)            254 bytes
               2 Dir(s)  82,628,870,144 bytes free 

D:\UH\courses\2008.1S\ICS211\HelloWorld>javac HelloWorld.java 

D:\UH\courses\2008.1S\ICS211\HelloWorld>dir
 Volume in drive D is Data
 Volume Serial Number is EC42-FA85 

 Directory of D:\UH\courses\2008.1S\ICS211\HelloWorld 

01/25/2008  07:51 PM    <DIR>          .
01/25/2008  07:51 PM    <DIR>          ..
01/25/2008  07:51 PM               681 HelloWorld.class
01/19/2008  01:12 PM               254 HelloWorld.java
               2 File(s)            935 bytes
               2 Dir(s)  82,628,866,048 bytes free 

D:\UH\courses\2008.1S\ICS211\HelloWorld>java HelloWorld 

Since this particular program used dialog boxes for input and output, the dialog boxes pop up as expected.

              and                  

Once you have created the setenv.bat file, you can use copies of it any place you want to.  I have a version of the file that I use at home and I modified the file in the ICS lab, so I can use it there also.  Note: the newest version of the JDK in the lab is update 13, so the JDK path in the lab may be different from what you might use on other computers.