Java™ Application Development on Linux®
Total Page:16
File Type:pdf, Size:1020Kb
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 [email protected] For sales outside the U. S., please contact: International Sales [email protected] 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 1.1 What You Will Learn ........................................................................... 3 1.2 The Command Line: What’s the Big Deal? ........................................ 4 1.3 Basic Linux Concepts and Commands ............................................... 5 1.3.1 Redirecting I/O ....................................................................... 6 1.3.2 The ls Command ................................................................... 9 1.3.3 Filenames .............................................................................. 9 1.3.4 Permissions ........................................................................... 10 1.3.5 File Copying ........................................................................... 12 1.3.6 Seeing Stars .......................................................................... 13 1.3.7 File Contents .......................................................................... 15 1.3.8 The grep Command .............................................................. 19 vii viii Contents 1.3.9 The find Command ................................................................ 20 1.3.10 The Shell Revisited ................................................................ 21 1.3.11 The tar and zip Commands ................................................... 26 1.3.12 The man Command ............................................................... 29 1.4 Review ................................................................................................ 30 1.5 What You Still Don’t Know .................................................................. 30 1.6 Resources .......................................................................................... 30 Chapter 2 An Embarrassment of Riches: Editors ....................................... 33 2.1 What You Will Learn ........................................................................... 33 2.2 Eye to Eye with vi ............................................................................... 34 2.2.1 Exiting .................................................................................... 40 2.2.2 Search and Replace .............................................................. 41 2.2.3 The Joy of Regular Expressions ............................................ 43 2.2.4 Starting Off Right: .exrc ..................................................... 44 2.3 Editors Galore ..................................................................................... 45 2.3.1 Editing Your Pipes (sed, the Stream EDitor) ......................... 47 2.3.2 Simple Graphical Editors ....................................................... 47 2.4 Review ................................................................................................ 49 2.5 What You Still Don’t Know .................................................................. 49 2.6 Resources .......................................................................................... 49 Chapter 3 An Experienced Programmer’s Introduction to Java ................ 51 3.1 What You Will Learn ........................................................................... 51 3.2 Fundamental Language Elements ...................................................... 52 3.2.1 Scalar Types .......................................................................... 53 3.2.2 Object Types .......................................................................... 57 3.2.3 Statements ............................................................................. 65 3.2.4 Error Handling, Java Style ..................................................... 74 3.2.5 print(), println(), printf() ............................... 78 3.3 Using (and Making) Java APIs ........................................................... 82 3.3.1 The package Statement ..................................................... 82 3.3.2 The import Statement ....................................................... 84 3.4 Encapsulation, Inheritance, and Polymorphism ................................. 85 3.4.1 Encapsulation ........................................................................ 85 3.4.2 Inheritance ............................................................................. 86 Contents ix 3.4.3 Polymorphism ........................................................................ 91 3.5 O, Templates! Where Art Thou? ......................................................... 93 3.6 Virtually Final ...................................................................................... 94 3.7 A Useful Simple Application ............................................................... 95 3.8 Review ................................................................................................ 95 3.9 What You Still Don’t Know .................................................................. 97 3.10 Resources .......................................................................................... 97 3.11 Exercises ............................................................................................ 98 Chapter 4 Where Am I? Execution Context .................................................. 99 4.1 What You Will Learn ........................................................................... 100 4.2 A Simple Start ..................................................................................... 100 4.2.1 Command-Line Arguments .................................................... 100 4.2.2 Unit Testing Made Easy ......................................................... 102 4.3 The System Class ........................................................................... 102 4.3.1 Java and Standard I/O ........................................................... 102 4.3.2 Environment Variables ........................................................... 104 4.3.3 Java and Environment Variables ........................................... 105 4.4 The Properties Class ................................................................. 109 4.5 The Runtime Class ......................................................................... 111 4.5.1 exec() ................................................................................ 111 4.5.2 Portability ............................................................................... 113 4.6 Review ................................................................................................ 113 4.7 What You Still Don’t Know .................................................................. 113 4.8 Resources .......................................................................................... 114 Chapter 5 The Sun Microsystems Java Software Development Kit ........... 115 5.1 What You Will Learn ........................................................................... 116 5.2 All You Need, and Not One Thing More ............................................. 116 5.3 The Java Compiler ............................................................................. 117 5.3.1 Compiler Behavior, Defaults, and Environment Variables ..... 117 5.3.2 javac Options ......................................................................... 119 5.4 The Java Runtime Engine .................................................................. 121 5.4.1 The Basics ............................................................................. 121 5.4.2 java Options ........................................................................... 122 5.5 Complete, Up-to-Date Program Documentation