Tuesday, October 2, 2007

Chapter 2 Small Victories, Creating Java Projects NOTES

When creating a JAVA source file, make sure to append it with the .java extension.

Once you create a .java source file, you will compile it using the javac compiler.

If your source file contains errors, start with the first one.

When installing the J2SDK you must make sure your path is setup. The purpose of the path is to tell windows where to run executable files from. When you install the SDK tools you must check to make sure and add if necessary the appropriate information so windows knows where to find the java command line tools.

To set the PATH environment variable s value, right-click on the My Computer icon located on your desktop. This opens a pop-up menu. From the menu, click Properties to open the System Properties window. In the System Properties window, click the Advanced tab at the top right, then click the Environment Variables button located in the center of the tab window. This opens the Environment Variables window.

When you open the Environment Variables window, you will see two sections. The upper part of the window shows the user variables. User variables apply to your user login. The lower part of the window shows the system variables. System variables apply to all users, but they can be augmented by user variables. System variables can only be modified by users having Administrator privileges.

There will be a system PATH variable, but I recommend leaving the system variable alone and creating a user PATH variable. If your user PATH variable does not already exist, click the New button located below the user variables section. In the Variable Name field enter the name of the user variable:

PATH
In the Variable Value field add the fully-qualified path value to the SDK bin directory:

C:\j2sdk1.4.2_01\bin;
To this I would also add the path to the JRE bin directory. The complete PATH variable value will now resemble the following string:

C:\j2sdk1.4.2\bin;C:\j2sdk1.4.2_01\jre\bin;
Notice how the C:\j2sdk1.4.2_01 portion of the path must be repeated for both the SDK and the JRE portion of the PATH variable value. Also notice how each separate path entry is separated by a semicolon.

When you are finished creating or editing the PATH environment variable, be sure to click the OK button on each window to save your changes.

It s now time to test the PATH environment variable. Open a command-processor window as shown in figure 2-7 and enter the command javac . This should display the Java compiler help summary.



Now that you have set the PATH environment variable, the operating system can find the Java command-line tools. You must now tell the operating system where to find the Java Platform class files, as well as the class files you will create as part of your programming projects. You do this by setting the system CLASSPATH variable.

Setting The CLASSPATH Environment Variable
You set the CLASSPATH environment variable the same way you set the PATH environment variable. In fact, you can follow the same steps shown in figure 2-6. However, before setting the CLASSPATH, you will need to know two things: 1) where the Java Platform class files are located, and 2) where you are going to put your class files when you do your program development.

Let s start with the location of the Java Platform class-file location. To run Java programs, you will need the JRE. The JRE was installed along with the SDK, and is located in the jre subdirectory of the SDK. In the jre subdirectory you will find another subdirectory named lib which stands for library. The lib directory contains the Java Platform Application Programming Interface (API) files required to run Java programs. The fully-qualified path name that should be used for the CLASSPATH environment variable will be similar to this:

C:\j2sdk1.4.2_01\jre\lib
Now, to tell the java interpreter to look in the current directory for your project s class files (wherever you happen to be when you try running your Java programs) add the following path value:

.;
Yes, it s a period followed by a semicolon. The full CLASSPATH variable value will now look like this:

C:\j2sdk1.4.2_01\jre\lib;.;
While you re at it, you may as well add one more search location to the CLASSPATH. Do this by adding the following path value:

.\classes;
This will instruct the JVM to search for classes in a directory named classes , which is located in the working directory. The complete CLASSPATH variable value should now look like this:

C:\j2sdk1.4.2_01\jre\lib;.;.\classes;
When you have finished setting the CLASSPATH variable click the OK buttons to save your changes. You should now have both your PATH and CLASSPATH environment variables set. It s time to test everything by creating a few classes, compiling them, and running the resulting application class file.

No comments: