The JavaTM Tutorial
Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Trail: Creating a GUI with JFC/Swing
Lesson: Getting Started with Swing

Compiling and Running Swing Programs (JDK 1.1)

Here are the steps for compiling and running your first Swing program with JDK 1.1 and JFC/Swing:
  1. Download the latest JDK 1.1 release, if you haven't already done so.
  2. Download the latest JFC 1.1 release.
  3. Create a program that uses Swing components.
  4. Compile the program.
  5. Run the program.

Download the Latest JDK 1.1 Release

You can download the reference implementation of JDK 1.1(outside of the tutorial) for free from java.sun.com. Just go to the appropriate page for your platform -- Solaris(outside of the tutorial) or Win32(outside of the tutorial) .

Download the Latest JFC/Swing Release

You can download the latest JFC 1.1 release from the JFC home page(outside of the tutorial) .This trail describes the Swing 1.1 version of JFC 1.1.

Create a Program That Uses Swing Components

You can use a simple program we provide, called SwingApplication. Please download and save this file: SwingApplication.java(in a .java source file). The spelling and capitalization of the file's name must be exactly "SwingApplication.java".

Compile a Program That Uses Swing Components

Your next step is to compile the program. Here's a general explanation of how to compile a Swing application with JDK 1.1.
  1. Make a note of where your copy of the JFC 1.1 (Swing 1.1) release is installed. The Swing class archive file, swing.jar, is in the top directory of this release. You might want to create an environment variable called SWING_HOME that contains the path of the top directory of the JFC 1.1 release.


    Note:  Don't bother unarchiving swing.jar!

  2. Make a note of where your copy of the JDK release is installed. You'll need this to be able to find the proper versions of the JDK classes and interpreter. You might want to create an environment variable, JAVA_HOME, and set it to the top directory of the JDK release.

    The JDK classes are in the lib directory of the JDK release, in a file called classes.zip. Don't uncompress that file! The interpreter for the Java programming language is in the bin directory of the JDK release.

  3. Compile the application, specifying a class path that includes the swing.jar file, the JDK classes.zip file, and the directory containing the program's classes (usually "."). Be sure that the JDK classes.zip file and the compiler you use are from exactly the same release of the JDK!

The following example shows how to compile SwingApplication on a UNIX system. It assumes that you've set up the environment variables JAVA_HOME and SWING_HOME.

$JAVA_HOME/bin/javac
    -classpath .:$SWING_HOME/swing.jar:$JAVA_HOME/lib/classes.zip
    SwingApplication.java

If you choose not to use the environment variables, you might instead use a command like this:

javac
    -classpath .:/home/me/swing-1.1/swing.jar:/home/me/jdk1.1.7/lib/classes.zip
    SwingApplication.java
Here's an example of compiling on Win32:
%JAVA_HOME%\bin\javac
    -deprecation
    -classpath .;%SWING_HOME%\swing.jar;%JAVA_HOME%\lib\classes.zip
    SwingApplication.java
Here's an alternative that doesn't use environment variables:
javac
    -deprecation
    -classpath .;C:\java\swing-1.1\swing.jar;C:\java\jdk1.1.7\lib\classes.zip
    SwingApplication.java

Note:  If you can't compile SwingApplication.java, it's probably due either to having the wrong files in your class path or to using a version of JFC 1.1 that has old Swing API. You should be able to use the programs in this book without change once you update to the most recent JFC 1.1 release. Before Swing 1.1 Beta 3, the Swing API used different package names. Here is how to change SwingApplication.java to use the old package names:
//import javax.swing.*;       //comment out this line
import com.sun.java.swing.*;  //uncomment this line
See Swing Package Names for more information about differences in package names.

Run the Program

Once the program has successfully compiled, you can run it. This section tells you how to run an application. For instructions on running an applet, see Running Swing Applets.

Make sure that the interpreter's class path includes not only what you needed to compile the file, but also the archive file for the look and feel the program uses. The Java look and feel, which is the default, is in the swing.jar file. The Windows look and feel is in windows.jar, and the CDE/Motif look and feel is in motif.jar. You aren't limited to these look-and-feel options: you can use any look and feel designed for use with the Swing 1.1 API.

This application uses the Java look and feel, so you need only swing.jar in the class path. Thus, the command for running it is similar to the command for compiling it. Just substitute java for javac, and remove the .java suffix. For example, on UNIX:

java -classpath .:/home/me/swing-1.1/swing.jar:/home/me/jdk1.1.7/lib/classes.zip
     SwingApplication

Here's an example of running an application that uses the Windows look and feel:

%JAVA_HOME%\bin\java
    -classpath .;%SWING_HOME%\swing.jar;%JAVA_HOME%\lib\classes.zip;%SWING_HOME%\windows.jar
    SomeClass
While you're developing your application, you can simplify the class path by using swingall.jar, which includes all the classes in the JFC 1.1 release. So instead of putting swing.jar and windows.jar in your class path, for example, you can just put in swingall.jar.

Important:  Avoid using swingall.jar in your final application. It contains information used by builders, as well as more look-and-feel packages than a typical application uses. You can save space by simply using the swing.jar file plus any look-and-feel archives that you need.

Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Copyright 1995-2002 Sun Microsystems, Inc. All rights reserved.