Java Reference
In-Depth Information
In Chapter 2, you learned that Java source code has to be compiled (converted to bytecode) and interpreted
(converted to machine language and run). Both of these functions are not standard Windows commands. If you tried
to run a bytecode file, Windows would search the location in the PATH variable and not find the “run” command.
In other words, Windows does not know anything about Java. Therefore, for a Java application to be executed, the
PATH and CLASSPATH variables have to be edited to include the java command files location (i.e., in the JRE) and the
location of the .class files.
If we modify the variables using the Environment Variables window, our application will only work for this
computer. For example, if we took the memory stick (in the F: drive) to another computer, we would have to set that
computer's environment variables to look for our class files and the JRE on the F: drive. This would have to be done
for every computer we want to run the application on. To get around this problem, we are going to write a small batch
program (and store it on the F: drive) that will set the PATH and CLASSPATH variables dynamically on whatever
computer we want to run the application on.
If you have not programmed on a PC before this may come as a surprise, but all the functions performed from
the Windows GUI (graphical user interface) can be performed with commands. For instance, the dir (directory)
command lets a user display the contents of a folder or drive (the same function that the My Computer icon provides).
In addition, there are path and Set classpath commands that can control the two environment variables we need
to define. Our batch program will use these commands to configure the environment variables. In addition, we will
include a JRE command to run our Java application.
Tutorial: Creating a Batch File
Windows commands can be stored in a file with an extension of “bat” (indicating that the file is a batch file). When
a bat file is specified at the command prompt (or its icon is double-clicked), Windows will execute the commands
contained in the file in sequential order. We will use the Windows Notepad utility to enter, and save the commands in
a batch file called Employee_Application.bat .
1.
Click Start, All Programs, Accessories, and then Notepad (see Figure A-17 ).
Figure A-17.
 
Search WWH ::




Custom Search