< Day Day Up > |
This section explains how to compile and run a Swing application. The compilation instructions work for all Swing programsapplets, as well as applications. The following are the steps you need to follow:
Note: 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 PlatformYou 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
Figure 1. The HelloWorldSwing application.
Compile the ProgramYour 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 ProgramAfter you compile the program successfully, you can run it. Assuming that your program uses a standard look and feelsuch as the Java, Windows, or GTK+ look and feelyou 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: Solarisjava -classpath.:/home/me/lnfdir/newlnf.jar HelloWorldSwing Microsoft Windowsjava -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 StartJava 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.) Figure 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 Java TM Web Start" link:
You should see a Java Web Start splash screen. (See Figure 3.) Figure 3. The Java Web Start splash screen.
The application is then downloaded, and you should see what's shown in Figure 4. Figure 4. The HelloJWS [6] 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. |
< Day Day Up > |