Brought to you by EarthWeb
ITKnowledge Logo Login Graphic The Complete Resource for All Web Builders
The Complete Resource for All Web Builders
ITKnowledge
Find:
 
EXPERT SEARCH ----- nav

EarthWeb Direct

EarthWeb sites: other sites

Previous Table of Contents Next


Part II: The sun Classes

Part II delves into the sun packages, a group of undocumented classes that add considerable power to Java programs. The java packages provide the public API that most programmers use, but the sun packages work behind the scenes. Many of Sun’s Java development tools, like javac and the appletviewer, are built from Sun classes. Furthermore, many of the public classes and interfaces in the JDK privately use the sun classes.

The following are just a few of the capabilities hidden inside the sun classes:

  Running applets
  Communicating with ftp, mail, Web and news servers
  Data encoding and decoding
  Playing audio files

As you can see, Sun has hidden a lot of functionality inside the sun packages. This book reveals it.

Part II is dangerous because these classes may not be present in future releases of Java. They may not even be present in Java implementations not written by Sun. Even if they are present, their public methods may not have the same signatures. Classes that are public in one version may have only package access in the future. They may even move from one package to another. Nonetheless, these classes provide too much additional power to be ignored, and there are some very simple techniques provided here that enable you to use these packages safely in even non-conforming implementations.

Part III: Platform-Dependent Java

Part III explores the possibilities opened by platform-dependent code. It demonstrates how to call the native API and how to create stand-alone executable programs that take advantage of unique abilities of the local platform.

This part is dangerous because it limits the audience of a program. It’s also dangerous because it violates many of the security restrictions normally imposed on Java programs. Nonetheless, not all programs are applets on Web pages. Many programs can benefit from taking advantage of native code, either for speed or to add additional functionality not present in the AWT. There are ways to use platform-dependent code to enhance your application without making your program inaccessible to users on all other platforms. This section explores these possibilities.

Why Java Secrets?

Relying on implementation-specific details opens up the possibility that your programs may stop working when Sun revises Java. Using the sun packages means that not all Java environments may be able to run your programs. Using native code limits your audience, increases the time-to-market, and makes your program buggier. There are ways you can limit these bad effects, but they are real, and they must be considered. Given these problems, why would anyone want to learn about the material in this book? I can think of several reasons.

Broader applicability

In some cases, the design of Java limits you to a very small portion of the programs you might want to write. For example, the getAudioClip() and loop() methods of the Applet and AppletContext classes let your applets play sounds. Only applets can play sounds, however. In fact, it’s a little worse than that. Only applets that run in a Web browser or an applet viewer can play sounds. Applet subclasses that you instantiate in main() or embed in your own programs cannot play sounds because they don’t have an AppletContext.


Secret:  There really isn’t any good reason for restricting sound playing to applets. Applications of all sorts often need to play sounds. By using the sun.audio classes, you can play sounds in all your Java programs, not just your applets.

More power


Secret:  The sun packages let you do things you just can’t do otherwise. For example, suppose you wanted to build an IDE for Java development and actually use Java. To accomplish this, you need to compile files, debug them, and run them. All the hooks to do that are in the undocumented sun.tools package.

Inspiration

The sun classes are often a fertile source of ideas. Although many of the classes and packages are incomplete, they often provide a pattern on which you can model your own, more functional classes.


Secret:  For example, the sun.net.nntp.NntpClient class lets you open groups, list the articles in the groups, request specific articles, and post new articles. That’s useful, but some obvious methods are missing. There is no method to get a list of all the newsgroups on the server, for example. You can use the sun.net.nntp.NntpClient class as a model for your own NNTP class that does know how to get a list of all the available newsgroups and a lot more. Furthermore, your class can fix some of the bugs in Sun’s NntpClient class.

Sometimes you can create your own classes by extending Sun’s; some-times you’ll copy and paste; sometimes you’ll write your own classes from scratch using a similar API. Whichever you choose, it’s almost always easier to start with a good design and correct some minor deficiencies than it is to design a class from scratch.

Of course, not all of Sun’s designs are good. Sometimes you can learn from the mistakes made in the sun classes so you don’t repeat them. Not every class described in this book actually performs as advertised, and I’ll be sure to tell you when that’s so. One reason that some classes and packages are undocumented is that they’re buggy, poorly designed, or incomplete. Learning from your mistakes is good. Learning from someone else’s mistakes is even better. You can learn from Sun’s mistakes.

Where Did the Secrets Come From?

Some people have asked how I discovered this information. To be perfectly honest, it really wasn’t that hard. Sun has not gone to particularly great lengths to hide Java’s internal structure from nosy eyes.


Previous Table of Contents Next
HomeAbout UsSearchSubscribeAdvertising InfoContact UsFAQs
Use of this site is subject to certain Terms & Conditions.
Copyright (c) 1996-1999 EarthWeb Inc. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.