Chapter 2: Installing and Using Java
The Java Development Kit (JDK) is provided free of charge from Sun Microsystems (http://java.sun.com). You can download two files: the development kit library and executables and the documentation files. They are also provided on the accompanying CD-ROM. For Windows 95, these files are called JDK-1_0_2-win32-x86.exe and JDK-1_0_2-apidocs.zip. You can download sources of some of the libraries as src.zip as well.
To install the JDK libraries, simply run the exe file:
JDC-1_0_2-win32-x86
This will create a \java directory with a number of directories under it, notably \java\bin and \java\lib.
To install the documentation, you must unzip it using a zip program that preserves long filenames. WinZip from Nico-Mak Computing (http://www.winzip.com) is one such program. The 16-bit Windows or DOS versions of PKZIP will not preserve the needed long filenames and you shouldn't try to use them.
Using WinZip, unpack the "apidocs" file into the directory \java\api.
You need to add the \java\bin directory to your path so that the command line programs java and javac are available to you. Add the following statement to the bottom of your autoexec.bat file:
path=%path%;c:\java\bin;
if you have installed the programs on your C drive.
You also need to add the CLASSPATH environment variable to your system by inserting the following statement in your autoexec.bat file:
set CLASSPATH=.;c:\java\lib\classes.zip;
Do not unzip the classes.zip file in this directory: it is just a series of library files used by Java. Note especially the ".;" at the beginning of the CLASSPATH statement. This is required for Java programs to find the files that you compile in other directories.
All Java source files have the .java extension. For example the simulated search window program we showed in Chapter 1 is called srchapp.java . Note that since this is a 4-character extension, this qualifies as a long filename under Windows 95, and all of the tools you use for handling Java programs must be able to deal with long filenames. (This will probably not apply to a Windows 3.1 version of java which is reportedly under development.)
When you compile a Java program, it produces one or more files having the ".class" extension which you then can operate on either directly with the java interpreter or with the appletviewer program.
You can write Java programs using the EDIT program provided with Windows 95. This is just a simple DOS Window-based character editor, but it allows you to read and write files having long filenames. You can also use the WordPad editor for this purpose.
Further, there are any number of integrated development environments which feature automatic indenting and syntax highlighting. These include Symantec Café, Sun Java Workshop, Borland Latte and many others. Note however, that whatever editing system you use, that source code files for java programs always has the .java extension.
When you have written your first java program you compile it by running the javac compiler. Several of the integrated development environments do this for you under the covers. Symantec's Café system actually uses a native x86 compiler called sj under the covers to perform this compilation considerably faster than the Sun's javac compiler.
As an example, let's compile the simple simulated search application we referred to in the last chapter. This program is in the \chapter1 directory of the example disk, and is called Srchapp.java. Before you can compile it, you must copy it to a directory on your disk where the compiler can read and write files. Then, to compile it, you type
javac Srchapp.java
or if you have Symantec Café loaded, you can use its much faster native Intel compiler and simply type
sj Srchapp.java
Note that you must specify the program filename using the exact case of the long filename and that you must include the .java filename extension as part of the command line.
The java compiler will produce one or more output files having the .class extension. In this case, it generates the file
Srchapp.class
Then, to execute this program we run the Java interpreter, specifying the main file of the program:
java Srchapp
Note that here, the exact case of the filename is again required, but that the filename extension (.class) is not required. In fact, if you include the ".class" extension, it is an error. The Java interpreter runs the main program and searches for the other required class files in the path specified by the CLASSPATH environment variable. In this case, the interpreter looks first in the current directory, where the Srchapp.class program is located and finds the MainPanel1.class file as well. Then it looks in the classes.zip file in the c:\java\lib directory for any needed java support files.
The Srchapp program we worked on above is a Java application or a stand-alone program which runs without respect to a web browser or web page. By contrast, applets are programs which are embedded in web pages and can only be run either by your browser or by a test program called appletviewer.
As we noted in the previous chapter, an applet is restricted in the access it has to your computer. It cannot read or write local files or environment variables and it cannot gain access to your network or to other computers than the computer providing the web server. To embed an applet in a web page, you need to include an <applet> tag in the HTML text of your web page. A simple web page which simply displays an applet is contained in the file srchapp.html which has the contents:
<html> <body> <applet code="srchapp.class" width = 400 height =300> </applet> </body> </html>
The srchapp program we have been discussing has been written so that it can run either as an application or as an applet, so it will in fact work when embedded in a web page.
Then, if you want to view that applet, you can simply load that web page into your browser, or you can run the appletviewer program from your command line:
appletviewer srchapp.html
Note in particular that the target file for the appletviewer program is an HTML file, not a .class file. You can look at a similar search application embedded in a web page by typing the above command.
Figure 2-1 shows the same application running in
the appletviewer and Figure 2-2 shows this applet running on a
web page in the Netscape browser.
Figure 2-1: The simulated search application running
using the appletviewer program.
Figure 2-2: The simulated seach application running inside Netscape.
Since Java is an interpreted language, some have indicated their worry that its performance will be unacceptable. Two things argue against this concern: First, the Java byte-code interpreter for the most part just makes direct calls the the operating system for all graphical operations, making it roughly equivalent to Visual Basic.
Second, for more computationally bound operations, Just In Time (JIT) compilers are now becoming available. These JIT compilers interpret the byte codes as usual, but also translate them into local machine language so that if that code is executed more than once in a loop, all further executions will be executed as native machine instructions. Performance of first generation JIT compilers so far has provided a 3x to 5x speed improvement, and greater improvements are on the horizon.
Now that we have a grasp of how Java is installed
and how to compile simple programs, let's look at
the Java language
itself, and see how it compares with Visual Basic and other related
languages.
[Top of page] | [Previous chapter] |
[Next chapter] |
[Java expertise]
Java and
all Java-based trademarks and logos are trademarks or registered
trademarks of Sun Microsystems, Inc. in the U.S. and other countries.