Stepping Up to Straight Java
In This Chapter
If you've made it this far in the book
and whether it's worth looking into.
as long as you have a Java-enabled browser handy. The same Java applet will run on Windows 95, Windows NT, UNIX, and Macintosh, with no need for customizing the applet to the particular computer.
Java Relies on a "Virtual Computer"
In order to pull off the trick of being able to work on a variety of platforms, Java applets actually run on what's called the Java Virtual Machine. Look at it this way: There are programs available today that allow you to run Windows software on a Macintosh, or UNIX software on a Windows computer, and so on. They do this by emulating the behavior of a particular computer type (they "pretend" to be a Mac or Windows or UNIX machine so that the software thinks it's running on the correct computer).
The Java Virtual Machine is another type of emulator, but it doesn't emulate Mac, Windows, or UNIX. Rather, it pretends to be a totally different type of system; and by defining what the Java computer can and cannot do, Sun was able to create a program that, no matter what physical hardware it's run on, will always look like a Java computer to the applets.
Java is an "object-oriented" programming language most closely related to C++. "Object-oriented" is a difficult concept to explain clearly, but in essence, it means that the language revolves around manipulating end-results rather than designing the tools for manipulation. An object-oriented sculptor, for instance, would be more concerned with melding together various blocks of clay than the tools that carve the clay initially. The popular construction game Lego is a nice example of an object-oriented activity.
To start, you'll need to do a little surfing and pick up a copy of the collection of utilities Sun puts out to aid Java programmers: the Java Developer's Kit.
HotJava from Sun is the other current Java-capable browser, so let's take a quick peek at it. At this time, HotJava can only view "alpha" stage applets, and Navigator 2.0 can only view "beta" stage applets. In time, these two distinctions should erode.
The HotJava browser is available from Sun's Web site, at
Once you download the Windows 95 version, simply run the .exe file and it will self-extract. From there, launch HotJava by running the file hotjava.exe, which is in the newly created hotjava\bin directory.
HotJava is not as feature-laden a browser as Netscape Navigator, but it is simple to use. To connect to a new URL, either enter it into the URL Location box at the top of the window, or choose the menu commands File, Open.
At the bottom of the HotJava browser window are the five navigation buttons: Back, Forward, Home, Reload Current, and Stop.
You don't need to do anything specific to enable Java; merely opening a Web page that contains a compatible (alpha) Java applet will cause it to be loaded and run. A good place to begin is the Java Demos page, available among several other preset pages in the HotJava Help menu.
The Java Developer's Kit
Before you dive into Java programming, you need to pick up a copy of the Java Developer's Kit (JDK for short). The JDK includes:
Why Include an "Upgrade Utility"?
There are actually two different versions of Java floating about in CyberSpace: the one that came with the HotJava browser and the one in the JDK. At the time the JDK was released, the Java language was "frozen" (no more major changes). However, there were some significant changes in Java between the release of HotJava and the introduction of the JDK.
To make the transition from 1.0 Alpha 3 (the HotJava version) to 1.0 beta (the JDK version), the JDK includes a utility to help convert old applets.
- A compiler to turn your applet program code into bytecode, the language the interpreter understands.
- An interpreter that runs your applets, one line at a time.
- An applet viewer for testing and running applets.
- A debugger to help track down problems in your applet code.
- A collection of example applets to play with.
- A huge collection of documentation files, in browser (HTML) format.
- An upgrade utility to convert old applets to the new standard.
In a nutshell, the JDK has everything the budding Java programmer needs to start.
What? How much does it cost? Put your checkbook away; the JDK is available free off the Internet. Simply fire up your browser (any browser will do) and point it at the Sun Java Homesite at:
Browser your way into the Developers section and you'll find the links for downloading the JDK for your machine. The file you'll be downloading is rather large (4M or more), so once you start the download process, go get a cup of coffee.
Once the file has been downloaded,
An .EXE File That Unpacks Itself?
This is called a self-extracting archive and needs only to be "run" by you. In Windows, you can choose File, Run from the Program Manager, or in Windows 95 choose Run from the Start Menu on the taskbar. From there, the file will decompress and install itself.
- Move it to the root of the drive where you want to install the JDK (C:\ for example). Make sure that you have at least 5 Megabytes of free space on the disk before going to the next step (that's 5 Megabytes after you copy the file to it).
- Run the file (either from a DOS window, File, Run from Windows NT, or the Run option after pressing Windows 95's Start button).
The file will automatically unpack itself, create a \java directory and a bunch of subdirectories, and copy a ton of files into these new directories. When all this is done, you can delete the original file (or store it in a safe place, if you like to keep a copy around
just in case).
Where Are the Manuals?
One of the nice things about the JDK is the existence of very thorough documentation. What documentation, you ask? Well, if you fire up your browser and open the \java\progguide\index.html file, you'll be introduced to one of the nicest documentation collections on Java (and the JDK) available:
JDK online documentation.
Now, scroll down a bit. Below the Getting Started heading, you'll find Duke, the Java mascot (a cute little guy who looks amazingly like the Starfleet insignia)
and he's waving at you! You guessed it. Duke's a Java applet!
Duke waves a friendly greeting. Resist the temptation to wave back!
All of the documentation that comes with the JDK is available in HTML format, so you might want to browse around a bit right now before going further in the book. One point, though: not all the HTML files in the JDK are linked to the other files. The JDK contains a collection of document sets. You can simply list all of the JDK help files with the File Manager or the Windows Explorer, and double-click on any of the HTML files you like. They'll then be opened into your browser.
Back from your local surfing? Good! Take a quick detour into HTML and see how Java applets connect to Web pages: the <APPLET> tag.
Introducing the <APPLET> Tag
The <APPLET> tag's structure is relatively simple. Actually, there are two tags that work together: APPLET and PARAMS. The <APPLET> tag looks like this:
<APPLET CODE="appletFile" WIDTH=pixels HEIGHT=pixels>
<!- Alternate HTML code goes here ->
The CODE attribute identifies the applet program and is a file with a ".class" extension located in the same directory as the HTML document. To make your page do something for those poor souls who don't have a Java-enabled browser, you can place additional HTML statements inside the <APPLET> tag. Java browsers will ignore these statements; non-Java browsers will ignore the <APPLET> tag (which works out quite well, don't you think?).
The CODE Attribute Must Be Relative
Unlike most other HTML tags and attributes that use URLs, the CODE attribute cannot be an absolute URL; it must point to something relative to the current directory the HTML files are in.
The WIDTH and HEIGHT attributes define the size of the applet on the page. An applet's size is specified in pixels.
Additionally, there are several other optional attributes that you can add to the <APPLET> tag to modify the behavior of the tag.
The CODEBASE attribute lets you specify a URL (directory path) where your applet .class files are located (if they're in a different place from your HTML documents). As previously mentioned, if you don't specify CODEBASE, the applets are assumed to be in the same directory as the HTML files.
If, for some reason, a browser understands the <APPLET> tag but can't run applets (for example, a browser that's being upgraded to handle Java but isn't quite there yet), you can specify some text to display with the ALT attribute. This is similar to the ALT attribute of the <IMG> tag, which displays the specified text string on browsers that can't show graphics (text-based browsers).
Just like the <IMG> tag attribute of the same name, ALIGN controls how the applet is lined up. Acceptable values of the attribute are LEFT, RIGHT, TOP, TEXTTOP, MIDDLE, ABSMIDDLE, BASELINE, BOTTOM, and ABSBOTTOM.
VSPACE and HSPACE
These attributes also behave the same way their <IMG> tag counterparts do: they specify the amount of space (in pixels) to leave above and below (VSPACE) and on each side (HSPACE) of the applet.
Passing Applet Parameters
Those of you who remember the days of DOS (those glory days before Windows, X Window, and the Macintosh made pictures out of everything) might recall that you could run some programs by typing the name of the program and adding additional information on the command line before pressing the Enter key. These additional pieces of information were called parameters.
You Can Spot Old (pre-JDK) Applets Easily
If, as you surf the Web, you come across an applet that's implemented in HTML using an <APP> tag instead of the JDK <APPLET> tag, you've found an old applet.
It's possible to create applets that can take parameters (in the case of applets, parameters are additional options that are unique to the applet and aren't covered by other <APPLET> attributes). Passing parameter values to an applet is done with the <PARAM> tag. <PARAM> tags are placed after the <APPLET> tag and before any of the "alternate HTML" statements and the </APPLET> tag. The <PARAM> tag looks like this:
<PARAM NAME=appletAttribute VALUE=value>
where NAME identifies the particular attribute and VALUE identifies its value. If you have more than one parameter, you use more than one <PARAM> tag. Inside the applet code, you access the values passed in with <PARAM> with the getParameter() function.
Java with Class
Classes are at the heart of what makes Java an object-oriented language. A class is like a library of objects, all of which inherit certain characteristics from the class itself. "Class" is a hierarchical concept-one class could be a subset of a higher-level class. In Java there is a term "abstract class," which is a parental class containing many child classes. In this case, though, the abstract class is never directly used-it is defined solely to provide the "genes" for its children classes.
If any of this seems confusing, that's because it is. That is why there are entire books written about object-oriented programming and Java in particular. Now, though, at least you have a taste for what object-orientedness is about-modules that are part of larger modules, which are part of yet larger modules.
Command Line? I'm Running a Mac!
If you're running a Macintosh (or a PowerPC), you're probably a bit confused by the reference to a "command line": the Mac operating system doesn't have one. Does this mean that Mac users are out of luck when it comes to creating Java applets?
Sadly, at this moment, the answer is, "Yes, unless you're willing to spend some money." There are no environments or library sets (source code) for Java compiling on the Mac available for free off the Internet. Several companies (Symantec at http://www.symantec.com/ and Metrowerks at http://www.metrowerks.com/ to name two) have announced that they will be integrating Java applet creation into their compilers
but their products aren't available yet.
Natural Intelligence, however, does market a package called Roaster that gives Mac users the ability to compile and develop Java applets on the Mac from inside the Mac OS. For more information, check out Natural's web page at http://www.natural.com/.
C++ is a very common object-oriented programming language that also revolves around these class concepts. One major difference between C++ and Java, in terms of how they deal with classes, is that Java programs can more easily withstand changes in class definitions without breaking any programs based on the previous definitions. In C++, on the other hand, if you change any class definitions, every program written using the previous definitions needs to be recompiled. For this reason, Sun calls Java "dynamic."
/* HelloWorld.java: Hello World...Java Style */
public class HelloWorld
public static void main(String args)
Remember to save this file as HelloWorld.java. Now, once you type that in, you need to compile the applet. Get yourself to a DOS prompt (you'll need to open a DOS window to do this) and type:
Finally, you can run your new applet right from DOS by typing:
Congratulations! You've just taken another big step: Java programming!
The Least You Need To Know
Java programs are called applets. Once a program (applet) has been written, it must be compiled before it can be incorporated into an HTML document. After compiling the applet, it's linked into a Web page through the <APPLET> tag.
For comments or technical support for our books and software, select Talk to Us.
To order books, call us at 800-716-0044 or 317-228-4366.
© 1996, QUE Corporation, an imprint of Macmillan Publishing USA, a Simon & Schuster Company.