by Joe Carpenter
This chapter is intended to help you install Java, give you a basic introduction to the Java Developer's Kit, and give you several Java-enabled browsers. By the end of the chapter, you will have installed what you need to get going, and you will have compiled and run your first Java application.
The Java Developer's Kit ( JDK) is a software package that Sun has made available to the public for free. This 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 Web browser, as well as a number of other programs 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.
See "JDK Tools: Javac, Appletviewer, Javadoc," Chapter 4
If there's no such thing as a free lunch, then 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 the integrated development environment 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 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).
NOTE: There are a growing number of integrated development environments (IDEs) out there from various third-party companies, each with various features that make life easier on the programmer. If you decide to do your Java development with an IDE, expect to find a code editor that can colorize Java code, a project file manager, and a faster compiler. Most of the major development companies have IDEs for Java. Microsoft (J++), Borland (JBuilder), Symantec (Cafe), Metroworks (CodeWarrior), Roaster Inc. (Roaster), and Aysmetrix (SuperCede) are just a few of the commercial Java development environments available. Each has strengths and weaknesses. If you are planning on doing serious Java development, check them out and see which fits your programming needs the best.
A C++ compiler takes high-level C++ code and compiles it into instructions a computer's microprocessor can understand. This means that every different type of computer platform 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 computer platforms 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 JVM 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 whatever platforms have JVMs, such as Windows 95, Solaris, and Macintosh.
CAUTION:
Even with Java, there are slight differences between platforms. When working with Java, it's a good idea to test a program on as many different types of computers as possible.
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.
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 using 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.
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. |
NOTE: Alternately, you can use a Web browser and a connection to the Internet to get the JDK. If you are going to download the JDK, see the section "Downloading the JDK" later in this chapter.
NOTE: What if you're not using one of those three platforms? You may or may not be in luck. There are a number of other JDKs for other platforms, but you may need to look around the Internet for them. The three previous ones are supported by Sun--any other platforms are not. There are ports for systems such as Linux, DEC Alpha, Amiga, and many others. The best place to look for information on those releases is at the Java External Related Mailing Lists and Resources page:
http://www.javasoft.com/Mail/external_lists.html
Now you'll look at how to install the JDK onto 32-bit Windows systems from the CD-ROM. The setup is fairly easy, but you should be familiar with the Windows and DOS environments before attempting to install the JDK.
Step 1: Remove Previous Versions of the JDK
Step 2: Unpacking the JDK CAUTION:
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 Site, 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.
UnZip
ftp://ftp.uu.net/pub/archiving/zip/WIN32/
WinZip
http://www.winzip.com/
Do not unzip the classes.zip file! It contains all of the classes the JDK needs to
compile your code, such as the Java.lang and AWT classes. If you unzip it, the Java
compiler may not be able to find the classes it needs, and will be unable to complete
a compile.
Step 3: Update Environment Variables
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 the changes take effect.
The next 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. For more information about getting ports of the JDK for other UNIX systems (such as Linux) see Chapter 49, "Java Resources."
The setup for installing the JDK onto a 32-bit Windows system is fairly easy;
but you should be familiar with the Windows and DOS environments before attempting
to install the JDK.
Step 1: Copy the Directory to Your Hard Drive
>cp -r sparc ~/
Step 2: Set Your Environment Variables
% setenv CLASSPATH .:/usr/local/java/lib/classes.zip
Or you can put this line of text in your .login or .profile files, so it's called every time you log in:
setenv CLASSPATH .:/usr/local/java/lib/classes.zip
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 it.
What You Need to Download the JDK
CAUTION:
At the time of this writing, the Macintosh version of the JDK 1.1 won't be available until quarter 2 of 1997. Any information here is based on the 1.02 release of the JDK. Since that release used a standard Macintosh installer program, downloading and installation instructions should not be very different. When in doubt, carefully read and follow the instructions provided by JavaSoft with the JDK installer.
Platform | Disk Space Compressed | Disk Space Uncompressed |
Solaris | 13.7 Meg | 16.5 Meg |
Windows | 5.77 Meg | 12.1 Meg |
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.
Now that you have the appropriate installer file for your computer somewhere handy on your hard drive, it is time to actually install the software so you can get to work programming. Each platform has its own standard installation procedures, and the 1.1 release of the JDK is pretty good at following them to make installation a simple and straightforward procedure.
For Solaris, the JDK 1.1 is normally distributed as a self-extracting shell script (a file with a .sh extension); the name of the file indicates its version.
CAUTION:
Use tar or your standard system backup process to back up any previous releases of the JDK before beginning installation of a new version! 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.
ls -ld /usr1
The options to the ls command specify a long listing, which includes
information about ownership and permission, and also specifies to ls to
not list the contents of the directory, which is the default. For more information
about the ls command, see your system manuals.
The output of the command should be similar to the following:
drwxr-xr-x root other 512 Feb 18 21:34 /usr
In this case, the directory is owned by root (the system administrator), and
neither the group nor the general user community has permission to write to this
directory. If you run into this situation, and you are not root, you need the assistance
of your system administrator to install in that directory.
> . jdk1.1-solaris2-sparc.sh
This will execute the shell script, which will then automatically uncompress
the file you need into the directories that you need them in.
java
java/bin
java/classes
java/demo
java/lib
java/src
setenv PATH $PATH:/usr1/java/bin
For the Korn shell and its derivatives, use:
PATH= $PATH;/usr1/java/bin
export PATH
setenv CLASSPATH /usr1/java/lib/classes.zip
For the Korn shell and its derivatives, use:
CLASSPATH = CLASSPATH /usr1/java/lib/classes.zip
export CLASSPATH
TIP: Rather than set these variables from the command line each time, you probably should add the commands to set the PATH and CLASSPATH variables in your shell resource file--.shrc, .cshrc, .profile, and so on. If you are a system administrator installing the JDK as a network development tool, you may want to add these parameters to the default configuration files.
You need Windows 95 or Windows NT to run Java. For Windows 3.1, see "Installing IBM's Applet Developer's Kit for Windows 3.1" later in this chapter.
Installing the JDK is a fairly simple procedure, but you should know your way around the Windows and DOS environments. For Windows, the JDK is normally distributed as a self-extracting compressed file; the name of the file indicates its version.
C:\JAVA
C:\JAVA\BIN
C:\JAVA\CLASSES
C:\JAVA\DEMO
C:\JAVA\LIB
set PATH=c:\windows;c:\dos;...;c:\java\bin
set CLASSPATH=c:\java\lib\classes.zip
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.
CAUTION:
Make sure to archive your current version of the JDK before installing a newer version. 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.
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 write a 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 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:
javac HelloWorld.java
It should pause for a moment, then return you to your prompt.
Get a directory listing in a DOS window to make sure you have the following files:
>dir HelloWorld.class HelloWorld.java
Or, in UNIX, 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:
>java HelloWorld
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.
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.
NOTE: The authors of the Macintosh Java Runner application have cleverly hidden the Quit command in the Apple menu. Why they did that isn't known. If you want to free up the memory that the Java Runner is taking up after it's finished running your program, choose Apple, Java Runtime, Quit. Not very Mac-like, but at least it's not a command line. Or, to quit, you can just hit command-Q, like any other normal Mac program.
Why isn't there a Sun JDK for Windows 3.1? Well, there are a number of technical issues that make porting the JDK tools to Windows 3.1 difficult, and with the release of Windows 95, Windows 3.1 was seen as a dying platform, so the decision was made to not directly support it. Some of these issues include the fact that Java needs long file names such as the ".java" and ".class" filenames. The eight-character file name and three-character extension of Window's 3.1 naming system just couldn't fully support Java file names. A more difficult problem to solve, however, is the fact that Java is a multi-threaded language, meaning it can run more than one process at the same time, but Windows 3.1 doesn't support multithreading. In order to support Java in Windows 3.1, several groups undertook projects to port the JDK to 3.1, the most successful of which is IBM's ADK.
With IBM's release of their ADK, Windows 3.1 users now have a way to develop Java applets and applications without upgrading to Windows 95 or NT. It includes a number of programs that help get around the problems previously described, as well as improving upon the tools that come with the JDK.
To get to the main ADK Web page, you first need to launch your Web browser and go to http://www.alphaworks.ibm.com/. This is the main Web page for a number of IBM's projects that are currently under development. To get to the ADK Web page, you'll need to pick the "ADK for Win 3.1" entry in the pop-up menu in the "Select" selection.
To completely install the ADK, and use all of its features, you need three components: The ADK itself, the Windows 32-bit extension Win32s, and the WinG graphics extension.
To download and install the two windows components, ftp to ftp://ftp.microsoft.com/softlib/mslfiles/ and get the following two files:
pw1118.exe
wing10.exe
The WinG extension file name is wing10.exe, and it is about 830k.
The Win32s file name is pw1118.exe, and it is about 2.4 megs. You will need to get and install both of these before installing the ADK.
To install these two system enhancements, make a temporary directory for each of the two, and put the .exe files into them. Use either a DOS prompt, or the Run command in the File menu of the program manager, to execute the .exe files. For example, if you put the wing10.exe file in a directory called "wingtemp" on your C: drive, the DOS prompt command would look like:
C:\wingtemp\>wing10.exe
This decompresses all the files to do the complete install. Each should decompress to a large number of files, with an executable called "setup.exe." After it is done decompressing, execute the setup program, again using either a DOS prompt or the File, Run menu. The setup program will prompt you for some information and then install all of the needed files. After you are done you can delete the temporary directories you put the installer programs in.
When you have WinG and Win32s installed, you can proceed with the installation of the ADK itself. You will first need to read the ADK license agreement at:
http://www.alphaWorks.ibm.com/ADK
At the bottom of the page is a button labeled "I Agree." If you have read the license and agree to its terms, you can click that button and it will take you to the download page where you can download the ADK installer. The actual ADK file is rather large, about 4 megabytes, and will take a while to download, especially over a modem connection.
Once you've gotten the ADK installer, you can then execute it from the Windows program manager File, Run menu. It will ask you for an installation directory (For example: C:\java\), and then it will do its stuff, installing all the files you'll need to get up and running with the ADK.
When the ADK is completely installed, it will create a program group with the items in Table 3.4.
Configure AppletViewer | This will run the AppletViewer and display a license document. |
ADK.WRI | The ADK User Guide, read this for more information on the ADK. |
ADK File | A file manager type application that lets you manipulate files with long file names, rather than the Win 3.1 standard 8.3 file names. |
ADK Edit | A small editor that integrates the ADK tools into one program, so you can work with Java code without having to switch between a number of other programs. |
ADK Console | The guts of the ADK, this is the program that runs all of the Java environment-based tools such as AppletViewer and javac. |
To test your installation, follow these steps:
This should launch the Animator applet, and put a dancing Duke on your screen. If it did, then you're all set to develop Java programs on your Windows 3.1 machine. If it didn't, make sure that the path you put into the Working Directory field is actually the path that has the Animator applet and that there is a example1.html file in that directory. If not, you may need to go back through the installation process and try again.