Java 101: Packages organize classes and interfaces

Create packages, import packaged classes and interfaces into your programs, move packages, and encapsulate them in jar files

1 2 3 Page 3
Page 3 of 3
// PkgInfo.java
class PkgInfo
{
   public static void main (String [] args)
   {
      if (args.length < 1 || args.length > 2)
      {
          System.err.println ("usage: java PkgInfo pkgname [clsintname]");
          return;
      }
      if (args.length == 2)
          try
          {
              Class.forName (args [1]);
          }
          catch (ClassNotFoundException e)
          {
             System.err.println (args [1] + " not found.");
             return;
          }
      Package p = Package.getPackage (args [0]);
      if (p == null)
      {
          System.err.println (args [0] + " package not found.");
          return;
      }
      System.out.println ("Name = " + p.getName ());
      System.out.println ("Specification title = " +
                          p.getSpecificationTitle ());
      System.out.println ("Specification vendor = " +
                          p.getSpecificationVendor ());
      System.out.println ("Specification version = " +
                          p.getSpecificationVersion ());
      System.out.println ("Implementation title = " +
                          p.getImplementationTitle ());
      System.out.println ("Implementation vendor = " +
                          p.getImplementationVendor ());
      System.out.println ("Implementation version = " +
                          p.getImplementationVersion ());
   }
}

PkgInfo requires one or two command-line arguments. The first command-line argument identifies a package: PkgInfo calls Package's static Package getPackage(String name) method with the first command-line argument to return a reference to a Package object corresponding to that argument. If no package information is available, that is, if getPackage(String name)'s caller's classloader did not load any classfiles corresponding to the package's classes and interfaces, that method returns null instead of a Package reference.

The second command-line argument identifies one of the first command-line argument's classes or interfaces, and proves necessary to ensure at least one classfile loads (via Class.forName (args [1]);) before a call is made to getPackage(String name). If that is not done, most likely no package information will be available for the package identified by the first command-line argument.

If only the first command-line argument is present, PkgInfo assumes you want information on a package where one of the package's classfiles loads when the JVM starts running. One such package is java.lang. If you were to execute java PkgInfo java.lang, you would see the following specification and implementation strings for that package:

Name = java.lang
Specification title = Java Platform API Specification
Specification vendor = Sun Microsystems, Inc.
Specification version = 1.4
Implementation title = Java Runtime Environment
Implementation vendor = Sun Microsystems, Inc.
Implementation version = 1.4.0

The first line displays the package's name, which Package's String getName() method returns. The remaining lines display strings returned from Package's String getSpecificationTitle(), String getSpecificationVendor(), String getSpecificationVersion(), String getImplementationTitle(), String getImplementationVendor(), and String getImplementationVersion() methods, respectively.

Let's display the specification and implementation strings in the previously created testpkg.jar package file. To do that, execute java PkgInfo testpkg testpkg.A. If successful, you should see the following output:

Name = testpkg
Specification title = testPkg specification title
Specification vendor = me
Specification version = 1.0
Implementation title = testPkg implementation title
Implementation vendor = you
Implementation version = 1.0.1

Look familiar? The specification and implementation strings first appeared in our testpkg.jar file's manifest file. If you do not see this output but receive an error message, set the classpath environment variable to both testpkg.jar and the current directory (set classpath=testpkg.jar;.), and ensure the current directory is the root directory. That way, PkgInfo's classloader can locate testpkg.jar and that package's class A.

Review

This article explored packages, a concept that saves you from reinventing the wheel. Packages help you organize your classes and interfaces, which you can then import into your programs to save you from rewriting code. For further organizational purposes, you can move these directories around on your hard drive with the help of the classpath environment variable. Also, bundling a package's classfiles and manifest information into a jar file simplifies package distribution.

I encourage you to email me with any questions you might have involving either this or any previous article's material. (Please keep such questions relevant to material discussed in this column's articles.) Your questions and my answers will appear in the relevant study guides.

In next month's article, I will introduce you to the Character, String, StringBuffer, and StringTokenizer classes.

Jeff Friesen has been involved with computers for the past 20 years. He holds a degree in computer science and has worked with many computer languages. Jeff has also taught introductory Java programming at the college level. In addition to writing for JavaWorld, he has written his own Java book for beginners— Java 2 by Example, Second Edition (Que Publishing, 2001; ISBN: 0789725932)—and helped write Using Java 2 Platform, Special Edition (Que Publishing, 2001; ISBN: 0789724685). Jeff goes by the nickname Java Jeff (or JavaJeff). To see what he's working on, check out his Website at http://www.javajeff.com.

Learn more about this topic

1 2 3 Page 3
Page 3 of 3