Compiling Java

To be able to compile and load the Java tutorial examples, you need to tell Java where to find the files. When you compile a Java source (".java") file, it produces a class (".class") file, which is what it loads to run the class. When a reference is made to an class that is not loaded into the Java Virtual Machine (JVM), the Java class loader searches for the file relative to its classpath.

Files must be located relative to one of the entries in the classpath, The code we prepared for this tutorial is in the tutorial.tcltk98 package, so the Java files need to be in the directory tutorial/tcltk98/ relative to one of the entries in the classpath.

The simplest way to set the classpath is to set the CLASSPATH environment variable. On Solaris, you might go:

  setenv CLASSPATH .:/users/johnr/java
(It is always handy to have the current directory "." in the classpath, as it allows you to create test classes wherever you are without having to worry about packages.) Now, this classpath will only work if none of our Java code reference the Tcl Blend java code in tcl.lang. If it does (which it will in a couple of pages, we need to make sure that the tclblend.jar thet came with Tcl Blend is in the classpath as well. For example:
  setenv CLASSPATH .:/users/johnr/java:/opt/tclBlend1.0/tclblend.jar
(As far as the Java class loader is concerned, a .jar or .zip file is the same as a directory containing the the files, and .jar and .zip files are generally used to distribute compiled Java packages.)

On Windows NT, you can use the System control panel to set the CLASSPATH environment variable. On my installation, I set it to:

  .;c:\java;c:\tools\tcl\lib\tclblend1.0\tclblend.jar

Once you have done that, change to the tutorial/tcltk98 directory relative to the directory you just set in the classpath and compile the Hello class:

  > javac Hello.java
To check that you can run Java programs, type:
  > java tutorial.tcltk98.Hello
This should print "Hello, world!". If not, check that your classpath is set correctly, and that javac and java are in your path. (If your classpath includes the Java core libraries classes.zip, make sure that the version corresponds to the version of javac in your PATH -- a frustrating error you may get if you work with different versions of Java).

More about CLASSPATH

(You may prefer to skip this section and come back to it later.)

The classpath is a little tricky, and for any substantial work in Java we generally prefer not to set the environment variable CLASSPATH. Why? Because we've been bitten often enough by programs getting upset about odd values of CLASSPATH that we prefer to set the CLASSPATH individually for each program or project. Some examples of typical classpath problems:

  1. If you set CLASSPATH to include the Netscape 4.04 libraries, and then start Netscape 4.5, you won't be able to run Java applets.
  2. If you try to run Jacl and you inadvertently have the Tcl Blend JAR file in you classpath, you will get run-time errors.
  3. If you work with multiple versions of Java (it seems like there is always a beta of the next version already out!), having the classpath hardwired into environment variables can make life more difficult.

Here are some ways of avoiding (permanently) setting the CLASSPATH environment variable:

  1. Set it in makefiles. A sample rule from one of our makefiles is:
    .SUFFIXES: .class .java
    .java.class:
            rm -f `basename $< .java`.class
            CLASSPATH=$(CLASSPATH)$(AUXCLASSPATH) $(JAVAC) $(JFLAGS) $<
    
  2. Use the -classpath option to javac and java. We could, for example, have written above:
      javac -classpath .:/users/johnr/java:/opt/jdk1.1.6/lib/classes.zip Hello.java
    
    Bogon alert!
    If you set the CLASSPATH environment variable, the Java tools will know where to find the JDK libraries. If you use the -classpath option, however, you need to explicitly tell the tools where to find their libraries. In JDK versions up to and including JDK1.2beta3, the libraries are in lib/classes.zip relative to the installation directory; in JDK1.2beta4, they are in jre/lib/rt.jar relative to the installation directory.
To run Java applications, you can use either of these methods, and figure the classpath out in a wrapper script. (If you installed Jacl on Windows, take a look at the jacl.bat file, which does exactly this.)

Having said all that, provided you understand that you'll need to do more work with setting up classpaths if you do more work in Java, all of this tutorial can be run quite easily by setting the CLASSPATH variable by hand, and we'll assume that you can figure out what to do when we say "add so-and-so to your classpath."