Testing the Java Compiler and JVM

   

Now that you have hopefully made it through the installation process without any issues, you need to be sure the installation was successful. The two main things that you need to test are the Java compiler and the Java Virtual Machine. These tools are necessary to compile and run your applications. Before you get there however, you need to be sure that the two environment variables that you'll need are setup correctly. Let's look at each one separately.

The Path Environment Variable

When you want to run a Java application or use the SDK tools, the operating system needs to be able to locate the java command to execute the program. You can specify this by fully qualifying the directory where the SDK tools are installed, which is in the \ bin directory under the base directory for the SDK 1.3 installation.

The other option is to add the \ bin directory to the system PATH variable. This approach allows you to run the SDK command from any directory. To do this on a Windows platform, just add the <<JAVA_HOME>>\ bin directory to your PATH variable in the autoexec.bat (for Win95/98/2000) or the system environment properties (for WinNT). The <JAVA_HOME> variable is the base directory where the SDK was installed. On Unix, you can add the PATH variable to the .login so that it will be set each time you log in.

The CLASSPATH Environment Variable

The CLASSPATH variable is the single way to tell applications and SDK tools where to locate user and system classes. It must be set correctly. If it isn't, the compiler, interpreter, and other SDK tools and user applications will not be able to find the class files they need to run.

The CLASSPATH variable contains class file directories, JAR files, and ZIP files. You should separate each one of the directories or JAR / ZIP files by a separator character depending on the platform. For Windows, the separator is a " ; " character, for Unix and Linux, use a " : " character.

Another important question is what directories or JAR files are required to be in the CLASSPATH . For the SDK 1.3, you should have a minimum of two JAR files in the CLASSPATH . The two JAR files are the <<JAVA_HOME>>\ jre\ lib\ rt.jar file and the <JAVA_HOME>\ lib\ tools.jar file. You will also probably have other JAR files and or directories as you develop your applications.

There are two methods to setting the variable. One method is to set the CLASSPATH variable each time you run an application by specifying the “cp or “classpath option for the java or javac commands. The second alternative is to set the system CLASSPATH once and then there is not much need to set it when starting your applications or using the Java tools. A good approach is to set the CLASSPATH variable with the SDK necessary JAR files and then use the “cp or “classpath options when running your applications. This way, you will not have to worry about changing the system CLASSPATH very often. Most developers use batch or shell scripts to execute Java applications and set the CLASSPATH variables in there. You can add JAR files and directories to the system CLASSPATH by using the environment variable to reference the system CLASSPATH variable. For example, on the Windows platform, here is an example of starting an application:

 java classpath %CLASSPATH%;c:\ classfiledir;c:\ another.jar 

On UNIX machines, the CLASSPATH variable is a colon -separated list of directories, JAR files, and/or ZIP files.

 setenv CLASSPATH .:/users/java/:/usr/local/jdk1.3/jre/lib/rt.jar:  /usr/local/jdk1.3/lib/tools.jar 

This command can be put in your .login file, so it's set properly every time you log in. If you are the system administrator, you probably want to make this CLASSPATH variable available to all users.

On the Windows platform, it's a semicolon-separated list of directories in the form

 set CLASSPATH=.;C:\ users\dac\ classes;C:\ jdk1.3\ jre\ lib\ rt.jar;  c:\ jdk1.3\ lib\ tools.jar 

This line can be put in your AUTOEXEC.BAT file so that the CLASSPATH is set properly every time you boot your machine.

The first period points the CLASSPATH at the current working directory, which is quite helpful if you don't feel like typing in full pathnames every time you want to do something with the Java program you're working on at a given moment.

   


Special Edition Using Java 2 Standard Edition
Special Edition Using Java 2, Standard Edition (Special Edition Using...)
ISBN: 0789724685
EAN: 2147483647
Year: 1999
Pages: 353

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net