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 (Java 2 Platform)

Here are the steps for compiling and running your first Swing program with the Java 2 SDK, v 1.2 or 1.3:
  1. Download the latest release of the Java 2 Platform, 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 Release of the Java 2 Platform

Two versions of the Java 2 Platform are available, both for free. The first is the Java 2 SDK, Standard Edition, v 1.2(outside of the tutorial), which is sometimes called "v 1.2" for short (or, incorrectly, "JDK 1.2"). The second is Java 2 SDK, Standard Edition, v 1.3(outside of the tutorial), which is sometimes called simply "v 1.3".

For information about the differences between 1.2 and 1.3, see the Java 2 SDK v 1.3 documentation(outside of the tutorial).

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. Compiling a Swing program with one of the Java 2 SDKs is simple, since the Swing packages are part of the Standard Edition of the Java 2 Platform. 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 compiler instead of a v 1.2 or 1.3 compiler, or because you're using a beta release of v 1.2. Once you update a more recent release of the Java 2 Platform, you should be able to use the programs in this trail without change. See Swing Package Names for information about the package names that were used in the 1.2 beta releases.

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 and feel, Windows look and feel, or CDE/Motif look and feel -- you can use the v 1.2 or 1.3 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
Microsoft Windows:
    java -classpath .;C:\java\lnfdir\newlnf.jar SwingApplication

Note:  Don't include the core classes in the class path. The v 1.2 and 1.3 interpreters find them automatically.

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.