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

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

Compiling and Running Swing Programs (JDK 1.2)

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

Download the Latest JDK 1.2 Release

You can download JDK 1.2 for free.

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. 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. Compiling a Swing program with JDK 1.2 is simple, since the Swing packages are part of JDK 1.2. Here is an example:
javac -deprecation SwingApplication.java
If you can't compile SwingApplication.java, it's probably either because you're using a JDK 1.1 instead of a JDK 1.2 compiler, or because you're using a beta release of JDK 1.2. Once you update to the most recent JDK 1.2 release, you should be able to use the programs in this trail without change. If you must use JDK 1.2 Beta 4, for some reason, 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

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

Assuming that your program uses a standard look and feel -- such as the Java Look & Feel, Windows Look & Feel, or CDE/Motif Look & Feel -- you can use the JDK 1.2 interpreter to run the program without adding anything to your class path. For example:

java SwingApplication
If you use a nonstandard look and feel, you must make sure that its package is included in the class path. For example:
Solaris:
    java -classpath .:/home/me/lnfdir/newlnf.jar SwingApplication
Win32:
    java -classpath .;C:\java\lnfdir\newlnf.jar SwingApplication

Note:  Don't include the JDK 1.2 classes in the class path. The 1.2 interpreter finds them automatically.

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