Prentice.Hall – Java.Application.Development.on.Linux

Java™ Application Development
on Linux®

Java™ Application Development
on Linux®

Carl Albing
Michael Schwarz

Prentice Hall Professional Technical Reference
Boston

Many of the designations used by manufacturers and sellers to distinguish their products are
claimed as trademarks. Where those designations appear in this book, and the publisher was
aware of a trademark claim, the designations have been printed with initial capital letters or in
all capitals.
The authors and publisher have taken care in the preparation of this book, but make no
expressed or implied warranty of any kind and assume no responsibility for errors or omissions.
No liability is assumed for incidental or consequential damages in connection with or arising
out of the use of the information or programs contained herein.
The publisher offers excellent discounts on this book when ordered in quantity for bulk

purchases or special sales, which may include electronic versions and/or custom covers and
content particular to your business, training goals, marketing focus, and branding interests.
For more information, please contact:
U. S. Corporate and Government Sales
(800) 382-3419
corpsales@pearsontechgroup.com
For sales outside the U. S., please contact:
International Sales
international@pearsoned.com
Visit us on the Web: www.phptr.com
Library of Congress Cataloging-in-Publication Data:
CIP data on file.
© 2005 Pearson Education, Inc.
ISBN 0–13–143697-X
Text printed in the United States on recycled paper at Pheonix Color in Hagerstown Maryland.
First printing, November 2004

To my mother, for her love of words.
—Michael


To my wife, Cynthia.
—Carl

Contents

Preface ..........................................................................................................

xxi

Introduction ................................................................................................... xxvii

PART I

Getting Started ......................................................

1

Chapter 1

An Embarrassment of Riches: The Linux Environment ............


3

What You Will Learn ...........................................................................
The Command Line: What’s the Big Deal? ........................................
Basic Linux Concepts and Commands ...............................................
1.3.1 Redirecting I/O .......................................................................
1.3.2 The ls Command ...................................................................
1.3.3 Filenames ..............................................................................
1.3.4 Permissions ...........................................................................
1.3.5 File Copying ...........................................................................
1.3.6 Seeing Stars ..........................................................................
1.3.7 File Contents ..........................................................................
1.3.8 The grep Command ..............................................................

3
4
5
6
9

9
10
12
13
15
19

1.1
1.2
1.3

vii

viii

Contents

1.4
1.5
1.6

Chapter 2
2.1
2.2

2.3

2.4
2.5
2.6
Chapter 3
3.1
3.2

3.3

3.4

1.3.9 The find Command ................................................................
1.3.10 The Shell Revisited ................................................................
1.3.11 The tar and zip Commands ...................................................

1.3.12 The man Command ...............................................................
Review ................................................................................................
What You Still Don’t Know ..................................................................
Resources ..........................................................................................

20
21
26
29
30
30
30

An Embarrassment of Riches: Editors .......................................

33

What You Will Learn ...........................................................................
Eye to Eye with vi ...............................................................................
2.2.1 Exiting ....................................................................................

2.2.2 Search and Replace ..............................................................
2.2.3 The Joy of Regular Expressions ............................................
2.2.4 Starting Off Right: .exrc .....................................................
Editors Galore .....................................................................................
2.3.1 Editing Your Pipes (sed, the Stream EDitor) .........................
2.3.2 Simple Graphical Editors .......................................................
Review ................................................................................................
What You Still Don’t Know ..................................................................
Resources ..........................................................................................

33
34
40
41
43
44
45
47
47
49

49
49

An Experienced Programmer’s Introduction to Java ................

51

What You Will Learn ...........................................................................
Fundamental Language Elements ......................................................
3.2.1 Scalar Types ..........................................................................
3.2.2 Object Types ..........................................................................
3.2.3 Statements .............................................................................
3.2.4 Error Handling, Java Style .....................................................
3.2.5 print(), println(), printf() ...............................
Using (and Making) Java APIs ...........................................................
3.3.1 The package Statement .....................................................
3.3.2 The import Statement .......................................................
Encapsulation, Inheritance, and Polymorphism .................................
3.4.1 Encapsulation ........................................................................
3.4.2 Inheritance .............................................................................


51
52
53
57
65
74
78
82
82
84
85
85
86

ix

Contents

3.5

3.6
3.7
3.8
3.9
3.10
3.11

3.4.3 Polymorphism ........................................................................
O, Templates! Where Art Thou? .........................................................
Virtually Final ......................................................................................
A Useful Simple Application ...............................................................
Review ................................................................................................
What You Still Don’t Know ..................................................................
Resources ..........................................................................................
Exercises ............................................................................................

91
93
94
95

95
97
97
98

Chapter 4

Where Am I? Execution Context ..................................................

99

What You Will Learn ...........................................................................
A Simple Start .....................................................................................
4.2.1 Command-Line Arguments ....................................................
4.2.2 Unit Testing Made Easy .........................................................
The System Class ...........................................................................
4.3.1 Java and Standard I/O ...........................................................
4.3.2 Environment Variables ...........................................................
4.3.3 Java and Environment Variables ...........................................
The Properties Class .................................................................
The Runtime Class .........................................................................
4.5.1 exec() ................................................................................
4.5.2 Portability ...............................................................................
Review ................................................................................................
What You Still Don’t Know ..................................................................
Resources ..........................................................................................

100
100
100
102
102
102
104
105
109
111
111
113
113
113
114

The Sun Microsystems Java Software Development Kit ...........

115

What You Will Learn ...........................................................................
All You Need, and Not One Thing More .............................................
The Java Compiler .............................................................................
5.3.1 Compiler Behavior, Defaults, and Environment Variables .....
5.3.2 javac Options .........................................................................
The Java Runtime Engine ..................................................................
5.4.1 The Basics .............................................................................
5.4.2 java Options ...........................................................................
Complete, Up-to-Date Program Documentation Made Easy ..............

116
116
117
117
119
121
121
122
122

4.1
4.2

4.3

4.4
4.5

4.6
4.7
4.8
Chapter 5
5.1
5.2
5.3

5.4

5.5

x

Contents

5.12
5.13
5.14
5.15

5.5.1 Running javadoc ...................................................................
5.5.2 Javadoc Command-Line Options ...........................................
5.5.3 Javadoc Comments ...............................................................
Dispensing with Applets .....................................................................
Going Native .......................................................................................
Introducing RMI ..................................................................................
5.8.1 A Brief Introduction to RMI .....................................................
5.8.2 The rmic Tool ........................................................................
5.8.3 The rmiregistry Tool .............................................................
5.8.4 Setting Up Servers and Clients ..............................................
5.8.5 RMI Summary ........................................................................
The Java Debugger ............................................................................
Return to the Source: The Java Decompiler ......................................
Bundling a Java Program: Put It in a JAR ..........................................
5.11.1 Deploying Applications ...........................................................
5.11.2 Basic jar Operation ................................................................
The Rest of the Toolkit .......................................................................
Review ................................................................................................
What You Still Don’t Know ..................................................................
Resources ..........................................................................................

123
124
128
131
132
137
137
143
144
144
147
148
157
157
160
163
164
166
166
166

Chapter 6

The IBM Developer Kit for Linux, Java 2 Technology Edition ...

167

What You Will Learn ...........................................................................
Use Linux Features to Make Multiple Java SDKs Play Nicely
Together .............................................................................................
6.2.1 Links .......................................................................................
6.2.2 Switching Java Versions by Symlink ......................................
How the IBM JDK Differs from the Sun JDK ......................................
6.3.1 Performance ..........................................................................
6.3.2 Differences in the Commands ................................................
6.3.3 IBM Classes ...........................................................................
What Are All These “_g” Versions? ....................................................
Review ................................................................................................
What You Still Don’t Know ..................................................................
Resources ..........................................................................................

167

5.6
5.7
5.8

5.9
5.10
5.11

6.1
6.2

6.3

6.4
6.5
6.6
6.7

168
168
173
174
175
175
175
176
176
177
177

xi

Contents

Chapter 7

The GNU Compiler for Java (gcj) .................................................

179

7.5
7.6
7.7
7.8
7.9
7.10

What You Will Learn ...........................................................................
A Brand GNU Way .............................................................................
The GNU Compiler Collection ............................................................
Compiling Our Simple Application with gcj ........................................
7.4.1 Compiling FetchURL with gcj ................................................
7.4.2 Compiling a Multiclass Program ............................................
Options and Switches .........................................................................
Reasons to Use gcj ............................................................................
Reasons Not to Use gcj .....................................................................
Review ................................................................................................
What You Still Don’t Know ..................................................................
Resources ..........................................................................................

179
179
180
181
182
184
185
186
187
187
187
188

Chapter 8

Know What You Have: CVS ..........................................................

189

What You Will Learn ...........................................................................
Source Control: Whys and Hows ........................................................
8.2.1 Setup ......................................................................................
8.2.2 Import .....................................................................................
8.2.3 Normal Use ............................................................................
8.2.4 Update ...................................................................................
8.2.5 Merges ...................................................................................
8.2.6 Log .........................................................................................
8.2.7 cvs status ......................................................................
8.2.8 cvs tag ..............................................................................
8.2.9 Branching Tags ......................................................................
8.2.10 cvs export ......................................................................
8.2.11 A Quick Look behind the Scenes ...........................................
A GUI: jCVS ........................................................................................
8.3.1 Installing jCVS ........................................................................
Review ................................................................................................
What You Still Don’t Know ..................................................................
Resources ..........................................................................................

190
190
193
195
198
201
201
203
205
205
207
210
210
211
211
213
214
214

7.1
7.2
7.3
7.4

8.1
8.2

8.3
8.4
8.5
8.6

xii

Contents

Chapter 9
9.1
9.2
9.3

9.4

9.5
9.6
9.7
Chapter 10
10.1
10.2

10.3
10.4

10.5
10.6
10.7

Ant: An Introduction .....................................................................

215

What You Will Learn ...........................................................................
The Need for a Different Build Tool ....................................................
Obtaining and Installing Ant ................................................................
9.3.1 Installing a Binary Tarball .......................................................
9.3.2 Installing a Source Tarball .....................................................
A Sample Ant Buildfile ........................................................................
9.4.1 XML for the Uninitiated ..........................................................
9.4.2 The Buildfile Tags ..................................................................
9.4.3 A Real, Live Buildfile ..............................................................
Review ................................................................................................
What You Still Don’t Know ..................................................................
Resources ..........................................................................................

215
216
217
217
219
220
220
222
229
232
232
232

Integrated Development Environments ......................................

235

What You Will Learn ...........................................................................
NetBeans: The Open Source IDE ......................................................
10.2.1 A Brief History of NetBeans ...................................................
10.2.2 Installing NetBeans ................................................................
10.2.3 Getting Around in NetBeans ..................................................
10.2.4 Integration with CVS ..............................................................
10.2.5 Integration with Ant ................................................................
10.2.6 Other Add-on Tools ................................................................
SunONE Studio Community Edition ...................................................
Eclipse: The Source of SWT ..............................................................
10.4.1 Selecting and Installing Eclipse .............................................
10.4.2 Using Eclipse for a New Project .............................................
10.4.3 Using Eclipse for an Existing Project .....................................
10.4.4 Working with Eclipse ..............................................................
Review ................................................................................................
What You Still Don’t Know ..................................................................
Resources ..........................................................................................

236
236
236
237
241
245
248
250
251
251
251
254
256
257
258
259
259

xiii

Contents

PART II

Developing Business Logic ................................. 261

Chapter 11

Balancing Acts: An Imaginary Scenario .....................................

263

What You Will Learn ...........................................................................
Statement of the Need ........................................................................
How to Develop Software ...................................................................
What Makes a Good Requirement .....................................................
Whom to Ask for Requirements ..........................................................
Requirements for the Budget Application ...........................................
11.6.1 Monday Morning, 10 A.M. ......................................................
11.6.2 Back at His Desk ....................................................................
11.7 Documenting, Prototyping, and Stakeholder Buy-In ...........................
11.7.1 Documenting ..........................................................................
11.7.2 Stakeholder Buy-In ................................................................
11.7.3 Prototyping .............................................................................
11.8 Review ................................................................................................
11.9 What You Still Don’t Know ..................................................................
11.10 Resources ..........................................................................................
11.11 Exercises ............................................................................................

263
264
264
267
268
269
269
272
272
272
274
275
276
276
276
277

11.1
11.2
11.3
11.4
11.5
11.6

Chapter 12

Analysis and Design: Seeking the Objects ................................

279

12.1
12.2
12.3
12.4
12.5
12.6
12.7
12.8
12.9
12.10
12.11
12.12
12.13

What You Will Learn ...........................................................................
Facing the Blank Page .......................................................................
Using CRC Cards ...............................................................................
Finding the Objects .............................................................................
Finding the Methods and Attributes ....................................................
Essential and Nonessential ................................................................
Analysis Paralysis ...............................................................................
Real Software Engineering .................................................................
Core Classes ......................................................................................
Review ................................................................................................
What You Still Don’t Know ..................................................................
Resources ..........................................................................................
Exercises ............................................................................................

279
280
280
280
283
284
287
288
289
289
289
289
292

xiv

Contents

Chapter 13

JUnit: Automating Unit Testing ...................................................

295

What You Will Learn ...........................................................................
JUnit: Why All the Fuss? ....................................................................
Design Then Test Then Code ............................................................
Installing and Running JUnit ...............................................................
13.4.1 Downloading and Unzipping ..................................................
13.4.2 Using JUnit .............................................................................
13.5 Writing Test Cases .............................................................................
13.5.1 JUnit Assertions .....................................................................
13.5.2 Running a Test Case .............................................................
13.6 Running Test Suites ...........................................................................
13.7 Review ................................................................................................
13.8 What You Still Don’t Know ..................................................................
13.9 Resources ..........................................................................................
13.10 Exercises ............................................................................................

295
296
296
297
299
301
303
306
308
309
312
312
313
313

13.1
13.2
13.3
13.4

Chapter 14

Storing the Data ............................................................................

315

What You Will Learn ...........................................................................
Follow the Objects ..............................................................................
Of Persistence ....................................................................................
Thinking of the Future, or Painting in Corners ....................................
Oracle, PostgreSQL, MySQL .............................................................
14.5.1 MySQL ...................................................................................
14.5.2 PostgreSQL ...........................................................................
14.5.3 Oracle ....................................................................................
14.5.4 Selection Criteria ....................................................................
14.6 Being Self-Contained ..........................................................................
14.7 Beyond the Basics ..............................................................................
14.8 Persistence Is Not the Whole Story ....................................................
14.9 Setting Up PostgreSQL for BudgetPro ...............................................
14.9.1 Installing PostgreSQL ............................................................
14.9.2 Creating a postgres User .................................................
14.9.3 Creating Our Database ..........................................................
14.9.4 Straight JDBC ........................................................................
14.10 Review ................................................................................................

315
316
316
316
316
317
317
318
318
318
319
322
322
322
323
324
325
325

14.1
14.2
14.3
14.4
14.5

xv

Contents

14.11 What You Still Don’t Know ..................................................................
14.12 Resources ..........................................................................................
14.13 Exercises ............................................................................................
Chapter 15

326
326
326

Accessing the Data: An Introduction to JDBC ...........................

327

What You Will Learn ...........................................................................
Introducing JDBC ...............................................................................
Making Connections ...........................................................................
15.3.1 Downloading JDBC for MySQL ..............................................
15.4 Querying Data ....................................................................................
15.5 Getting Results ...................................................................................
15.6 Updates, Inserts, Deletes ...................................................................
15.7 Review ................................................................................................
15.8 What You Still Don’t Know ..................................................................
15.9 Resources ..........................................................................................
15.10 Exercises ............................................................................................

327
328
329
332
332
334
336
336
336
337
338

15.1
15.2
15.3

PART III

Developing Graphical User Interfaces ................ 339

Chapter 16

Getting in the Swing of Things: Designing a GUI for
BudgetPro ......................................................................................

341

What You Will Learn ...........................................................................
A Simple Swing Program ....................................................................
Stompin’ at the Savoy, or The Swing Paradigm .................................
Slow, Slow, Quick-Quick, Slow: The Basic Swing Objects .................
Layout Managers ................................................................................
Beyond Arthur Murray: Actions, Listeners, Events .............................
Getting Down to Cases: Designing a GUI for BudgetPro ...................
16.7.1 Overview ................................................................................
16.7.2 Creating Pieces ......................................................................
16.8 Review ................................................................................................
16.9 What You Still Don’t Know ..................................................................
16.10 Resources ..........................................................................................
16.11 Exercises ............................................................................................

341
342
343
345
347
348
348
350
352
373
374
375
375

16.1
16.2
16.3
16.4
16.5
16.6
16.7

xvi

Contents

Chapter 17
17.1
17.2

17.3

17.4
17.5
17.6
17.7
17.8

Other Ways: Alternatives to Swing .............................................

377

What You Will Learn ...........................................................................
The IBM SWT Toolkit .........................................................................
17.2.1 Another GUI Toolkit. Why? ....................................................
17.2.2 Duplicated Effort. Why Cover It? ............................................
17.2.3 Portability: Better and Worse ..................................................
17.2.4 The Rest of the Chapter .........................................................
17.2.5 SWT: Close to the Metal ........................................................
17.2.6 “Hello, world” SWT Style ........................................................
Porting BudgetPro to SWT .................................................................
17.3.1 Step 1: Convert the Class Members ......................................
17.3.2 Step 2: Converting the main() Method ..............................
17.3.3 Step 3: Converting the GUI build() and init()
Methods .................................................................................
17.3.4 Completing the Conversion of the BudgetPro Class ........
17.3.5 Completing the Conversion of the Application .......................
17.3.6 Closing Thoughts ...................................................................
SWT and gcj ......................................................................................
Review ................................................................................................
What You Still Don’t Know ..................................................................
Resources ..........................................................................................
Exercises ............................................................................................

377
378
378
379
380
380
380
381
384
387
389
391
394
395
395
396
398
398
398
399

PART IV Developing Web Interfaces .................................. 401
Chapter 18
18.1
18.2
18.3
18.4
18.5
18.6

18.7

Servlets: Java Pressed into Service ............................................

403

What You Will Learn ...........................................................................
Servlets: Program-Centric Server-Side Documents ...........................
Perspective .........................................................................................
How to Write a Servlet ........................................................................
Input, Output .......................................................................................
Matters of State: Cookies, Hidden Variables, and the Dreaded
“Back” Button ......................................................................................
18.6.1 Cookies ..................................................................................
Designing a BudgetPro Servlet ..........................................................

403
404
405
407
411
413
414
416

xvii

Contents

18.7.1 Prototype ................................................................................
18.7.2 Design ....................................................................................
18.8 Review ................................................................................................
18.9 What You Still Don’t Know ..................................................................
18.10 Resources ..........................................................................................
18.11 Exercises ............................................................................................
Chapter 19
19.1
19.2
19.3

19.4
19.5
19.6
19.7
19.8
Chapter 20
20.1
20.2
20.3

20.4
20.5

416
417
420
420
421
421

JSP: Servlets Turned Inside Out .................................................

423

What You Will Learn ...........................................................................
Servlets Turned Inside Out: JSP ........................................................
How to Write a JSP Application ..........................................................
19.3.1 Scriptlet ..................................................................................
19.3.2 Declaration .............................................................................
19.3.3 Expression .............................................................................
19.3.4 Directive .................................................................................
19.3.5 New Syntax ............................................................................
19.3.6 JavaBeans in JSP ..................................................................
19.3.7 Tag Libraries ..........................................................................
Using JSP with BudgetPro .................................................................
Review ................................................................................................
What You Still Don’t Know ..................................................................
Resources ..........................................................................................
Exercises ............................................................................................

424
424
426
427
429
430
431
434
434
437
438
439
440
440
441

Open Source Web Application Servers ......................................

443

What You Will Learn ...........................................................................
Downloading JBoss ............................................................................
Be an Enabler, or “Let’s Be Codependent!” ........................................
20.3.1 Nonroot-Installed Software ....................................................
20.3.2 Finer Grained Control ............................................................
Installing JBoss ...................................................................................
Things That Make It Go ......................................................................
20.5.1 System V Init System .............................................................
20.5.2 RedHat/Fedora chkconfig ....................................................
20.5.3 Other Distributions .................................................................
20.5.4 IDE Integration .......................................................................

443
444
444
445
450
450
451
451
458
463
464

xviii

Contents

20.6
20.7
20.8
20.9
20.10
20.11
20.12

Disposition of Forces ..........................................................................
Apache Geronimo ...............................................................................
Installing Geronimo .............................................................................
Running the Geronimo Server ............................................................
Review ................................................................................................
What You Still Don’t Know ..................................................................
Resources ..........................................................................................

464
464
467
468
468
469
469

PART V

Developing Enterprise Scale Software ............... 471

Chapter 21

Introduction to Enterprise JavaBeans ........................................

473

What You Will Learn ...........................................................................
Expanding to EJBs .............................................................................
21.2.1 EJB Concepts ........................................................................
21.2.2 Bean Types ............................................................................
21.2.3 Under the Hood ......................................................................
What’s in a Name? An Introduction to JNDI .......................................
21.3.1 Naming and Directory System Concepts ...............................
21.3.2 Common Directory Services ..................................................
21.3.3 Putting a Face to a Name: JNDI ............................................
21.3.4 Using JNDI with JBoss ...........................................................
Review ................................................................................................
What You Still Don’t Know ..................................................................
Resources ..........................................................................................

473
473
474
477
482
482
482
484
487
491
491
492
492

Building an EJB .............................................................................

493

What You Will Learn ...........................................................................
EJBs: You Don’t Know Beans? ..........................................................
22.2.1 SessionBean ....................................................................
22.2.2 EJBObject .........................................................................
22.2.3 EJBHome ..............................................................................
22.2.4 Summarizing the Pieces ........................................................
22.2.5 EJBLocalHome and EJBLocalObject ......................
22.2.6 Compiling Your Beans ...........................................................
Review ................................................................................................

493
493
495
497
498
499
499
501
502

21.1
21.2

21.3

21.4
21.5
21.6
Chapter 22
22.1
22.2

22.3

xix

Contents

22.4
22.5

What You Still Don’t Know ..................................................................
Resources ..........................................................................................

502
503

Deploying EJBs .............................................................................

505

What You Will Learn ...........................................................................
Lend Me Your EAR: Enterprise Packaging and Deployment .............
23.2.1 What’s in an EJB-JAR File .....................................................
23.2.2 Using Our Bean .....................................................................
23.2.3 Packaging the Servlet ............................................................
Deploying the EAR .............................................................................
23.3.1 JBoss .....................................................................................
23.3.2 Geronimo ...............................................................................
Maintaining a Distributed Application .................................................
23.4.1 Ant and CVS ..........................................................................
23.4.2 XDoclet ..................................................................................
Abstracting Legacy Applications .........................................................
Review ................................................................................................
What You Still Don’t Know ..................................................................
Resources ..........................................................................................

505
506
508
511
512
514
515
515
516
516
517
518
518
519
519

Parting Shots .................................................................................

521

The Future’s So Bright, I Squint and Look Confused .........................
Our Book Is Yours ..............................................................................
Came the Revolution ..........................................................................
What You Still Don’t Know ..................................................................
Resources ..........................................................................................

521
522
522
523
523

Appendix A ASCII Chart ....................................................................................

525

Appendix B A Java Swing GUI for BudgetPro ................................................

527

Appendix C GNU General Public License ........................................................

539

Index .............................................................................................................

549

Chapter 23
23.1
23.2

23.3

23.4

23.5
23.6
23.7
23.8
Chapter 24
24.1
24.2
24.3
24.4
24.5

Preface

JAVA AND LINUX
Why another book on Java? Why a book on Java and Linux? Isn’t Java a platform-independent system? Aren’t there enough books on Java? Can’t I learn
everything I need to know from the Web?
No doubt, there are a host of Java books on the market. We didn’t wake
up one morning and say, “You know what the world really needs? Another book
about Java!” No. What we realized was that there are a couple of “holes” in the
Java book market.
First, Linux as a development platform and deployment platform for Java
applications has been largely ignored. This is despite the fact that the *nix
platform (meaning all UNIX and UNIX-like systems, Linux included) has long
been recognized as one of the most programmer-friendly platforms in existence.
Those few resources for Java on Linux that exist emphasize tools to the exclusion of the Java language and APIs.
Second, books on the Java language and APIs have focused on pedagogical
examples that serve to illustrate the details of the language and its libraries, but
very few of these examples are in themselves practically useful, and they tend
xxi

xxii

Preface

to deal only with the issues of writing programs, and not at all with deploying
and maintaining them. Anyone who has worked on a major software project,
especially a software project that is developed and deployed in a business for a
business, knows that designing and coding are only about half of the work involved. Yes, writing Java code is only slightly affected by the development and
the deployment platform, but the process of releasing and maintaining such
applications is significantly different between platforms.
To address these missing pieces, we decided to cover development and
deployment of a Java application that has command-line, GUI, servlet, and
enterprise components on a Linux platform. We’re writing the guide book we
wish we had had when we started writing and deploying Java applications on
Linux. We’re going to show you a simplistic enterprise application, “from cradle
to grave,” but along the way cover issues of design process, production environment, setup, administration, and maintenance that few books bother to cover.1
If you are considering buying this book and you are wondering if there is
any information in here that you can’t get for free on the Web, then, no. There
is not. In fact, there is little information in any Java or Linux book that is not
available for free on the Internet. In fact, in each of our chapters we will tell
you where on the Web to find virtually all of the information we present, and
then some. And yet books continue to sell, and we have the chutzpah to ask
you to buy the book. The reason is that Web information is scattered, unorganized, and of highly variable quality. We will be trying to bring all the relevant
information together in this book, in a clearly organized manner (and, we
would like to believe, at an acceptably high level of quality). We think that
has value.
Also, this book is part of the Bruce Perens’ Open Source Series. This book
is part of the Web literature. And you may freely read it and use it on the Web.
We hope this book will be one of those you use on the Web and buy on paper.
We don’t know about you, but we like to use Web books for reference, but for
reading, we like books. We own at least three books that are available for free
on the Web: Thinking in C++, Thinking in Java, and O’Reilly’s Docbook: The
Definitive Guide. We hope that open publishing will be the new model.

1. This is not to say this book is without purely pedagogical examples. Especially in Part I we
make use of your typical “throwaway” examples and single classes. To try to illustrate the basics
with a complete application would obscure and confuse the points being illustrated.

xxiii

Preface

FREE SOFTWARE AND JAVA
GNU/Linux2 is Free Software. It is Open Source. I don’t even want to start
the debate on what each term means and which one is “right.” One of the two
authors of this book is a Free Software advocate, and the other is of a purely
laissez-faire attitude towards the question (we won’t tell you which, although
we invite you to guess). But even with a deliberate decision to cease-fire, the
question remains: Is Java Open Source or Free Software?
The answer is mixed. Neither Sun’s nor IBM’s Java implementations are
Open Source or Free Software. You may download and use them for free, but
you do not have the source code to them, nor do you have the right to make
modifications to them.3 This book will cover the GNU Compiler for Java,
which compiles Java source code to native machine code. The GNU Compiler
for Java (gcj) is both Open Source and Free Software. It is, however, supporting
differing levels of the Java APIs (some packages are current, some are back at
1.1.x levels) and does not fully support the AWT or Swing GUIs.
However, none of this means that you cannot write your own Java programs and release them under a Free Software or Open Source license. So you
can certainly develop Free Software in Java. Staunch Free Software partisans
(such as Richard Stallman and the Free Software Foundation) would question
the wisdom of doing so. Their argument would be that a Free Software product
that depends on non-Free tools isn’t really Free Software, since to compile, use,
or modify it, you need to make use of a proprietary tool.
There is more than one effort to produce a Free Software Java runtime
implementation. None of them is “ready for prime time.” It would, in our
opinion, be a very good thing for Sun to release their SDK and Java Virtual
Machine as Free Software. But so far, they have steadily resisted calls to do so.

2. This is the only time we will refer to it as “GNU/Linux.” See Section 7.3 for the story of
why GNU/Linux is the preferred name of some. We understand Stallman and the FSF’s position, but “Linux” is much easier on the eyes and ears than “GNU/Linux.” And that, not principle, is how names and words go into the language. For better or for worse, “Linux” is the
name of the operating system.
3. As we write this, a very public discussion is taking place between Sun, IBM, and Eric Raymond, founder of the Open Source Initiative, about opening Java under some sort of open
source license. At this time, no one knows how this will turn out, but it is possible that Java
will be Free Software in the future.

xxiv

Preface

The fact, however, that two distinct vendors (Sun and IBM) produce effectively interchangeable development and runtime environments reduces some
of the risk that you face when you select a platform available only from a single
vendor who does not provide source code.
So, to put the case firmly: Java is free for use, but it is certainly not Free
Software as defined in The GNU Manifesto 4 or the GNU General Public License.5 This is a political and philosophical issue of interest only to those
aforementioned Free Software partisans. For the rest of us, this has no bearing
on Java’s technical or business merits. As for us, obviously we like the language
or we wouldn’t be writing about it.

YOU CAN HELP!
This book is part of the Bruce Perens’ Open Source Series. Shortly after this
book is published in dead-tree form, it will be on the Web,6 free for use, redistribution, and modification in compliance with the terms of the Open Publication License,7 with no options taken. You can immediately create your own
version as permitted in that license.
Naturally enough, we plan to maintain our “official” version of the online
book, so we encourage you to send suggestions, corrections, extensions, comments, and ideas to us. Please send any such to javalinux@multitool.net
and we will try to keep our little tome up-to-date so it continues to serve the
needs of the Java and Linux development communities.

ACKNOWLEDGMENTS
First off, we naturally wish to thank Mark L. Taub, our acquisitions editor at
Prentice Hall PTR, for believing in the book and in open publishing as th