Java Programming On Linux
Java Programming On Linux
Table of Contents
Back Cover
This book is neither a course in Java programming nor a manual for the Linux
OS. While the well-written text provides overviews of both Java and Linux, it's
really a compendium of information you'll want on hand once you've chosen
Java-on-Linux. Coverage includes: configuring your Linux desktop, a list of the
Java core classes, a rundown of compilers, the Kaffe cleanroom, tidbits about
Linus Torvalds and Richard Stallman, the Open Source movement, when JIT
compilers are a benefit, threads, GNU, what's supported where, decompilers
and obfuscators, and improved graphical rendering. This must-have book will
answer your questions and provide enjoyable browsing for a long time
Table of Contents
-2-
Chapter 15 - Troubleshooting the Blackdown JRE/JSDK Installation - 238
Chapter 16 - Participating in the Blackdown Community - 247
Part V Tools in the Blackdown JSDK
Chapter 17 - The Java Application Launchers: java, jre, and oldjava - 250
Chapter 18 - The Java Applet Viewer: appletviewer - 256
Chapter 19 - The Java Compiler: javac - 260
Chapter 20 - The Java Debugger: jdb - 263
Chapter 21 - The Java Archiver: jar - 268
Chapter 22 The Java Native Code Header and Stub File Generator:
-
javah - 270
Chapter 23 - The Java Documentation Generator: javadoc - 272
Chapter 24 - Miscellaneous JSDK Development Tools - 281
Part VI Additional Java Runtime Environments
-3-
Chapter 50 - Deploying Applets with Java Plug-in - 429
Chapter 51 Crossing Platform Component Models: Bringing Java to
-
ActiveX - 439
Chapter 52 - InstallShield: Creating Self-Installing Java Applications - 456
Chapter 53 - DashO: Optimizing Applications for Delivery - 459
Part XII Linux Platform Issues
Chapter 65 - Java on the Web: Java Servlets and Apache JServ - 574
Chapter 66 - Java from Web Pages: JSSI and JSP - 588
Chapter 67 - Java, Linux, and Three-Tiered Architectures - 560
Part XV Appendixes
Back Cover
Java Programming on Linux is your guide to using the Java programming
language on the Linux platform. Written by an experienced Java and Linux
developer, this book introduces you to the many Java technologies available
today for you to use under Linux -- from proprietary Sun technologies to fully
Open Source solutions. Filled with practical, hands-on advice, Java
Programming on Linux will help you get the most out of Java and Linux, as an
applications platform, a development environment, and an enterprise server.
-4-
• How to identify performance bottlenecks that are slowing down your
Java applications on Linux and other platforms.
• How to deploy Java on Linux servers to support three-tier application
architectures.
Nathan Meyers
-5-
All rights reserved. No part of this book shall be reproduced, stored in a retrieval system,
or transmitted by any means, electronic, mechanical, photocopying, recording, or
otherwise, without written permission from the publisher. No patent liability is assumed
with respect to the use of the information contained herein. Although every precaution
has been taken in the preparation of this book, the publisher and author assume no
responsibility for errors or omissions. Neither is any liability assumed for damages
resulting from the use of the information contained herein.
Trademarks
All terms mentioned in this book that are known to be trademarks or service marks have
been appropriately capitalized. Waite Group Press cannot attest to the accuracy of this
information. Use of a term in this book should not be regarded as affecting the validity of
any trademark or service mark.
Nathan left HP in 1999 to pursue other opportunities in the worlds of Linux and Java.
Besides books like this, he has published in the Hewlett-Packard Journal, The X Resource
Journal, and the Linux Journal. He participates actively in the Java/Linux community and
manages this book's Web site at http://www.javalinux.net—visit the site for
information, updates, errata, or just to send email to the author.
Dedication
To Vicki.
Acknowledgments
It takes a village to make a book, and this book has benefited from the talents of many
important contributors.
First, I'd like to thank Margot Maley of Waterside Productions and Don Roche of
Macmillan Computer Publishing, who worked together to bring this project into existence.
Development editor Robyn Thomas and project editor Charlotte Clapp coordinated the
complex logistics required to turn my words into a real book. Copy editor Geneil Breeze
-6-
kept my use of the language honest, and technical editors Luke Jones, Michael Jarvis,
and Juan Jose Sierralta P. checked my work on the technical side. To anyone else I've
neglected to mention: My sincere gratitude and my apologies for the oversight.
Beyond the efforts that went into creating this book, I must also acknowledge the heroic
efforts in the Java, Linux, and Open Source communities that have made this book both
possible and of value. To the many brilliant developers behind the Blackdown organization,
Transvirtual Technologies, Cygnus Solutions, IBM AlphaWorks, and many other
organizations mentioned in the book: Thank you for making Java on Linux a great place to
do software and a great place to do business.
You can fax, email, or write me directly to let me know what you did or didn't like about
this book—as well as what we can do to make our books stronger.
Please note that I cannot help you with technical problems related to the topic of this
book, and that due to the high volume of mail I receive, I might not be able to reply to
every message.
When you write, please be sure to include this book's title and author as well as your
name and phone or fax number. I will carefully review your comments and share them
with the author and editors who worked on the book.
Fax: 317-581-4770
E-mail: [email protected]
Five years ago, as Microsoft Windows 95 swept the world, Linux and Java were tiny blips
on the radar. In 1995, The term "Open Source" had not yet been coined, Linux was an
underground movement, and Java was struggling to prove itself a working technology.
In the past few years of explosive Internet growth, both Linux and Java have assumed
crucial roles in advancing network technologies and shaping the Web. Both have had to
mature quickly, and, with the recent releases of the Linux 2.2 kernel and the Java 2
Platform, both demand to be taken seriously as technologies and as businesses.
-7-
Linux and Java go together like, well… cream and coffee. Linux offers a powerful, stable,
efficient operating system; Java offers a powerful and portable applications platform of
huge and growing popularity. You've probably already used the two together—if you've
ever run Netscape Navigator on Linux. But there is much more to Java than applets and
browsers. This book will help you take the next step: to the world of Java applications,
Java development, and Java Web services.
• Part I: A Brief Introduction to Java—If you're new to Java, this part takes you on a
brief tour of the language and the environment.
• Part II: A Brief Introduction to Linux—If you're new to Linux, this part gives you a
brief introduction to the operating system and helps you get started setting up a Linux
system.
• Part III: Setting Up for Java Development and Deployment on Linux—This part
describes the pieces you need to enable Java deployment and development in your
Linux environment.
• Part IV: The Blackdown Port: A Sun Java SDK for Linux—The Blackdown
organization is the group responsible for porting Sun's Java software to Linux. This
part of the book describes how to obtain and install Java runtime and development
software from Blackdown.
• Part V: Tools in the Blackdown JSDK—The Java Software Development Kit (JSDK)
from Blackdown includes all the pieces you need to develop, test, and run Java. Here
we describe the tools and how to use them.
-8-
• Part VI: Additional Java Runtime Environments—The Sun software distributed by
Blackdown is not the last word in running Java on Linux. This part describes
alternative Java environments you can use under Linux.
• Part VII: Additional Java Runtime Components—This part of the book describes
additional components to make your Java environment faster, better, and more
capable.
• Part VIII: Compilers and Debuggers—You have many Java development tool
choices beyond the SDK. Here we present some alternative compilers and debuggers
you can use.
• Part IX: IDEs, GUI Builders, and RAD Tools—This part explores advanced
development tools—integrated development environments, user interface builders,
and rapid application development tools—available for use on Linux. One such tool,
Inprise JBuilder, is bundled on the accompanying CD-ROM.
• Part XI: Java Application Distribution—This part of the book helps you distribute
your Java applications to the rest of the world, including users on other operating
systems.
• Part XII: Linux Platform Issues—This part discusses issues specific to using Java on
the Linux platform, such as accessing native platform capabilities and dealing with the
X Window System.
• Part XIII: Java Performance—This part explores Java performance: why it's slow,
why it's improving, and how you can tune your own applications for better
performance.
• Part XIV: Java and Linux on Servers—Java and Linux both have important roles on
three-tier applications servers and Web servers. This part of the book discusses using
Linux and Java for server applications.
• Part XV: Appendixes—Here you'll find an index of programs provided in the book,
some code listings, and some pointers to additional resources.
• Code lines, commands, statements, variables, and any text you type or see onscreen
appears in a mono typeface. Bold italic mono typeface is often used to represent
the user's input.
-9-
• Command syntax descriptions use the following notation to describe commands and
arguments:
• Long listings of code or output are printed with line numbers to aid in reading. If a line
is too wide to fit on the page, the remainder appears in the following line without a line
number.
• The book also contains Subtleties sidebars that explore a topic in more detail. The
information here may not be of immediate use but is helpful in better understanding
the topic or solving difficult problems.
• JDK—A JDK is a Java technology release, such as JDK1.0, JDK1.1, and JDK1.2. (Its
original meaning was "Java Development Kit," but common usage has broadened it to
mean an entire technology release. This is discussed in more detail in Chapter 10,
"Java Components for Linux," in the section on "A Glossary of Sun Java
Terminology.")
• JRE—A JRE is a Java Runtime Environment. This is a subset of the SDK targeted at
deployment platforms. It contains everything needed to run Java programs but no
development tools.
This book will assume the use of bash (the Bourne-Again SHell), which is the most
popular Linux command shell. Command input lines will be shown with this prompt:
- 10 -
bash$
When an input line is too long in a Linux command shell, you can end it with the
backslash character and continue it on the next line:
bash$ echo The quick brown fox jumps over the lazy \
dog
The quick brown fox jumps over the lazy dog
bash$
For most interactions discussed in this book, the choice of command shell has little effect
on how commands are entered. But there are two important exceptions.
bash$ FOO=bar
bash$ export FOO
or, occasionally, the form used to set the variable for the duration of a single command:
For users who prefer the popular csh (C-shell) or tcsh (a csh clone), you will need to
perform your own translation to the csh-style notation:
When you start a new login session running bash, it reads a file called
~/.bash_profile (that's .bash_profile in your home directory) for any user-
specific setup of your environment. This book sometimes instructs you to add commands
to that file for setting environment variables.
If you are a csh or tcsh user, you will need to translate these instructions. The
initialization file it reads is called ~/.login (.login in your home directory)—this is
where you will need to add the corresponding setenv commands.
- 11 -
Part I: A Brief Introduction to Java
Chapter List
Part Overview
The first part of the book provides a brief introduction to Java. If you're a Linux user or
developer coming to Java for the first time, you may find the Java concept a bit
bewildering because Java is a lot of things: a language, an architecture, an applications
platform, and more.
So we begin with a look at what Java really is, where and how it is used, what it offers to
programmers, and what sort of applications capabilities it provides.
So what exactly is Java? It's a language. It's a machine architecture. It's a loading model.
It's a file format. It's an applications environment (several different applications
environments, actually). It's a specification. And it's an implementation.
Java began life as a failed Sun research project called Oak, targeted at embedded
operation in appliances. In 1995, Sun repackaged Oak as a portable "Internet programming
language" and positioned it initially as a way to run programs in Web browsers. The result
was something of a misfire: Web applets were not a huge success, and even today they
occupy a largely specialized niche. But Java displayed usefulness in other areas, and
interest in Java for different tasks—particularly Web services and enterprise connectivity—
skyrocketed. Java has since settled into a number of important application areas (we
explore more below), including, at long last, appliances!
- 12 -
By the time Sun announced Java in 1995, C++ and object-oriented programming had
been around for years. C++ had grown, in episodic spurts, from a preprocessor into a full-
featured compiled language. It had become the language of choice for projects of all
scales, and it had been through several stages of standardization—culminating in the
acceptance of the ANSI C++ standard in 1998.
C++ had also, along the way, picked up considerable baggage. It had a substantial
number of non-object-oriented artifacts, and it had become a difficult language to write
compilers for. It was also difficult to achieve complete portability: even the excellent ANSI
standardization did not completely shield developers from platform-specific language
porting headaches.
One of Java's goals was to fix what was wrong with C++, with a special focus on the
error-prone aspects of C++ development—those that tend to take up too much
debugging time. In this it has certainly succeeded: Java developers (especially C++
converts) find the language well-suited for rapid prototyping and development. Java's
remedies include:
• Eliminating the error-prone direct manipulation of memory pointers and the confusion
of referencing and dereferencing
• Performing runtime checking for such common problems as illegal typecasting, bad
array subscripts, and null object references
A detailed language specification is available, both in printed form and from Sun's Web
site (http://java.sun.com). Like most specs, it is better as a reference work than a
learning tool. For actually learning the language, a good place to start would be Java
Unleashed (Sams).
Chapter 2, "Moving from C++ to Java," uses some programming examples to take a
closer look at the differences between Java and C++. Despite the differences, Java looks
much like C++, and experience suggests that C++ programmers can pick it up quickly
and easily. So although this book is not in the business of teaching the language, the
introduction and the examples should be enough to get you well past the "Hello World"
stage.
- 13 -
Figure 1.1: Java defines a low-level architecture and instruction set closely
aligned with the high-level language.
The loading of Java classes consists of several steps (see Figure 1.2):
2. Verifying that the bits describe a well-structured class containing well-structured Java
code
4. Resolving references
Figure 1.2: The Java class loader builds the environment during application
execution.
- 14 -
modified by subclassing: Developers can define new sources of Java functionality not
envisioned by Java's creators.
The concept of runtime loading of functionality is certainly not new. We see it routinely in
dynamically loaded libraries, object models (CORBA, COM, and so on), and the plug-in
capability of many products. What is new is the full integration of class loading with the
language and the environment: it's never before been this easy, flexible, or extensible.
Allowing class loading from arbitrary sources (local disk, the Web, networked devices, a
dynamic code generator, and so on) is a notable advance in object-oriented
programming: it treats executable code objects with the same facility previously reserved
for data objects.
The loading model, combined with the portability of the code itself, gives Java bragging
rights as an "Internet programming language."
• JDK1.0—Sun's initial release, heavily hyped but not ready for prime time. A basic
applications environment with a basic GUI component (AWT, the Abstract Windowing
Toolkit) built on top of native platform GUI mechanisms.
• JDK1.2 (officially The Java 2 Platform, version 1.2)—Many consider this the first
ready-for-prime-time Java. It is huge but useful, introducing security enhancements, a
robust 2D graphics imaging model, the JFC Swing GUI toolkit (a native Java look and
feel), an accessibility API, drag-and-drop support, the collections classes, improved
persistence support, reference objects, an audio API, CORBA support, and more.
The language and architecture have also evolved with each release (nested classes, for
example, appeared in version 1.1), but the environment has changed most dramatically.
- 15 -
with a call to exit(). A standalone program is usually run by invoking a JVM and
specifying a class file to execute.
• The applet environment contains a restrictive security manager that prevents applets
from affecting the world outside the browser (such as the local file system) and
constrains the behavior of class loading and networking.
• Applets have a different life cycle from applications, described in terms of when they
are initially loaded, when they are started and stopped by the browser due to page
visibility, and when they are finally unloaded from the browser. There is no main() in
applets.
Applets are typically run when a browser reads a page containing the HTML tags to load
and execute a Java class.
Differences aside, both applets and applications expect a full JRE. So a browser
supporting JDK1.2 (as of this writing, neither major browser does) would include the full,
gigantic JDK1.2 environment—Swing toolkit and all.
Java does define other, simpler environments for use in more constrained applications:
- 16 -
• EmbeddedJava—A subset of JDK1.1 for use in embedded controllers, with extensions
targeted at real-time environments. EmbeddedJava is a political hot potato at the
moment: A number of vendors with deep experience in real-time systems were so
dissatisfied with Sun's EmbeddedJava work that they formed the J-Consortium in early
1999 to work toward better, vendor-neutral real-time Java extensions.
• JavaCard—A Java environment for use in smart cards, "credit cards with brains,"
designed to support the application and transaction requirements of that market.
In mid-1999, Sun announced the Java 2 Platform Micro Edition, a unification targeted at
subsuming these technologies.
We examine the core JRE classes in more detail in Chapter 3, "A Look at the Java Core
Classes."
Given the Java specification, it is possible for anyone to create any part of Java—a
compiler, a VM, an SDK—without any encumbrances to Sun. Later, you learn of some
"cleanroom" Java pieces, built entirely from specs, available on Linux.
Sun has also created a reference implementation of everything in the spec: JVM, core
libraries, and a development kit containing a full complement of tools. Sun ships two
commercial implementations, for Solaris and Windows NT, that were created from the
reference implementation. It also licenses the reference implementation to other vendors,
which is the basis for commercial Java ports on such platforms as HP-UX, AIX, Ultrix,
and others. The reference implementation is also the basis for the Blackdown SDK for
Linux, which gets extensive coverage beginning in Chapter 13, "Blackdown: The Official
Linux Port."
Use of the reference implementation comes at a price: The source is available for no
charge, but any products built from it are encumbered by licensing obligations to Sun. The
licensing terms are reasonably generous to anyone building a noncommercial
implementation; all others pay fees, resulting in an important revenue stream for Sun.
- 17 -
• Java Cryptography Extensions—A framework for private- and public-key cryptography
• Smart cards—a complete Java Virtual Machine plus the card-holder's data can reside
in a chip on a small plastic card.
In later chapters, we explore how some of these environments are being deployed in Linux.
- 18 -
That is also Java's problem: it's slow. We examine performance issues (and what to do
about them) in more detail later. The performance story undoubtedly will improve, but
there is good reason to doubt that Java will ever challenge compiled native applications
in terms of speed. Among the problems Java cannot handle today:
• GUIs—Of course Java does GUIs—Swing is a first-rate toolkit. But GUI performance
needs a great deal of attention if Java is to be a serious