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
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. 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 Set the JAVA_HOME
environment variable and insert it into the system path.
rem Set ICS211_HOME
environment variable and insert into default path.
rem Set an environment
variable for TextPad and insert it into the default path.
rem Display environment
variables for verification
rem GoTo ICS211_HOME
directory 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
Directory of
D:\UH\courses\2008.1S\ICS211
D:\UH\courses\2008.1S\ICS211>cd 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
Directory of
D:\UH\courses\2008.1S\ICS211\HelloWorld D:\UH\courses\2008.1S\ICS211\HelloWorld>javac HelloWorld.java
D:\UH\courses\2008.1S\ICS211\HelloWorld>dir Directory of D:\UH\courses\2008.1S\ICS211\HelloWorld
01/25/2008 07:51 PM
<DIR> . 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. |