Home > Articles

  • Print
  • + Share This
This chapter is from the book

Compiling and Running Swing Programs

This section explains how to compile and run a Swing application. The compilation instructions work for all Swing programs—applets, as well as applications. The following are the steps you need to follow:

  1. Install 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.

If you're new to writing and compiling programs that use the Java platform, read The Java Tutorial trail "Getting Started" first; it's available online and on the CD at: JavaTutorial/getStarted/index.html. That trail also explains common errors that users may encounter.

Install the Latest Release of the Java 2 Platform

You can download the latest release of the J2SE SDK for free from http://java.sun.com/j2se. Version 1.4.2 of the J2SE SDK is included on the CD that accompanies this book.

Create a Program That Uses Swing Components


You can use a simple program we provide, called HelloWorldSwing, that brings up the GUI shown in Figure 1. The program is in a single file, HelloWorldSwing.java.4 When you save this file, you must match the spelling and capitalization of its name exactly. If you prefer to type in the file's code yourself, you can find the full code starting on page 12 in Chapter 2.

01fig01.gifFigure 1. The HelloWorldSwing application.

Compile the Program

Your next step is to compile the program. Here's an example of compiling HelloWorldSwing.java:

javac HelloWorldSwing.java

If you can't compile, make sure you're using the compiler in a recent release of the Java platform, such as 1.4.2 included on this book's CD. Once you've updated your SDK, you should be able to use the programs in this book without changes. Another common mistake is installing the Java Runtime Environment (JRE) and not the full Software Development Kit (SDK) needed to compile these programs. Refer to the "Getting Started" trail of The Java Tutorial to help you solve any compiling problems you encounter.5 Another installation troubleshooting guide for the Java 2 platform is online at: http://servlet.java.sun.com/help/installation/.

Run the Program

After you compile the program successfully, you can run it. Assuming that your program uses a standard look and feel—such as the Java, Windows, or GTK+ look and feel—you can use the interpreter to run the program without adding anything to your class path. For example:

java HelloWorldSwing

For programs that use a nonstandard look and feel or any other nonstandard code package, you must make sure that the necessary classes are in the class path. For example:


java -classpath.:/home/me/lnfdir/newlnf.jar HelloWorldSwing

Microsoft Windows

java -classpath .;C:\java\lnfdir\newlnf.jar HelloWorldSwing

Alternatively, you can launch your program from a Web browser using Java Web Start.

Running Programs Using Java Web Start

Java Web Start is a technology that simplifies the distribution of applications. With a click on a Web page link, you can launch full-featured applications without a complicated download and installation process. The first time you launch an application, Java Web Start automatically downloads all necessary files. It then caches them on your computer so the application can be quickly relaunched from a desktop shortcut or from a Web browser. Java Web Start checks for updates each time you run a remote application and downloads any updated files automatically.

In 1.4.1 and later releases of the Java platform, Java Web Start is shipped as part of the platform. So, if you have J2SE or JRE v1.4.1 or higher, you already have Java Web Start installed. When you install a version of the Java platform with Java Web Start, on most platforms you'll see a Java Web Start shortcut on your desktop. (See Figure 2.)

01fig02.jpgFigure 2. The Java Web Start desktop shortcut.

To test whether your browser can launch an application using Java Web Start, point it to one of the following places and click the "Launch JavaTM Web Start" link:

You should see a Java Web Start splash screen. (See Figure 3.)

01fig03.jpgFigure 3. The Java Web Start splash screen.

The application is then downloaded, and you should see what's shown in Figure 4.

01fig04.gifFigure 4. The HelloJWSYou can find the HelloJWS.java source files here: JavaTutorial/uiswing/14start/example-1dot4/index.html#HelloJWS. application launched from a Web browser using Java Web Start.


If you're unable to launch this application, you should check the Java Web Start Troubleshooting (page 733) guide in the Appendix.

  • + Share This
  • 🔖 Save To Your Account