by Joe Carpenter
This chapter is intended to help you install through the Java, installation and give you a basic introduction to the Java Developer's Kit, as well asand give you several Java -enabled browsers. By the end of the chapter, you will have installed what you need to get going, and you'll have compiled and run your first Java application.
The Java Developer's Kit (JDK) is a software package that Sun has made available free to the public for free. thatThis package gives you all of the tools you need to start writing and running Java programs. It includes all of the basic components that make up the Java environment, including the Java Compiler, the Java interpreter, an applet viewer that lets you see applets without opening a Java compatible wWeb browser, as well as a number of other programs that are useful in creating Java programs (all of which are explained in more detail in Chapter 4). The JDK represents the bare minimum of what you need to work with Java.
If there's no such thing as a free lunch, then the JDK is more of a free light snack. Although it does contain all of the tools you really need to work with Java, it isn't an integrated development environment that many programmers are used to working with. The tools that come with the JDK are command-line driven; they don't have a nice graphical user interface like those of Visual C++ or Borland C++. The tools are intended to be called from the command prompt (the DOS prompt, for Windows 95 and NT systems). The files that contain your source code are plain ASCII text files that you create with a text editor (which you need to supply), such as the NotePad (for Win32 systems), vi (on UNIX), or BBEdit (on the Macintosh).
A C++ compiler takes high-level C++ code and compiles it into instructions that a computer's microprocessor can understand. This means that every different type of computer will need a different compiling of a given C++ program in order to be able to run it. Taking a C++ program and compiling it on different types of computers is not an easy task. Because different computers do things in different ways, the C++ program has to be able to handle those differences. This is a significant problem when dealing with the wide variety of computers available today.
The Java environment overcomes this problem by putting a middleman between the compiler and the computer. The middleman is called the Java Virtual Machine (JVM). Instead of compiling directly for one type of computer, the Java compiler, javac, takes the high-level, human-readable Java source code in a text file and compiles it into lower-level Java bytecodes that the JVM understands. The JVM then takes that bytecode and interprets it so that the Java program runs on the computer the JVM is running on. The only platform-specific program is the JVM itself. Similarly, Web browsers that support Java applets all have JVMs built into them.
The Java Virtual Machine concept provides a number of advantages—the main one being cross platform compatibility. Java programmers don't need to worry about how a computer platform handles specific tasks, and they don't need to worry about how to compile different versions of their program to run on different platforms. The only platform that programmers need to worry about is the JVM. Programmers can be reasonably confident that their program will run on whichever platforms have JVMs, such as Windows 95, Solaris, and Macintosh.
The main disadvantage of this system is that interpreting code is much slower than running a program that is native to the host computer. For each instruction in the Java bytecode, the JVM must figure out what the equivalent is for the system it is running on. This creates a slowdown in processing a Java program.
To overcome the speed limitation of Java, a number of Just-In-Time compilers (JITs) are available. JITs make the Java system even more confusing, but they make it run much faster, by taking the already compiled Java bytecode and compiling it into instructions that are native to a given computer. It's all done transparently to the user from within the JVM. The JIT, because it's part of the JVM, is also platform specific but will run any Java bytecode, regardless of what type of machine it came from. Using a JIT, a Java program can achieve speeds close to that of a native C++ program.
So now that you know a little bit more about what Java and the JDK are, you're now ready to get going on actually installing and usinge it.
If you haven't done so already,sit down at your computer, turn it on, and load up the cd-rom from the back of the book. On the CD-ROM, there is a directory called JDK. Inside the directory, "JDK" there are three subdirectories: MACINTOSH, SOLARIS, and WINDOWS. Each of these subdirectories contains the complete installation of Sun's Java Developer's Kit, for each of those three platforms. Table 3.1 shows what those refer to.
Table 3.1 Contents of the JDK folder on the CD-ROM
Directory | Contents |
MACINTOSH | Contains the JDK for the Macintosh platform, both 68k and PowerPC. |
SOLARIS | Contains two subdirectories, one for the SPARC Solaris JDK and one for the x86 Solaris JDK |
WINDOWS | Contains the JDK for x86 32 bit Windows systems, namely Windows 95 and Windows NT. |
There should not be any. No copies of previous versions of the Java Developers Kit should be on your computer. If you have stored any additional Java source code files (files that you have written or files you have received from someone else) in a directory under the main JDK Java directory, you should move those files to a new directory before deleting previous versions of the JDK. You can delete the entire Java directory tree using
After removing the previous version of the JDK, execute the self-extracting archive to unpack the JDK files. You should unpack the file in the root directory of the C drive to create C:\JAVA. If you want the JDK in some other directory, unpack the archive file in that directory. Unpacking the archive creates a Java parent directory and all of the necessary subdirectories for this release.
Unpacking the archive also creates SRC.ZIP and LIB/CLASSES.ZIP. DO NOT UNZIP THE CLASSES.ZIP FILE. If you want to review the source for some of the JDK class libraries, you may unzip the SRC.ZIP file. However, you must use an unzip program that maintains long file names to unzip SRC.ZIP. Two such unzip utility programs are UnZip 5.12, which can be found at the UUNet FTP Ssite, and WinZip. Look for the file UNZ512XN.EXE file or a later version. The sites these can be found at are in table 3.2.
After unpacking, you should add the JAVA\BIN directory onto the path. The easiest way to accomplish this is to edit the AUTOEXEC.BAT file and make the change to the path statement there.
If you have set the CLASSPATH environment variable, you may need to update it. You must replace CLASSPATH entries that pointed to the JAVA\CLASSES directory to point to JAVA\LIB\CLASSES.ZIP. Again, the easiest way to accomplish this is to edit the AUTOEXEC.BAT file and make the change to the CLASSPATH environment variable there.
After completing these changes to AUTOEXEC.BAT, save the file and reboot so that the changes take effect.
This section covers the installation of the JDK for x86 and SPARC Solaris UNIX Systems. This installation procedure is similar to some of the other UNIX operating system installations.
Copy the appropriate directory (either the x86 or Sparc Solaris release directory) onto your hard drive. Depending on how your file system is configured and the privileges on your system, you might want to either copy itthe directory into a public area, such as /USR/LOCAL, or into your home directory. The command to copy the Sparc release from the Solaris directory on the CD-ROM to your home directory is
The CLASSPATH variable is an enviroment variable that defines a path to the "classes.zip" file. Most of the tools that come with the JDK use the CLASSPATH variable to find that file, so having it set correctly is fairly important. You can set the CLASSPATH variable at the command prompt by entering the following:
Or you can put this line of text in your .login or .profile files, so it's called every time you log in:
(d)Downloading the JDK
You can download the JDK off of the Internet instead of getting it from the CD-ROM in the back of the book. When you download the JDK off the Internet, you can be fairly certain that you're getting the latest version of drive. Either way, the JDK isit's not all that hard to download, but I still think using the CD-ROM is easier.it.
The first item you need to download the JDK is a computer with a connection to the Internet that can use a Web browser. The particular Web browser doesn't really matter all that much, but for these examples, I am using the Netscape Navigator browser.
The second item you need is some (well, actually, quite a bit) of free hard disk space on the machine to which you are planning on downloading the JDK to. Table 3.3 contains the amounts of disk space you need to download and decompress the JDK for each platform.
Table 3.3 Disk Space requirements for the JDK
Platform | Disk Space Compressed | Disk Space Uncompressed |
Solaris | 5.4 Meg | 10 Meg |
Windows | 4.4 Meg | 6.5 Meg |
Macintosh | 2.5 Meg | 6 Meg |
If you have some free disk space and a browser handy, you're ready to download. Let's get started!
The JDK is a pretty big file, and downloading is going to take a while. How long it takes depends on how fast your connection is, the user load on the ftp server at that particular moment, the network load on the Internet at the time of day you are downloading the file, the beating of a butterfly's wings somewhere on the planet, sunspots, blind luck, and a large number of other factors that are even more difficult to predict. If the file transfer is going too slow for your taste, try connecting at another time. Depending on where you are on the planet, good times to connect will vary, again depending on many of the same factors that control the transfer rate.
(c)Installing a Downloaded JDK
(d)Solaris x86 and SPARC platforms.
For Solaris, the JDK is normally distributed as a compressed-tape archive (a file with a TAR.Z extension); the name of the file indicates its version.
Installing the JDK on a Solaris machine can be done one of two ways. It can either be installed into a user's home directory for individual use, or it can be installed into a public bin directory, such as /usr/local/bin/, so that all users on a system can use it. The installation process is the same for both.
(d)Windows Installation
You need Windows 95 or Windows NT to run Java. There is curently no JDK for Windows 3.1, 3.11 or Windows for Workgroups.
Why can't Java work under Windows 3.x?
At the time of this writing, no one has had the time, much less the patience, to produce a production-quality version of Java for earlier versions of Windows. There are several reasons for this, but the biggest ones seem to be these:
Installing the JDK is a fairly simple procedure, but you should know your way around the Windows and DOS enviroments. For Windows, the JDK is normally distributed as a self-extracting compressed file; the name of the file indicates its version.
If you want to squeeze a little more performance out of Java (at the cost of a little more disk space), move the classes.zip file to C:\JAVA\CLASSES (you need to create this directory), and use PKUNZIP to extract the files. You can then remove the classes.zip file.
If you choose this option, you need to change your
For Macintosh, the JDK is normally distributed as a stuffed, bin-hexed archive (a file with a HQX.SIT extension). The file version is indicated in its name.
You don't want to lose all that work you put into it, and you'll have a copy of the previous release in the event something goes wrong with your new copy.
The Macintosh allows you to name directories and files in a manner that chokeThisof folder UNIX. Filenames that UNIX can't handle include the naming of directories with slashes (/). This causes problems with the JDK because it uses a mixed UNIX/Mac method of tracking paths when the JDK attempts to locate your files. Thus, a slash in the name of a directory is interpreted as a change of directory.
UNIX also has a few problems with names that include spaces. As of this release, you should follow the UNIX file and directory naming conventions used by the developers. That means you shouldn't use spaces, slashes, asterisks, and most other punctuation characters in your file and directory names. You can, however, use as many periods as you want, and the file name can be as long as you want it (as long as it's less than 32 characters long).
For example, the following is a perfectly good Macintosh file name, but will not work under UNIX:
/../..../Stuff \/\/..java
To work under UNIX and the Mac, the file name should look like this:
Stuff.java
You now have a working copy of the JDK on your hard drive folder. This includes two essential programs: the Java compiler and the Appletviewer. You are now ready to move onto the next (and much more fun) parts of Java development.
Now you're ready to writea small Java application to test your installation of the JDK.
Somewhere on your hard drive, create a new directory to store your projects. I call mine PROJECTS and I keep it out of the JDK directory, so that I don't need to move it around whenever I install a new version of the JDK. Inside that directory, create another directory called HELLOWORLD.
Now, using your favorite text editor (such as the NotePad, vi, emacs, SimpleText, or something else), create a file called HelloWorld.java (double-check your capitalization—Java is case-sensitive), and type the following contents into it:
public class HelloWorld {
public static void main(String[]
args) {
System.out.println("Hello,
World!");
}
};
Don't worry about the details of syntax right now; just type that in, save it, and exit your text editor. Make sure it's saved as a standard ASCII text file.
If you're on a UNIX or Windows machine, at the command (DOS) prompt, type the following:
It should pause for a moment, then return you to your prompt.
Get a directory listing to make sure you have the following files:
>ls
HelloWorld.class HelloWorld.java
If you get any errors, check the HelloWorld.java code to make sure it looks exactly as it does here.
If you get an error that javac was not found, you didn't set the JAVA/BIN directory in your PATH variable. Go back and check your installation.
Now you're ready to run your first Java program! At your command prompt, type the following:
You should see the following:
Hello, World!
If you did, congratulations. You've run your first Java application, but more importantly, you've correctly and successfully installed the JDK.
If you didn't see "Hello, World!", there is something wrong with your installation. Check to make sure your CLASSPATH variable is set to point at both the current working directory (a period ".") and to the classes.zip file. Check to make sure you typed the name of the file correctly, keeping in mind that Java is case sensitive. If none of that works, you may need to reinstall the JDK.
The procedure is a bit different for a Macintosh because it doesn't have a command prompt.
1. On your Mac, open your HELLOWORLD folder so that your HelloWorld.java file appears.
2. Then open the MACJDK folder so that the java compiler icon appears (it should be a little "Duke" with a "C" on his chest). Drag the HelloWorld.java file onto the java compiler icon. The Java compiler then launches and begins compiling the program. When it's finished, a file called HelloWorld.class should appear in your HELLOWORLD folder.
3. If you received compile time errors, check the HelloWorld.java code to make sure it looks exactly as the previous code.
4. Double-click the HelloWorld.class file. The java runner application launches, loads the HelloWorld program, and runs theprogram. A window titled "stdout" should appear, with the words Hello, World! in them.
If it did, congrats. You've installed the JDK and run your first Java program.
If you didn't see "Hello, World!", there is something wrong with your installation. Check to make sure you are running System 7, that the JDK installed completely, and that the filename and the name of the class generated match, keeping in mind that Java is case sensitive. If you still can't get it to work, you may need to reinstall the JDK.
Or, to quit, you can just hit command-q, like any other normal Mac program.
Now you're ready to take on the rest of this monster book. The next chapter goes into more detail regarding the various tools that come with the JDK. It's a pretty good place to go from here.
| Previous Chapter | Next Chapter |
| Que Home Page | Digital Bookshelf | Disclaimer |
To order books from QUE, call us at 800-716-0044 or 317-361-5400.
For comments or technical support for our books and software, select Talk to Us.
© 1996, QUE Corporation, an imprint of Macmillan Publishing USA, a Simon and Schuster Company.