Getting Java installed so we can have some serious fun. <br><em>Plus:</em> Answers to your questions
This month we start off with a discussion of some of the common problems that people face when trying to get Java to work for them. After that we jump into some reader mail.
Installation
Some people are having difficulty installing and running Java programs. Given the growing variety of Java development tools, this issue is becoming much more complex. Now there are ports of Sunโs Java Development Kit (JDK) to a wide number of platforms as well as a growing contingent of proprietary Java development tool options. Each of these tools on each platform has its own idiosyncrasies and bugs.
RTFM
By far the most common problem is that people donโt read the documentation that comes with the tools. Although it seems like such a simple thing, there is something about documentation that repels us. Itโs best to start by skimming the installation and setup documentation once to get an idea of whatโs going on and then go back through it, carefully following the instructions.
Some of the documentation for the various toolsets are platform-specific, so make sure you are reading the sections appropriate to your situation. For instance, Sunโs JDK tool reference pages are segregated into Solaris and Microsoft Windows sections.
In addition to reading the printed documentation, be sure to read any notes or README files that come with the toolset. All of the Java vendors have Web pages with lots of potentially helpful information, including patches/updates to the tools themselves and additional documentation like Frequently Asked Questions (and answers) files.
CLASSPATH
Setting the
CLASSPATH
environment variable is the next most common problem. It is spelled out in the documentation of the various toolkits, but it still seems to throw people for a loop.
The purpose of the CLASSPATH variable is to let the various tools know where to find the Java class files. It is a list of directories in which the tools should start their search for the class files.
Under Microsoft Windows platforms the list elements are separated by semicolons like so:
set CLASSPATH=.;c:codejava;c:javalib
That would tell, for example, the Java compiler to look first in the current directory, then in my local Java coding directory, and finally in the JDK library directory.
Under Unix-like platforms, the list elements are separated by colons like so:
setenv CLASSPATH .:/home/john/java/code:/usr/local/java/classes/classes.zip
This tells it to look first in the current directory, then in my local Java coding directory, and finally in the JDK classes archive.
A particularly annoying problem with the CLASSPATH environment variable is that itโs used by both the development tools and at least some of the Java-enabled browsers (such as Netscapeโs Navigator). That can make for an unfortunate clash of class files. My solution is to wrap my use of the tools and the browsers inside shell scripts and batch files. These wrappers set the CLASSPATH as appropriate for the program it then invokes.
The wrapper solution also makes it a lot simpler to use multiple toolsets and browsers without them stomping all over each other. Typically, I have two different versions of Netscape Navigator (v.2.0 and v.3.0 beta) along with a couple of versions of the JDK and appletviewer (an older, more โstableโ version along with the latest, cutting-edge version).
Applets
A small contingent of users asked how to add their applets to their home pages. This is easy to do.
Read the documentation for your Web server and find out where to put your home page. On my Linux system, itโs /home/httpd/html. Place the .class files that were generated when you compiled your applet into this directory. Make sure that you set the permissions such that everyone can read the file.
Load your home page inside an ASCII editor. Donโt use a word processor because it will save your file in some bizarre format. If you have an HTML editor, you could use that instead.
Now, decide on a place in the file to add your applet. Applets are distinguished by the APPLET HTML tag. To include a simple applet named MyApplet, which takes no parameters, add the following to your Web page:
<applet code=MyApplet.class width=250 height=250></applet>
For an applet that takes parameters, you can add PARAM HTML tags inside the APPLET tags like so:
<applet code=MyApplet.class width=250 height=250>
<param name=ArgName1 value=ArgValue1>
<param name=ArgName2 value=ArgValue2>
</applet>
where ArgName1 is the name of the first parameter and ArgValue1 is its value.
You can put your class files in a different directory from your Web page. However, you must tell the server where to find those files by using the optional CODEBASE field of the APPLET tag:
<applet codebase=classDir code=MyApplet.class width=250 height=250>
</applet>
where classDir is the directory to look in for the class files.
After you have edited your file, save it. Now launch a Web browser that supports Java and load your home page. The applet should then be loaded and executed, assuming you did not do anything naughty.
When it comes to learning HTML, a key tactic is to learn from what others have done. Whenever you come to a Web page that does something interesting, view the source for that page. All browsers worth anything have some sort of View mode to allow you to read the raw HTML page.
Books
Another key resource for learning Java is the many books that already have been written about it. Some are definitely better than others in helping to get people started with Java.
I like the SunSoft Press Java Series (http://www.prenhall.com/~java_sun) as a tool for getting started with Java. It comes with a CD-ROM that has the Sun JDK for Windows 95 and NT, Solaris, and the Mac. It also has all of the code from the book as well as a copy of Symantecโs Cafe Lite development environment for Windows 95.
The best desktop reference for Java is currently OโReillyโs Java in a Nutshell (http://www.ora.com/catalog/books/javanut/) by David Flanagan.
The best book to give your boss to justify all the time you are spending learning Java and surfing the Web and to anyone else looking for a lucid overview is Manning Publicationโs Making Sense of Java (http://www.browsebooks.com/Simpson/) by Simpson, Mitchell, Christeson, Zaidi, and Levine. (Of course, as one of this bookโs authors, I am a wee bit biased.)
Mail grab bag
Letโs switch gears and answer some specific reader questions.
Variable line width
Ivana Cuozzo asks: How is it possible to draw a line with a specified thickness?
Use the java.awt.Graphics methods drawRect() and fillRect() instead of the drawLine() method. Note that this is much slower than drawing a line but you can get any thickness that you want.
Mouse cursors
Vance Allen asks: Ho do I change the appearance of the mouse cursor when it enters an applet?
The java.awt.Frame class defines a number of standard cursors and has methods for getting and setting the cursor. Subclass the Frame class and override the handlers for the events you are interested in. Check out the mouseEnter() and gotFocus() methods for changing the cursor on entry and mouseExit() and lostFocus() methods for cursor exit.
Of course, that solution does not work if you donโt use a Frame.
Java-aware editors
Kirrin Jones asks: Where can I download a copy of a good Java editor?
Well, that is a question fraught with personal bias. I use GNU Emacs for most of my writing tasks. For Java, I use Barry Warsawโs excellent C/C++/Objective-C/Java Mode (ftp://ftp.python.org/pub/emacs/cc-mode.tar.gz) package.
Most of the commercial Java development environments have some sort of Java-aware editor. The standalone commercial editors are adding Java support if they have not done so already. I donโt know of any other free editors that have Java support.
Callbacks
Gene Callahan asks: โฆI want to be able to say, โHereโs a <pointer in C> to a function returning in and taking two args. Call it with these params when you see this name.โ โ and I canโt see how.
Check out Java Tip 10 (http://www.javaworld.com/javatips/jw-javatip10.html). It discusses the use of Javaโs interface mechanism to provide for callbacks. Itโs not exactly the same as if Java had method references, but it works nicely in its own way.
Inter-applet communication
Cees van Huet asks: Is it possible that two applets in two different HTML frames can communicate with each other?
Indeed, it is possible. One solution was presented in Java Tip 3 (http://www.javaworld.com/javatips/jw-javatip3.html).
paint() versus update()
Tim Klocek asks whether the update() method should call the paint() method or vice versa.
The paint() method is responsible for the actual painting of the component. The update() is a communication from elsewhere instructing the component to update itself. Therefore, the update() method for most things should call the paint() method.
Bye bye
Well, thatโs it for now. Get Java installed so you can start percolating too!
Subsisting on caffeine, sugar, and too little sleep, John D. Mitchell has been consulting for most of the last nine years, and has developed PDA software in OO assembly language at Geoworks. He funds his Java addiction by writing compilers, Tcl/Tk, C++, and Java systems. He coauthored the hot new Java book Making Sense of Java and is currently developing a Java compiler.


