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 xxiPreface xxviiIntroduction 1Getting Started PART I 3An Embarrassment of Riches: The Linux Environment Chapter 1 3What You Will Learn 1.1 4The Command Line: What’s the Big Deal? 1.2 5Basic Linux Concepts and Commands 1.3 6Redirecting I/O 1.3.1 9The ls Command 1.3.2 9Filenames 1.3.3 10Permissions 1.3.4 12File Copying 1.3.5 13Seeing Stars 1.3.6 15File Contents 1.3.7 19The grep Command 1.3.8 vii Contents 20The find Command 1.3.9 21The Shell Revisited 1.3.10 26The tar and zip Commands 1.3.11 29The man Command 1.3.12 30Review 1.4 30What You Still Don’t Know 1.5 30Resources 1.6 33An Embarrassment of Riches: Editors Chapter 2 33What You Will Learn 2.1 34Eye to Eye with vi 2.2 40Exiting 2.2.1 41Search and Replace 2.2.2 43The Joy of Regular Expressions 2.2.3 44Starting Off Right: .exrc 2.2.4 45Editors Galore 2.3 47Editing Your Pipes (sed, the Stream EDitor) 2.3.1 47Simple Graphical Editors 2.3.2 49Review 2.4 49What You Still Don’t Know 2.5 49Resources 2.6 51An Experienced Programmer’s Introduction to Java Chapter 3 51What You Will Learn 3.1 52Fundamental Language Elements 3.2 53Scalar Types 3.2.1 57Object Types 3.2.2 65Statements 3.2.3 74Error Handling, Java Style 3.2.4 78print(), println(), printf() 3.2.5 82Using (and Making) Java APIs 3.3 82The package Statement 3.3.1 84The import Statement 3.3.2 85Encapsulation, Inheritance, and Polymorphism 3.4 85Encapsulation 3.4.1 86Inheritance 3.4.2 Contentsviii 91Polymorphism 3.4.3 93O, Templates! Where Art Thou? 3.5 94Virtually Final 3.6 95A Useful Simple Application 3.7 95Review 3.8 97What You Still Don’t Know 3.9 97Resources 3.10 98Exercises 3.11 99Where Am I? Execution Context Chapter 4 100What You Will Learn 4.1 100A Simple Start 4.2 100Command-Line Arguments 4.2.1 102Unit Testing Made Easy 4.2.2 102The System Class 4.3 102Java and Standard I/O 4.3.1 104Environment Variables 4.3.2 105Java and Environment Variables 4.3.3 109The Properties Class 4.4 111The Runtime Class 4.5 111exec() 4.5.1 113Portability 4.5.2 113Review 4.6 113What You Still Don’t Know 4.7 114Resources 4.8 115The Sun Microsystems Java Software Development Kit Chapter 5 116What You Will Learn 5.1 116All You Need, and Not One Thing More 5.2 117The Java Compiler 5.3 117Compiler Behavior, Defaults, and Environment Variables 5.3.1 119javac Options 5.3.2 121The Java Runtime Engine 5.4 121The Basics 5.4.1 122java Options 5.4.2 122Complete, Up-to-Date Program Documentation Made Easy 5.5 ixContents 123Running javadoc 5.5.1 124Javadoc Command-Line Options 5.5.2 128Javadoc Comments 5.5.3 131Dispensing with Applets 5.6 132Going Native 5.7 137Introducing RMI 5.8 137A Brief Introduction to RMI 5.8.1 143The rmic Tool 5.8.2 144The rmiregistry Tool 5.8.3 144Setting Up Servers and Clients 5.8.4 147RMI Summary 5.8.5 148The Java Debugger 5.9 157Return to the Source: The Java Decompiler 5.10 157Bundling a Java Program: Put It in a JAR 5.11 160Deploying Applications 5.11.1 163Basic jar Operation 5.11.2 164The Rest of the Toolkit 5.12 166Review 5.13 166What You Still Don’t Know 5.14 166Resources 5.15 167The IBM Developer Kit for Linux, Java 2 Technology Edition Chapter 6 167What You Will Learn 6.1 168 Use Linux Features to Make Multiple Java SDKs Play Nicely Together 6.2 168Links 6.2.1 173Switching Java Versions by Symlink 6.2.2 174How the IBM JDK Differs from the Sun JDK 6.3 175Performance 6.3.1 175Differences in the Commands 6.3.2 175IBM Classes 6.3.3 176What Are All These “_g” Versions? 6.4 176Review 6.5 177What You Still Don’t Know 6.6 177Resources 6.7 Contentsx [...]... 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... 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 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... 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... complete application would obscure and confuse the points being illustrated Preface xxiii 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... 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... develop and deploy Java applications on Linux systems, this book will provide a lot of useful information xxvii xxviii Introduction If you are an experienced Linux user or developer, and you are interested in using the Java language on that platform, this book will guide you through some advanced Java development topics and will present, we hope, some novel uses for familiar Linux and GNU tools If you are... this is an open-content book, we know full well that we will only be updating it as our “day jobs” permit In other words, those seeking complete and current reference material should go to the Web Those who have a multimillion-dollar budget for applications development will probably be well served by commercial application server products While we very much believe that Linux and Java on Linux are fully... supporting production environments, we recognize that products such as BEAWeblogic and IBM’s WebSphere have large support organizations behind them, and 1 Note that we do tend to recommend titles from Pearson Education (our publishers), but that we by no means confine ourselves to that publisher Introduction xxix (at least for now) a larger base of developers and contracting organizations with staff (variably)... 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 . Application Development on Linux ® Java ™ Application Development on Linux ® Carl Albing Michael Schwarz Prentice Hall Professional Technical Reference Boston Many. 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. ©