Que Logo

Chapter 22

Stepping Up to Straight Java



In This Chapter

JavaScript's Big Brother


If you've made it this far in the book… good for you! As you've seen, JavaScript is a very powerful language for extending the capabilities of your World Wide Web pages. No doubt, as you've surfed around checking out other authors' uses of JavaScript (okay, and peeked at their source code to get more ideas), you've encountered talk about a thing called Java. If you're the curious sort, you're probably wondering just what Java is… and whether it's worth looking into.
Java is a full-blown programming language, designed for the purpose of providing "truly interactive" content through the World Wide Web; Java is the language on which JavaScript is based. Developed by Sun Microsystems, Java attempts to bridge the "multiple platform" stew that exists on the Internet by creating applications (called applets) that will run on any computer of any type… 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.

Because Java is a programming language (like Pascal, BASIC, C, or C++), to work with it, you have to write a program. Java programs are similar to JavaScript scripts, except that they are much more extensive and include a lot of additional material necessary to successfully complete the next phase of Java development: compiling. Compiling an applet means to take the program that you've written and run a special program on it that converts it from "near-human-readable" format to "Java-machine-readable" format (this is called bytecode)--so that a Java browser can then run the applet.
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.
In this chapter, you're not going to dig deep into the bowels of Java. Like JavaScript, Java is still evolving, and by the time you're holding this book in your hands, there will no doubt be newer tools for creating Java applets. Instead, you'll take a quick glance at what it takes to get an applet put together and how to link applets into your HTML pages.
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.

Java Browsing


As with JavaScript, you need to use a Java-capable browser to run Java applets. At the time of this writing, there are two Java-capable browsers: HotJava from Sun and Netscape Navigator 2.0 (32-bit, although the 16-bit version is rumored to have Java support by the time you read this). It is likely that the other major browsers, such as Microsoft's Internet Explorer, will gain Java capabilities in the near future. In all cases, browsers for 16-bit platforms such as Windows 3.1 are less likely to see Java.
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
http://java.sun.com
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.

The HotJava Web browser.


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



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.
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:
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:
http://java.sun.com/
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.

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.
Once the file has been downloaded,
  1. 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).
  2. 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


Just as JavaScript scripts are identified by their own custom HTML tag (the <SCRIPT> tag you met back in Chapter 4), Java applets are included in Web pages through their own custom tag: the <APPLET> tag. As with other HTML tags, browsers that don't support Java will simply ignore the tag, so you can safely create Java-enabled pages that don't limit their viewing to Java-enabled users (however, you will need to do a little more HTML to provide those who haven't caught up with the times something to look at).
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 ->
</APPLET>


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 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 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.

CODEBASE


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.

ALT


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).

NAME


The NAME attribute does the same thing for <APPLET> that it does for other HTML tags: it gives the applet a name, allowing other applets on the same page to locate and talk with each other (much the same way JavaScript uses NAME to find and talk to different frames or fields in forms).

ALIGN


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



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.
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.
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


Java and JavaScript share many things, one of which being "objects" or things that give you access to different parts of a Web page. Java extends this by allowing you to create your own types of objects, as well as offering up a larger collection of "canned" objects. Predefined objects in Java are bundled together in "classes" or collections of objects that share a common purpose.
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.

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/.
Keep in mind, however, that even if you were to start writing your own Java applets, there still isn't a Web browser available for the Mac that supports Java (that, too, is "coming soon"), which is another good reason to stick with JavaScript: it runs on Mac, IBM, and UNIX right now!
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.
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."

Hello World!


Let's close with a quick look at making your own Java applet. As with JavaScript, you'll use the traditional "Hello World" program. The Java program looks like this:

/* HelloWorld.java: Hello World...Java Style */
public class HelloWorld
{
 public static void main(String args[])
 {
 System.out.println("Hello World!");
 }
}

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:

javac HelloWorld.java

Finally, you can run your new applet right from DOS by typing:

java HelloWorld

Congratulations! You've just taken another big step: Java programming!

The Least You Need To Know


You learned that Java is a programming language, similar to C++ or Delphi that is built around objects. Java is similar to JavaScript, but Java also allows you to create your own object and class types, extending the language to do what you need it to.
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.

Previous Chapter

Next Chapter


Beginning of ChapterTable of ContentsBook Home PageQue Home Page


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.