Java Secrets


[ Table of Contents | Examples | About the CD | Corrections | Los secretos de Java a tu alcance | Order ]
Java Secrets Book Cover Java Secrets delves into the parts of Java that are not documented by Sun, that are not generally accessible to anyone with a web browser, and that are not already in a hundred other books. Part One explores the internals of the Java virtual machine including byte code, thread models, garbage collection algorithms, class loaders, security managers, and more. It teaches you how to disassemble and decompile .class files so you too can learn things nobody wanted you to know. Part Two delves into the sun classes, a group of undocumented packages that add considerable power to Java programs. Part Three explores the possibilities opened by platform dependent code. It shows you how to call the native API and how to create stand-alone executable programs. Finally, the CD includes an assortment of Java hacking tools including a full version of the payware Java decompiler WingDIS 2.0.3.

Why I Wrote Java Secrets

There are more than a hundred books about Java on bookstore shelves today, and at least ninety of them are completely predictable and more or less interchangeable. It's as if they had all been written from the same outline but by different authors.

Each book begins with a chapter about what's special about Java and how it differs from other programming languages. Each book shows how to write Hello World and other command line applications to teach Java's syntax. There is a chapter or two on object-oriented programming, a chapter on threads, a chapter on exceptions, and a few chapters on the AWT. I know. I wrote one of these books.

Java Secrets is different. It starts where the other books stop. This book assumes you already know Java's syntax and what an object is. This book assumes you're comfortable with the AWT. Instead of rehashing these topics, this book delves into the parts of Java that are not documented by Sun, that are not generally accessible to anyone with a web browser, and that are not already in a hundred other books.

I had some reservations about writing this book. I still do. This is a dangerous book. It reveals knowledge that can easily be abused. Improper use of the secrets revealed herein can easily tie Java programs to specific platforms or implementations. As a longtime Mac user I know the agony of watching all the best software come out on Windows first and the Mac much later, if at all. I do not want to extend this trend to Java-based software.

Nonetheless I have come to the conclusion that a book like this is necessary if Java is to move out of its niche of creating applets for web pages and into the broader software development market. There are many applications for which Java is ideal, but which cannot be written without more information than Sun has chosen to reveal. Among other things these include stand-alone executable applications. HotJava and javac are such applications so it must be possible to write them, but until now Sun has not revealed how. This book reveals that secret among others.

However, rationalize though I might (and I'm quite good at rationalizing, I admit), the real reason this book is being written is that it seemed like a neat thing to do at the time. This is far and away the most exciting book I've ever written. The sheer number of "Aha!" experiences I've had while researching and writing it is phenomenal. I hope you'll get the same feeling while reading it. Nonetheless, I know the information I present here will be misused. I accept that. Nonetheless I firmly believe that in the long run more knowledge is a good thing, dangerous though it may be; and that secrets are meant to be revealed.

What's In This Book?

There are three different ways a Java program can become dangerous. It can rely on the internal structure of Java objects; it can use classes it isn't supposed to know about; or it can be platform specific. This book covers all three.

After a brief introduction, Part One begins with six chapters on Java internals. The reader will learn how objects and primitive data types are laid out in memory, how arguments are passed to and values returned from methods, what a variable really is, and more. Java's implementation of arrays and Strings will be explored. Different possible models for threads and algorithms for garbage collection are discussed and compared, shedding some light on why Java uses the data structures and algorithms it does and why it sometimes behaves in unexpected ways. This is all tied to the Java .class file format in two chapters that teach the reader how to read and disassemble Java byte code. You'll learn some details about Java's thread model and garbage collection algorithms. Finally you'll learn how an applet runs and what really happens when a web browser loads an applet.

Part Two delves into the sun classes, a group of undocumented packages that add considerable power to Java programs. The following are just a few of the undocumented classes that will be covered in this section:

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

Part Three explores the possibilities opened by platform dependent code. It demonstrates how to call the native API and how to create stand-alone executable programs.

This is not an introductory book. It is for the programmer who has learned enough about Java to be frustrated by its limitations. You should have a solid grasp of the fundamentals of both the Java language and the AWT, including advanced topics like threads. Although every effort has been made to make this book accessible to as broad a range of readers as possible, this is not an introductory book and does require more of its reader than most books on the market.

On the other hand this book does not assume prior experience with assembly language, Java byte code, compiler design, or even pointers. In fact this book may serve as a first taste of some of these to a reader who's never seen them before, in Java or any other language. Nonetheless low-level programmers who are familiar with pointers, assembly language and compiler design should nonetheless find the discussion of Java's implementation of these topics to be useful. They'll simply find the book easier going than a programmer encountering these topics for the first time.

Bugs

This book is so far out on the bleeding edge, I've got a personal account rep at the New York Blood Bank. I've done my best to try to provide useful and accurate information. All the code in his book has been verified on at least one virtual machine (VM). Most of the code has been tested on two or more. However, because Java runs on so many different platforms and because it is changing in Internet time, it is impossible to be completely precise and accurate in all instances. Furthermore, precisely because the material in this book is secret, it's been extremely hard to verify.

I am certain there are mistakes here. In fact, I'm sure there are some real whoppers. (The first one's on the back cover. Somehow the system requirements were mistakenly listed as Windows 95 and Office 97. That's totally untrue. Any Java 1.0 or later platform should be fine. The book was mostly written on a Macintosh PowerBook and a Solaris SparcStation. The screen shots are mostly from the Mac.) Please, please use this information carefully and read it with a critical eye. If you do find mistakes or inaccuracies, let me know by sending email to elharo@metalab.unc.edu, and I'll correct them in future editions. I will also post corrections and updates here so you may wish to check this web site first before sending me email.

Summing Up

This is one of the most interesting and exciting projects I've worked on in a long time. The sheer number of "Aha!" experiences I had while researching and writing this book was phenomenal. I hope you'll get the same feeling while reading it. I'm going upload the source code and some other material from Java Secrets to Cafe au Lait in the next few days. Furthermore, even though the book is 900 pages long, I still couldn't fit in everything I wanted to talk about so I'll be revealing some new and updated secrets here in the coming weeks. In the meantime, why don't you check out Java Secrets and let me know what you think?

If I've succeeded in piqueing your interest, you should be able to find Java Secrets at almost any bookstore that carries computer books including the online bookstore, amazon.com or Computer Literacy.

If you need to special order it, the ISBN number is 0764580078. It's $59.95, published by IDG Books, and written by me, Elliotte Rusty Harold.


Table of Contents

Preface
Part 1: How Java Works
Chapter 1 Introducing Java Secrets
Chapter 2 Primitive Data Types
Chapter 3 Classes, Strings, and Arrays
Chapter 4 The Java Virtual Machine
Chapter 5 Byte Code
Chapter 6 Threads and Garbage Collection
Chapter 7 Controlling Applets
Part II: The Sun Classes
Chapter 8 Introducing the Sun Classes
Chapter 9 Using the sun.applet Classes to View Applets
Chapter 10 Controlling Audio Playback with sun.audio
Chapter 11 Controlling the AWT with the sun.awt Package
Chapter 12 Encoding and Decoding Data with the sun.misc Package
Chapter 13 Network Servers and Clients in the sun.net Package
Chapter 14 Sending Mail with SMTP with the sun.net.smtp Package
Chapter 15 Reading News via NNTP with the sun.net.nntp Package
Chapter 16 Transferring Files with the sun.net.ftp package
Chapter 17 sun.net.www
Part III Platform Dependent Java
Chapter 18 Character Conversion with sun.io
Chapter 19 Mixed Language Programming with Native Methods
Chapter 20 Creating Stand-Alone Programs
Appendixes
Appendix A Class Defined-In Index
Appendix B Method Defined-In Index
Appendix C Subclass Index
Appendix D Interface Implemented By Index
Appendix E Class Returned By Index
Appendix F Class Passed To Index
Appendix G Sun Class Hierarchy Diagrams
Appendix H About the CD-ROM
Index

[ Cafe au Lait | Table of Contents | Examples | About the CD | Corrections | Los secretos de Java a tu alcance | Order ]

Copyright 1997 Elliotte Rusty Harold
elharo@metalab.unc.edu
Last Modified January 2, 2003