Programming Java: Looking for Files

You try to compile the Java program in the example listing. The Java compiler pokes through the code and stumbles upon some missing pieces. First there’s this thing called an ArtFrame. Then you have this Drawing business. The listing defines a class named ShowFrame, not ArtFrame or Drawing. So where does the compiler go for information about the ArtFrame and Drawing classes?

If you stop to think about it, the problem can be daunting. Should the compiler go searching all over your hard drive for files named ArtFrame.java or Drawing.class? How large is your new hard drive? 500GB? 750GB? 6,000,000GB?

And what about references to files on network drives? The search space is potentially unlimited. What if the compiler eventually resolves all these issues? Then you try to run your code, and the Java Virtual Machine (JVM) starts searching all over again.

To tame this problem, Java defines something called a CLASSPATH. The CLASSPATH is a list of places where the compiler and the JVM look for code. There are several ways to set a CLASSPATH. Some programmers create a new CLASSPATH each time they run a Java program. Others create a system-wide CLASSPATH variable.

If you’re familiar with the PATH variable on Windows and UNIX computers, you may already know how this stuff works.

One way or another, the compiler and the JVM need a list of places to look for code. Without such a list, these Java tools don’t look anywhere. They don’t find classes like ArtFrame or Drawing. You get a cannot find symbol message or a NoClassDefFoundError message, and you’re very unhappy.

To make this business about access modifiers clear, you need an example. In many Java examples, almost everything is public. With public access, you don’t have to worry about who can use what.

The code for this first example comes in several parts. The first part, which is in the listing, displays an ArtFrame. On the face of the ArtFrame is a Drawing. If all the right pieces are in place, running the code displays a window like the one shown in the figure.

image0.jpg
import com.burdbrain.drawings.Drawing;
import com.burdbrain.frames.ArtFrame;
class ShowFrame {
    public static void main(String args[]) {
        ArtFrame artFrame = new ArtFrame(new Drawing());
        artFrame.setSize(200, 100);
        artFrame.setVisible(true);
    }
}
  • Add a Comment
  • Print
  • Share
blog comments powered by Disqus
Advertisement

Inside Dummies.com

Dummies.com Sweepstakes

Win $500. Easy.