Chapter 11. Packages and JAR Files


When you start to develop more complicated Java applications, you may end up with a large number of source code, bytecode, and support files. Java provides packages and Java Archive (JAR) files to help you organize and store your work. Packages are used to separate and organize a collection of classes and interfaces. One reason to place your files in a package is to avoid naming conflicts. Two classes can't share the same name unless they are placed in different packages. If you write a class with an obvious name such as Beam , you don't have to worry about whether that name is being used by some other library if you place the Beam class in a user -defined package.

Packages allow you to bundle collections of classes and interfaces with similar functions. It would be convenient , for instance, to bundle classes that deal with gas mixtures into a Gas package. If you needed a class that encapsulated a gas mixture, you would look for it in the Gas package. Packages also have access control implications. See Chapters 7, 8, and 9 for an explanation of how packages affect class, variable, and method access.

We've been using packages in this book although you probably weren't aware of it. If you don't specify a package in your source code, your program will be placed in an unnamed package that originates from your current working directory.

JAR files are a way to store and compress a collection of Java files. The files can be source code, bytecode, image files, or anything else that a Java program might require. JAR files can be very useful when dealing with complicated applications or large libraries that may contain dozens of files. You can run applications directly from a JAR file.

In this chapter we will cover ”

  • Defining a package

  • import declarations

  • CLASSPATH environment variable

  • Packages and access control

  • JAR files



Technical Java. Applications for Science and Engineering
Technical Java: Applications for Science and Engineering
ISBN: 0131018159
EAN: 2147483647
Year: 2003
Pages: 281
Authors: Grant Palmer

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net