Sams Teach Yourself Java 2 in 24 Hours
- Table of Contents
- Copyright
- About the Author
- About the Technical Editor
- Acknowledgments
- We Want to Hear from You!
- Reader Services
- Introduction
- Hour 1. Becoming a Programmer
- Hour 2. Writing Your First Program
- Hour 3. Vacationing in Java
- Hour 4. Understanding How Java Programs Work
- Part II: Learning the Basics of Programming
- Hour 5. Storing and Changing Information in a Program
- Hour 6. Using Strings to Communicate
- Hour 7. Using Conditional Tests to Make Decisions
- Hour 8. Repeating an Action with Loops
- Part III: Working with Information in New Ways
- Hour 9. Storing Information with Arrays
- Hour 10. Creating Your First Object
- Hour 11. Describing What Your Object Is Like
- Hour 12. Making the Most of Existing Objects
- Part IV: Programming a Graphical User Interface
- Hour 13. Building a Simple User Interface
- Hour 14. Laying Out a User Interface
- Hour 15. Responding to User Input
- Hour 16. Building a Complex User Interface
- Part V: Creating Multimedia Programs
- Hour 17. Creating Interactive Web Programs
- Hour 18. Handling Errors in a Program
- Hour 19. Creating a Threaded Program
- Hour 20. Reading and Writing Files
- Part VI: Creating Multimedia Programs
- Hour 21. Using Fonts and Color
- Hour 22. Playing Sound Files
- Hour 23. Working with Graphics
- Hour 24. Creating Animation
- Part VII: Appendixes
- Appendix A. Tackling New Features of Java 2 Version 1.4
- Appendix B. Using the Java 2 Software Development Kit
- Appendix C. Programming with the Java 2 Software Development Kit
- Appendix D. Using Sun ONE Studio
- Appendix E. Where to Go from Here: Java Resources
- Appendix F. This Book's Web Site
Java Archives
Applets that use sound or graphics often require several different files to be loaded before the program can run successfully. The workshop you will undertake during this hour includes 12 sound files.
Because of the way the World Wide Web functions, each of these files requires its own connection between the Web server offering the applet and the user running the program during a Web surfing session. The same is true of World Wide Web pages—each graphic requires its own connection.
One way to reduce the time required to load an applet is to package all its files into a single archive file. The Software Development Kit includes a command-line tool called jar that enables Java archive files to be created, examined, and unpackaged.
Java archive files have the .JAR file extension. The jar tool is used to package a group of files into a single .JAR file. If you have created a SoundFX applet that includes 10 WAV files that have names beginning with fx, the following command could be entered at a command-line to package the applet and WAV files into a single JAR archive:
jar cf SoundFX.jar SoundFX.class fx*.wav
No file folders are included with the filenames in this example, so it must be used in the same folder that contains SoundFX.class and the WAV files used by the applet.
The jar tool takes the following arguments when it is run:
- Options determining what jar should do (cf in the previous example)
- The name of the archive (SoundFX.jar)
- The file (or files) to be archived, with each filename separated by a space (SoundFX.class and fx*.wav)
Wildcards can be used when specifying filenames, so fx*.wav refers to any file with a name that begins with fx and ends with .wav. You can also list each file separately.
After you have created a Java archive containing all files used in an applet, the ARCHIVE attribute of the <APPLET> tag is used to associate this archive with the applet.
Continuing the example of the SoundFX applet, you could include its JAR archive on a Web page with the following HTML code:
<applet code="SoundFX.class" archive="SoundFX.jar" height=80 width=140> </applet>
When the ARCHIVE attribute is used with an applet, the applet's main class file should be included in the specified archive file. The CODE attribute must still be used because the name of the main class file must still be identified, whether it's loaded directly or from an archive.
Java archives are a way to speed up the presentation of applets and to organize other multifile projects. All JavaBeans components are packaged into .JAR files, making them more self-contained and keeping them in a single file that's easier to make available for use in other Java programs.
Workshop: Making Your Computer Talk to You | Next Section

Account Sign In
View your cart