by Bryan Morgan
Because Java is an object-oriented programming language, new classes can be defined by any Java programmer that extend the capabilities of the language. For example, the Java char data type is used to define a single-character variable. However, the java.lang.String class has been defined within the java package in order to give programmers the ability to use strings of characters. This class is important because the use of strings within programs is much more common than single-character variables.
J++ provides literally hundreds of classes that can handle a wide variety of tasks. Classes contained within the Visual J++ class library give the Java programmer the ability to add networking, multimedia, COM objects, or graphics capabilities to an application. All this functionality can be dropped into an application simply by adding a variable that is of the class type. These classes can also be inherited from through the use of the Java extends keyword.
This chapter provides a general overview of the Java packages that are provided with Visual J++. Some of the classes included with these packages are a standard part of every Java development and runtime environment. Other classes in these packages can only be acquired by purchasing Visual J++. The majority of the Visual J++-specific classes are used to handle the creation or calling of COM objects and are therefore platform dependent. The three primary packages addressed in this chapter are the java, sun, and com packages.
Programmers familiar with Visual C++ have probably used the Microsoft
Foundation Class (MFC) library to develop Windows applications
in C++. Borland C++ programmers should be familiar with Borland's
Object Windows Library (OWL). Both of these application frameworks
provide a core set of classes that is included with every copy
of the compiler. The vast majority of these classes is totally
generic and can be reused in a wide variety of application scenarios.
The concept of a Java package was introduced in Chapter 3 "Object-Oriented
Programming with Java," as a construct that holds a set of
related classes and interfaces. A package can contain as few as
one class or as many as you wish. Packages can also contain other
packages (known as subpackages). Visual J++ includes three
packages that are contained in the classes.zip file and
are installed to the \windows\java\classes directory
by default. These packages are the java, sun,
and com packages.
| Note |
The classes.zip file also includes the source code for most of these classes. To extract the source code from this file, run the javasrc tool located in the \classes directory using the following syntax: javasrc classes.zip. |
The java package makes what is often referred to as the Java Base API or the Java Base Platform. This package is guaranteed to be included with every valid Java compiler and runtime environment. The contents of this package are controlled by JavaSoft. The java package contains the classes that are deemed to be of most importance to Java developers. Included in this package are classes to support graphics, threading, networking, and input/output. The 1.0.2 release of the Java Developer's Kit (JDK) contains the following subpackages:
Each of these subpackages contains a related set of classes and interfaces that share some common functionality. All classes in the applet package, for instance, provide the functionality that is used to create or assist a Java applet. The members of this package are used repeatedly throughout this book, so understanding the capabilities of applet is important. For more detailed information on the java package, refer to Chapter 13, "The Standard Java Packages."
The com package contains many classes and interfaces that allow Java code to access Microsoft component object model (COM) objects. Functionality is also provided within these classes to allow COM objects to be implemented in Java. Beginning in Chapter 20, "Calling COM Objects from Java," tools supplied with Visual J++ are introduced to demonstrate how you can use COM objects just as if they were standard Java objects.
The com package contains one subpackage: ms. Most programmers assume that this ms stands for Microsoft and that the name is used to differentiate Microsoft's classes from other vendors who may choose to extend the com package with their own subpackages. The com.ms package includes the following subpackages:
The following sections provide an overview of the contents of these subpackages. Their contents are explained here so that you will have a clear understanding of the capabilities supported by the com package as it is used in later chapters.
The BrowserAppletFrame class within the com.ms.applet package contains functionality used by Internet Explorer to manipulate Java applets. In addition to the standard applet methods such as getDocumentBase(), getCodeBase(), and getParameter(), methods are provided to manage applets within a frame window. The remaining classes within this package provide extended Java support for loading Java classes from within a CAB file. CAB files are files used to contain compressed Java classes. These files can be downloaded from a remote server, then decompressed on the client side. CAB files will be examined in more detail later in the book. Classes such as CabClassLoader and AppletSecurity are used to provide this functionality.
The com.ms.awt contains a set of classes that use native methods to create and manipulate Windows GUI components. The CaretX class supplies caret functionality and allows the caret position to be set using x and y coordinates. The FontX class inherits from java.awt.Font and will extend this class by providing TrueType Font support. The MenuX, MenuBarX, and MenuItemX classes extend the traditional Java menus by adding Windows attributes (such as check marks to show toggled menu items).
The com.ms.lang package at the current time only contains one class, SystemX. This class contains a large group of methods that deal specifically with retrieving data from the keyboard and also deal with Unicode functionality. Unicode can store and transmit international characters (not just the standard English characters) and is supported under Windows NT. All char variables in Java code are Unicode characters. Specific methods within this class that implement this functionality are LocalStringToJavaString(), JavaStringToLocalString(), getKeyboardLanguage(), and setKeyboardLanguage().
The com.ms.net package contains classes that directly call WinInet API functions. The com.ms.net.wininet.URLUtils class contains two methods, canonicalizeURL() and combineURL(), that can parse and manipulate URLs. Other classes within the com.ms.net.wininet.http package include HttpInputStream, HttpPostBufferStream, and HttpURLConnection.
The com.ms.com package contains the functionality that is probably of the most interest to Visual J++ developers. In the following chapters, you will begin to explore the various capabilities provided with Visual J++ that allow Java classes to call COM objects and vice versa. Thanks to the Microsoft Java Virtual Machine, COM objects are exposed as a set of properties or methods within a class or interface that fits in very nicely with the Java programming language. Likewise, tools within Visual J++ allow type libraries to be built for Java classes, which opens up the world of COM to Java programmers. Because the Java Virtual Machine is used as a COM in-process server for all Java objects, COM objects can retrieve a CLSID for any Java object, create it using standard COM calls, and then use that object through the set of interfaces it provides.
The IUnknown interface discussed in Chapter 16, "Java and the Component Object Model," is implemented in interface IUnknown. This interface contains the three methods, QueryInterface(), AddRef(), and Release().
COM provides a VARIANT structure type that can store various types of values. This data type is used to pass arguments by reference or by value to interfaces derived from the COM IDispatch interface. The Variant class represents this data type within the com.ms.com package. This class includes many methods that convert the Variant object's data to a wide variety of data types. Examples of these methods are toShort(), toInt(), toFloat(), toDouble(), toCurrency(), toDate(), toString(), toDispatch(), toBoolean(), toObject(), and toByte().
The COM SAFEARRAY data structure is commonly used to save bookmarks when scrolling through lists of values returned from COM objects. An example of a SAFEARRAY type in use is a recordset returned from a Data Access Objects (DAO) query. The recordset can be scrolled forward and backward. Bookmarks can be set using a bookmark that is actually a SAFEARRAY. The com.ms.com package includes a class named SafeArray that implements this functionality in a Java class.
The ComContext class contains a set of constants that represent the different types of COM objects. Constants identified within this class include INPROC_SERVER, INPROC_HANDLER, LOCAL_SERVER, INPROC_SERVER16, REMOTE_SERVER, INPROC_HANDLER16, INPROC_SERVERX86, and INPROC_HANDLERX86.
Finally, the LicenseMgr class allows the instantiation of licensed COM objects from within Java code. This class constructs these objects using the ILicenseMgr interface. Two methods are supplied with this interface: createInstance() and createWithLic(). The method createInstance() will create an object for your use based on a CLSID string that is passed to it. The createWithLic() method requires both a CLSID string and a license string to "unlock" the licensed COM object.
The DAOSAMPLE sample application included with Visual J++ creates a licensed COM object (DAO 3.0) for use within an application. Once this object has been created, it can be used throughout the application to query an underlying Access database.
In the following snippet of code, the string "mjgcqcejfchcijecpdhckcdjqigdejfccjri"
serves as the license string for the DAO DBEngine. This engine
is identified within the Windows Registry using the CLSID {00025E15-0000-0000-C000-000000000046}:
| Note |
To verify that this Registry entry is indeed valid, do a quick search through the Registry to determine if it exists on your system. If it doesn't, you will need to install the DAO runtime engine from the Visual J++ CD before you can run the example. On my local machine, the search for this Registry entry turned up a key containing the subkey InprocServer32. This key specifies the name of the physical object on the disk to be created. (On my machine, it was C:\PROGRAM FILES\COMMON FILES\MICROSOFT SHARED\DAO\DAO3032.DLL.) |
// Create the License Manager object
ILicenseMgr mgr = new LicenseMgr();
// Use the License Manager to create the DBEngine
result = (_DBEngine) mgr.createWithLic(
// The license key for the DAO DBEngine
"mjgcqcejfchcijecpdhckcdjqigdejfccjri",
// The CLSID for the DAO DBEngine
"{00025E15-0000-0000-C000-000000000046}",
// The aggregation IUnknown* punkOuter
null,
// The ctxFlag to create in inproc server
ComContext.INPROC_SERVER
);
The sun package contains a large group of classes and interfaces that have been developed by Sun Microsystems. These classes are subject to change over time and are not part of the Java Base API. In addition, they are not guaranteed to be included in Java runtime environments or development toolkits. However, they are included with Visual J++ and contain many useful objects. The primary subpackages contained within the sun package are
If some of these package or class names sound suspiciously familiar to packages or classes found in the java package, don't be surprised. Many of the classes found in the sun package are scheduled to be folded into the JDK 1.1 release. At the time of this writing, the 1.1 specification had been finalized, but no working betas have been released to the public. Expect to see updates to all of the major tool sets (including Visual J++) as new versions of the java package are released for public use.
Some of these undocumented, unsupported classes, which contain extremely useful functionality, are briefly described here. (These classes are not mentioned anywhere in the Visual J++ documentation.)
The sun.applet package contains some useful classes that may be of interest even to the beginning Java programmer. The AppletCopyright class derives from the java.awt.Frame class and can display a simple copyright splash screen to the user of your application.
The AppletAudioClip class implements the java.applet.AudioClip interface that can play audio clips (sound files).
The AppletProps class also extends from java.awt.Frame in order to create a simple form that can view or set an applet's properties. Specific properties that can be set using this class include proxy servers and security access privileges.
The AppletViewer class is the source code for the appletviewer utility included with Sun's JDK. This utility will correctly run and display Java applets. This functionality can be important given the wide variances between the various Java-capable browsers in terms of Java support. Because something may look fine within Netscape Navigator but may appear incorrectly in Internet Explorer (or vice versa), the appletviewer utility can be used to test an applet to see how it really should look.
Finally, the AppletZipClassLoader derives from the sun.applet.AppletClassLoader class. It can retrieve class files from ZIP files stored on a remote server. The ZIP file can be specified using a URL object (java.net.URL), which means that it does not have to exist on the local machine.
From the name of the package, you have probably concluded (correctly) that the classes and interfaces contained in the sun.audio package implement functionality to play and retrieve audio files. The AudioPlayer class derives from java.lang.Thread and implements an audio player within a thread. The AudioDevice class implements a Sun audio device. Because the playing of audio is a platform-specific operation, these classes use native methods to load Windows DLLs (in the case of the Microsoft Java Virtual Machine for Windows).
The sun.awt package contains a number of classes as well as two additional subpackages. Classes within this package provide functionality similar to the classes and interfaces found in the java.awt package. As is the case with other classes found in the sun package, some of these classes are so useful that they are sure to be folded into later releases of the Java Base API.
The FocusingTextField class is a useful text widget that extends the standard java.awt.TextField class by allowing text fields to be tabbed between each other. The setNextField() method can be called to set the "z order" of the TextFields on a form. When a field has the current input focus, it will be highlighted to alert the user.
The VariableGridLayout class extends the java.awt.GridLayout class. Grid layouts are layout managers that can be used to lay out controls on a form without setting specific (x,y) pixel values. The VariableGridLayout class allows controls to be laid out in rows and columns but also allows the relative sizes of these rows and columns to be set.
The sun.awt.image package contains a set of classes and interfaces that deal specifically with image file retrieval (in the form of GIF, XBM, or JPEG files). The GifImageDecoder class provides the functionality for reading in GIF files; the JPEGImageDecoder and XBMImageDecoder classes do the same for JPEG and XBM files.
The PixelStore, PixelStore32, and PixelStore8 classes can be used to create a memory cache for pixel data.
The sun.awt.win32 package provides a set of Win32-specific classes that can encapsulate common graphics features of the Win32 Graphics Device Interface (GDI). Most of these classes are peer interfaces to the Win32 controls. However, several interesting classes that implement portions of the Win32 GDI exist within this package.
The Win32Graphics class encapsulates a Windows graphics device context within a Java class. Many common drawing operations are contained here including methods for drawing shapes, images, and strings on a device context.
The Win32PrintJob class initiates and executes a Win32 print job. Programmers familiar with printing under Windows will realize that a printer's device context can be drawn on using the identical functionality used to draw items on the screen's device context. Therefore, the Win32PrintJob class supplies the getGraphics() method to return a Graphics object to be used for drawing purposes.
The sun.misc package contains a set of miscellaneous classes. Although these classes don't exactly fit into the other sun subpackages, the misc subpackage does not contain a random set of classes. In fact, this subpackage could have just as easily been named sun.encode. Many of the classes and interfaces supplied within this subpackage implement some of the more popular encoding/decoding schemes such as Base64 (Base64Encoder and Base64Decoder) and UUEncode/UUDecode (using UUEncode and UUDecode). Classes are also supplied to assist in performing cyclic redundancy checks (CRC16) and hex dumps (HexDumpEncoder).
The final package contained within the sun package is sun.net. As the name implies, this package contains many classes and subpackages that perform various networking operations.
The MulticastSocket class can join a multicasting group and open a multicast socket. Also included in the base package are classes that can build network clients and servers. These classes, NetworkClient and NetworkServer, use sockets to communicate between a client application and a server application. Another interesting class is the URLCanonicalyzer class. It takes a partial URL string value, parses it, and produces a valid URL. For instance, if the string ftp.sun.com was passed to the URLCanonicalyzer() constructor, the string ftp://ftp.sun.com would result.
The classes within this subpackage allow the Java programmer to provide FTP capabilities within an application. The FtpClient class contains methods such as login(), get(), put(), command(), list(), cd(), ascii(), and binary(). The IftpClient class is similar to FtpClient except that it allows the use of a proxy server to FTP files from outside of a firewall.
If you have ever read messages within an Internet newsgroup, you may be familiar with the term NNTP. It stands for Network News Transfer Protocol and is the protocol used to communicate between news reader clients and news servers. The sun.net.nntp subpackage contains classes that can be used to create news clients and retrieve articles from a remote server. Specifically, the NntpClient class contains methods such as openServer(), getGroup(), getArticle(), getHeader(), startPost(), and finishPost(). You can use the NewsgroupInfo class to retrieve information about a specific newsgroup from a news server.
E-mail is transferred across the Internet using a protocol known as Simple Mail Transfer Protocol (SMTP). Just as Java contains classes for dealing with other networking issues, a client class is included in this package that can create a mail client. Using the capabilities within the SmtpClient class, a Java program can send e-mail to a remote address via SMTP. The to() method adds destinations; the from() method names the sender. Meanwhile, startMessage() creates a PrintStream object that can be used to send a message to an SMPT mail server.
The sun.net.www subpackage contains a variety of classes and subpackages that complement existing Web-related networking classes already found in package java.net. Within the www package in particular are classes that can manipulate MIME tables on the Solaris and Win32 platforms.
The sun.net.www.content.image subpackage is a small package that contains wrapper classes used to create or manipulate image files. The gif, jpeg, x_xbitmap, and x_xpixmap classes create URLImageSource objects and return them as Object types to the caller.
The sun.net.www.http package provides support for using the Hypertext Transport Protocol (HTTP) to communicate between World Wide Web clients and server. The HttpClient class can create an HTTP-aware client application. Methods within this class include openServer(), getRequestStatus(), getURLFile(), getMimeHeader(), and getHeaderField().
This package may be of interest to Java developers because it contains a class that accepts a URL and allows that file to be open for input. This class, FileURLConnection, accepts a URL and returns an InputStream object that can write to a file.
One other interesting package hidden deep in the sun.net hierarchy is the sun.net.www.protocol.news subpackage. This package contains a variety of useful GUI components and classes for building a simple news reader application. The InlineButton class extends java.applet.Applet and acts similarly to an HTML anchor tag. When clicked, this button retrieves an associated URL. The ArticlePoster class extends from java.awt.Frame and contains methods for posting articles to a news server. The ReadIndicator class also extends java.applet.Applet and can display a listing of read and unread news messages within a specific newsgroup.
The Visual J++ class library includes nearly 500 classes and interfaces that Java programmers can reuse. Although some of these objects were not intended for standard application development tasks, most of these classes provide some useful, generic functionality. The java package contains the core functionality in every Java development tool and runtime environment. Included in this package are classes to create applets, perform networking operations, and read or write streams. The sun package includes many classes with a narrower focus than those in the java package. Many of the objects found within the sun package will eventually make their way into the standard java package. Finally, Microsoft supplies the com package exclusively for use with Visual J++. This package provides the "plumbing" between COM and the Java Virtual Machine. Important classes within this package can instantiate COM objects from within Java code and also create COM objects in Java.
The next two chapters introduce the Microsoft Java Virtual Machine and ActiveX. The Microsoft Java Virtual Machine is a fully compliant virtual machine that extends the standard Java Virtual Machine by allowing Java objects to use COM objects and vice versa. (This extension continues the Microsoft tradition of "embracing and extending" the technologies of other companies.) Following these final overview chapters, you will find working examples that use COM objects within a Java application and also create COM objects in Java that can be called from Visual Basic.