You are previewing Java™ Application Development on Linux®.
O'Reilly logo
Java™ Application Development on Linux®

Book Description

Linux is the fastest-growing Java development platform because it saves money and time by serving as a platform for both development and deployment. But developers face significant platform-specific challenges when managing and deploying Java applications in a controlled production environment.

Written for Java and Linux developers alike, Java™ Application Development on Linux®is the hands-on guide to the full Java application development lifecycle on Linux.

Determined to spare other developers hours of trial and error, Albing and Schwarz demonstrate the platform, tools, and application development by showing realistic, easy-to-follow examples. After a simple command-line application introduces basic tools, this program leads readers through business-logic object analysis, database design, Java servlet UIs, Java Server Pages (JSP) UIs, Swing GUIs, and Standard Widget Toolkit (SWT) GUIs. Scaling up to the enterprise level provides the opportunity to use both the JBoss Application Server and the Apache Geronimo Application Servers, and Enterprise JavaBeans (EJB).

Readers learn how to

  • Use development tools available on Linux, such as the GNU Compiler for Java (gcj), Ant, the NetBeans IDE, IBM's Eclipse Java IDE, JUnit, and SunONE Studio

  • Develop business logic layers using Java DataBase Connectivity (JDBC)

  • Add a Web interface using servlets and JSPs

  • Add a GUI using Sun's Abstract Window Toolkit (AWT) and IBM's SWT

  • Deploy EJBs in Linux

The authors conclude by demonstrating how a hierarchy of budgets can be created, tracked, and shared with Concurrent Versions System (CVS).

A companion Website includes all source code and a link to each tool described.

Java™ Application Development on Linux® can propel you from a standing start to the full-speed development and deployment of Java applications on Linux.

Table of Contents

  1. Copyright
    1. Dedication
  2. Bruce Peren's Open Source Series
  3. Preface
    1. Java and Linux
    2. Free Software and Java
    3. You Can Help!
    4. Acknowledgments
  4. Introduction
    1. Who Should Buy This Book
    2. Who Should Not Buy This Book
    3. How to Use This Book
      1. Many Paths
      2. The Linux Newbie Path
      3. The Java Newbie Path
      4. The Client-Side Path
      5. The Server-Side Path
      6. The Enterprise Path
  5. I. Getting Started
    1. 1. An Embarrassment of Riches: The Linux Environment
      1. 1.1. What You Will Learn
      2. 1.2. The Command Line: What’s the Big Deal?
      3. 1.3. Basic Linux Concepts and Commands
        1. 1.3.1. Redirecting I/O
          1. 1.3.1.1. Standard I/O
          2. 1.3.1.2. Pipes
        2. 1.3.2. The ls Command
        3. 1.3.3. Filenames
        4. 1.3.4. Permissions
        5. 1.3.5. File Copying
        6. 1.3.6. Seeing Stars
          1. 1.3.6.1. Escape at Last
        7. 1.3.7. File Contents
        8. 1.3.8. The grep Command
        9. 1.3.9. The find Command
        10. 1.3.10. The Shell Revisited
        11. 1.3.11. The tar and zip Commands
        12. 1.3.12. The man Command
      4. 1.4. Review
      5. 1.5. What You Still Don’t Know
      6. 1.6. Resources
    2. 2. An Embarrassment of Riches: Editors
      1. 2.1. What You Will Learn
      2. 2.2. Eye to Eye with vi
        1. 2.2.1. Exiting
        2. 2.2.2. Search and Replace
        3. 2.2.3. The Joy of Regular Expressions
        4. 2.2.4. Starting Off Right: .exrc
      3. 2.3. Editors Galore
        1. 2.3.1. Editing Your Pipes (sed, the Stream EDitor)
        2. 2.3.2. Simple Graphical Editors
      4. 2.4. Review
      5. 2.5. What You Still Don’t Know
      6. 2.6. Resources
    3. 3. An Experienced Programmer’s Introduction to Java
      1. 3.1. What You Will Learn
      2. 3.2. Fundamental Language Elements
        1. 3.2.1. Scalar Types
          1. 3.2.1.1. Integer Types
          2. 3.2.1.2. Floating Point Types
          3. 3.2.1.3. Other Types
          4. 3.2.1.4. Operators
          5. 3.2.1.5. Arrays
        2. 3.2.2. Object Types
          1. 3.2.2.1. Objects as References
          2. 3.2.2.2. Strings
          3. 3.2.2.3. Other Classes: Reading Javadoc
        3. 3.2.3. Statements
          1. 3.2.3.1. Conditional Execution
            1. The if-else statement
            2. The switch statement
          2. 3.2.3.2. Looping and Related Statements
            1. The while statement
            2. The do-while loop
            3. The for loop
            4. The for loop with iterators
            5. The break and continue statements
          3. 3.2.3.3. The return statement
        4. 3.2.4. Error Handling, Java Style
        5. 3.2.5. print(), println(), printf()
      3. 3.3. Using (and Making) Java APIs
        1. 3.3.1. The package Statement
        2. 3.3.2. The import Statement
      4. 3.4. Encapsulation, Inheritance, and Polymorphism
        1. 3.4.1. Encapsulation
        2. 3.4.2. Inheritance
          1. 3.4.2.1. Inheritance and Encapsulation
          2. 3.4.2.2. The static statement
          3. 3.4.2.3. The final statement
        3. 3.4.3. Polymorphism
      5. 3.5. O, Templates! Where Art Thou?
      6. 3.6. Virtually Final
      7. 3.7. A Useful Simple Application
      8. 3.8. Review
      9. 3.9. What You Still Don’t Know
      10. 3.10. Resources
      11. 3.11. Exercises
    4. 4. Where Am I? Execution Context
      1. 4.1. What You Will Learn
      2. 4.2. A Simple Start
        1. 4.2.1. Command-Line Arguments
        2. 4.2.2. Unit Testing Made Easy
      3. 4.3. The System Class
        1. 4.3.1. Java and Standard I/O
        2. 4.3.2. Environment Variables
        3. 4.3.3. Java and Environment Variables
      4. 4.4. The Properties Class
      5. 4.5. The Runtime Class
        1. 4.5.1. exec()
        2. 4.5.2. Portability
      6. 4.6. Review
      7. 4.7. What You Still Don’t Know
      8. 4.8. Resources
    5. 5. The Sun Microsystems Java Software Development Kit
      1. 5.1. What You Will Learn
      2. 5.2. All You Need, and Not One Thing More
      3. 5.3. The Java Compiler
        1. 5.3.1. Compiler Behavior, Defaults, and Environment Variables
        2. 5.3.2. javac Options
      4. 5.4. The Java Runtime Engine
        1. 5.4.1. The Basics
        2. 5.4.2. java Options
      5. 5.5. Complete, Up-to-Date Program Documentation Made Easy
        1. 5.5.1. Running javadoc
        2. 5.5.2. Javadoc Command-Line Options
        3. 5.5.3. Javadoc Comments
      6. 5.6. Dispensing with Applets
      7. 5.7. Going Native
      8. 5.8. Introducing RMI
        1. 5.8.1. A Brief Introduction to RMI
          1. 5.8.1.1. History and Background
          2. 5.8.1.2. RMI Basics
          3. 5.8.1.3. Writing the Interface
          4. 5.8.1.4. Writing the Server Class
          5. 5.8.1.5. Writing the Client Class
        2. 5.8.2. The rmic Tool
        3. 5.8.3. The rmiregistry Tool
        4. 5.8.4. Setting Up Servers and Clients
          1. 5.8.4.1. What RMI Servers and Clients Need to Be Able to Do
          2. 5.8.4.2. Our Environment
          3. 5.8.4.3. Compiling and Running the Server
          4. 5.8.4.4. Compiling and Running the Client
        5. 5.8.5. RMI Summary
      9. 5.9. The Java Debugger
      10. 5.10. Return to the Source: The Java Decompiler
      11. 5.11. Bundling a Java Program: Put it in a JAR
        1. 5.11.1. Deploying Applications
          1. 5.11.1.1. The Manifest File
          2. 5.11.1.2. Putting a Compiled Application in a JAR File
        2. 5.11.2. Basic jar Operation
      12. 5.12. The Rest of the Toolkit
      13. 5.13. Review
      14. 5.14. What You Still Don’t Know
      15. 5.15. Resources
    6. 6. The IBM Developer Kit for Linux, Java 2 Technology Edition
      1. 6.1. What You Will Learn
      2. 6.2. Use Linux Features to Make Multiple Java SDKs Play Nicely Together
        1. 6.2.1. Links
          1. 6.2.1.1. Hard Links
          2. 6.2.1.2. Symbolic Links, or Symlinks
        2. 6.2.2. Switching Java Versions by Symlink
      3. 6.3. How the IBM JDK Differs from the Sun JDK
        1. 6.3.1. Performance
        2. 6.3.2. Differences in the Commands
        3. 6.3.3. IBM Classes
      4. 6.4. What Are All These “_g” Versions?
      5. 6.5. Review
      6. 6.6. What You Still Don’t Know
      7. 6.7. Resources
    7. 7. The GNU Compiler for Java (gcj)
      1. 7.1. What You Will Learn
      2. 7.2. A Brand GNU Way
      3. 7.3. The GNU Compiler Collection
      4. 7.4. Compiling Our Simple Application with gcj
        1. 7.4.1. Compiling FetchURL with gcj
        2. 7.4.2. Compiling a Multiclass Program
      5. 7.5. Options and Switches
      6. 7.6. Reasons to Use gcj
      7. 7.7. Reasons Not to Use gcj
      8. 7.8. Review
      9. 7.9. What You Still Don’t Know
      10. 7.10. Resources
    8. 8. Know What You Have: CVS
      1. 8.1. What You Will Learn
      2. 8.2. Source Control: Whys and Hows
        1. 8.2.1. Setup
        2. 8.2.2. Import
        3. 8.2.3. Normal Use
        4. 8.2.4. Update
        5. 8.2.5. Merges
        6. 8.2.6. Log
        7. 8.2.7. cvs status
        8. 8.2.8. cvs tag
        9. 8.2.9. Branching Tags
        10. 8.2.10. cvs export
        11. 8.2.11. A Quick Look behind the Scenes
      3. 8.3. A GUI: jCVS
        1. 8.3.1. Installing jCVS
      4. 8.4. Review
      5. 8.5. What You Still Don’t Know
      6. 8.6. Resources
    9. 9. Ant: An Introduction
      1. 9.1. What You Will Learn
      2. 9.2. The Need for a Different Build Tool
      3. 9.3. Obtaining and Installing Ant
        1. 9.3.1. Installing a Binary Tarball
          1. 9.3.1.1. Environment Variables
        2. 9.3.2. Installing a Source Tarball
      4. 9.4. A Sample Ant Buildfile
        1. 9.4.1. XML for the Uninitiated
        2. 9.4.2. The Buildfile Tags
          1. 9.4.2.1. The project Tag
          2. 9.4.2.2. The target Tag
          3. 9.4.2.3. Properties
          4. 9.4.2.4. Tasks
            1. Standard task attributes
            2. The javac task
            3. The jar task
            4. The javadoc task
            5. The copy and mkdir tasks
            6. The rmic task
          5. 9.4.2.5. Other Tags
            1. The PatternSet, Include, and Exclude datatypes
            2. Other datatypes
        3. 9.4.3. A Real, Live Buildfile
          1. 9.4.3.1. Project Organization
          2. 9.4.3.2. The build.xml File for Payback
      5. 9.5. Review
      6. 9.6. What You Still Don’t Know
      7. 9.7. Resources
    10. 10. Integrated Development Environments
      1. 10.1. What You Will Learn
      2. 10.2. NetBeans: The Open Source IDE
        1. 10.2.1. A Brief History of NetBeans
        2. 10.2.2. Installing NetBeans
          1. 10.2.2.1. Downloading
          2. 10.2.2.2. Installing
          3. 10.2.2.3. Running
        3. 10.2.3. Getting Around in NetBeans
          1. 10.2.3.1. Filesystem versus Project
          2. 10.2.3.2. Editing
        4. 10.2.4. Integration with CVS
        5. 10.2.5. Integration with Ant
        6. 10.2.6. Other Add-on Tools
      3. 10.3. SunONE Studio Community Edition
      4. 10.4. Eclipse: The Source of SWT
        1. 10.4.1. Selecting and Installing Eclipse
        2. 10.4.2. Using Eclipse for a New Project
        3. 10.4.3. Using Eclipse for an Existing Project
        4. 10.4.4. Working with Eclipse
      5. 10.5. Review
      6. 10.6. What You Still Don’t Know
      7. 10.7. Resources
  6. II. Developing Business Logic
    1. 11. Balancing Acts: An Imaginary Scenario
      1. 11.1. What You Will Learn
      2. 11.2. Statement of the Need
      3. 11.3. How to Develop Software
      4. 11.4. What Makes a Good Requirement
      5. 11.5. Whom to Ask for Requirements
      6. 11.6. Requirements for the Budget Application
        1. 11.6.1. Monday Morning, 10 A.M.
        2. 11.6.2. Back at His Desk
      7. 11.7. Documenting, Prototyping, and Stakeholder Buy-In
        1. 11.7.1. Documenting
        2. 11.7.2. Stakeholder Buy-In
        3. 11.7.3. Prototyping
      8. 11.8. Review
      9. 11.9. What You Still Don’t Know
      10. 11.10. Resources
      11. 11.11. Exercises
    2. 12. Analysis and Design: Seeking the Objects
      1. 12.1. What You Will Learn
      2. 12.2. Facing the Blank Page
      3. 12.3. Using CRC Cards
      4. 12.4. Finding the Objects
      5. 12.5. Finding the Methods and Attributes
      6. 12.6. Essential and Nonessential
      7. 12.7. Analysis Paralysis
      8. 12.8. Real Software Engineering
      9. 12.9. Core Classes
      10. 12.10. Review
      11. 12.11. What You Still Don’t Know
      12. 12.12. Resources
      13. 12.13. Exercises
    3. 13. JUnit: Automating Unit Testing
      1. 13.1. What You Will Learn
      2. 13.2. JUnit: Why All the Fuss?
      3. 13.3. Design Then Test Then Code
      4. 13.4. Installing and Running JUnit
        1. 13.4.1. Downloading and Unzipping
        2. 13.4.2. Using JUnit
      5. 13.5. Writing Test Cases
        1. 13.5.1. JUnit Assertions
        2. 13.5.2. Running a Test Case
      6. 13.6. Running Test Suites
      7. 13.7. Review
      8. 13.8. What You Still Don’t Know
      9. 13.9. Resources
      10. 13.10. Exercises
    4. 14. Storing the Data
      1. 14.1. What You Will Learn
      2. 14.2. Follow the Objects
      3. 14.3. Of Persistence
      4. 14.4. Thinking of the Future, or Painting in Corners
      5. 14.5. Oracle, PostgreSQL, MySQL
        1. 14.5.1. MySQL
        2. 14.5.2. PostgreSQL
        3. 14.5.3. Oracle
        4. 14.5.4. Selection Criteria
      6. 14.6. Being Self-Contained
      7. 14.7. Beyond the Basics
      8. 14.8. Persistence Is Not the Whole Story
      9. 14.9. Setting Up PostgreSQL for BudgetPro
        1. 14.9.1. Installing PostgreSQL
        2. 14.9.2. Creating a postgres User
        3. 14.9.3. Creating Our Database
        4. 14.9.4. Straight JDBC
          1. 14.9.4.1. Static Account Members
          2. 14.9.4.2. Joining the User
      10. 14.10. Review
      11. 14.11. What You Still Don’t Know
      12. 14.12. Resources
      13. 14.13. Exercises
    5. 15. Accessing the Data: An Introduction to JDBC
      1. 15.1. What You Will Learn
      2. 15.2. Introducing JDBC
      3. 15.3. Making Connections
        1. 15.3.1. Downloading JDBC for MySQL
      4. 15.4. Querying Data
      5. 15.5. Getting Results
      6. 15.6. Updates, Inserts, Deletes
      7. 15.7. Review
      8. 15.8. What you Still Don’t Know
      9. 15.9. Resources
      10. 15.10. Exercises
  7. III. Developing Graphical User Interfaces
    1. 16. Getting in the Swing of Things: Designing a GUI for BudgetPro
      1. 16.1. What You Will Learn
      2. 16.2. A Simple Swing Program
      3. 16.3. Stompin’ at the Savoy, or The Swing Paradigm
      4. 16.4. Slow, Slow, Quick-Quick, Slow: The Basic Swing Objects
      5. 16.5. Layout Managers
      6. 16.6. Beyond Arthur Murray: Actions, Listeners, Events
      7. 16.7. Getting Down to Cases: Designing a GUI for BudgetPro
        1. 16.7.1. Overview
          1. 16.7.1.1. Instance Variables
          2. 16.7.1.2. Main
        2. 16.7.2. Creating Pieces
          1. 16.7.2.1. Simple JLabels
          2. 16.7.2.2. FlowLayout
          3. 16.7.2.3. BoxLayout
          4. 16.7.2.4. JButtons
          5. 16.7.2.5. Actions for Buttons
          6. 16.7.2.6. The createStatus() Revisited
          7. 16.7.2.7. JTable: The Workhorse of Data Display
          8. 16.7.2.8. Table Model
          9. 16.7.2.9. Renderers
          10. 16.7.2.10. Selection Listeners
          11. 16.7.2.11. Ready, aim, fire!
          12. 16.7.2.12. Scrolling
          13. 16.7.2.13. Dialogs
      8. 16.8. Review
      9. 16.9. What You Still Don’t Know
      10. 16.10. Resources
      11. 16.11. Exercises
    2. 17. Other Ways: Alternatives to Swing
      1. 17.1. What You Will Learn
      2. 17.2. The IBM SWT Toolkit
        1. 17.2.1. Another GUI Toolkit. Why?
        2. 17.2.2. Duplicated Effort. Why Cover It?
        3. 17.2.3. Portability: Better and Worse
        4. 17.2.4. The Rest of the Chapter
        5. 17.2.5. SWT: Close to the Metal
        6. 17.2.6. “Hello, world” SWT Style
          1. 17.2.6.1. Setting Up to Run an SWT Application
          2. 17.2.6.2. Anatomy of an SWT Application
      3. 17.3. Porting BudgetPro to SWT
        1. 17.3.1. Step 1: Convert the Class Members
        2. 17.3.2. Step 2: Converting the main() Method
        3. 17.3.3. Step 3: Converting the GUI build() and init() Methods
          1. 17.3.3.1. Converting the GUI build() Method
          2. 17.3.3.2. Converting the GUI init() Method
          3. 17.3.3.3. Reworking Event Handlers
        4. 17.3.4. Completing the Conversion of the BudgetPro Class
          1. 17.3.4.1. The Table, TableColumn, and TableItem Classes
        5. 17.3.5. Completing the Conversion of the Application
        6. 17.3.6. Closing Thoughts
      4. 17.4. SWT and gcj
      5. 17.5. Review
      6. 17.6. What You Still Don’t Know
      7. 17.7. Resources
      8. 17.8. Exercises
  8. IV. Developing Web Interfaces
    1. 18. Servlets: Java Pressed into Service
      1. 18.1. What You Will Learn
      2. 18.2. Servlets: Program-Centric Server-Side Documents
      3. 18.3. Perspective
      4. 18.4. How to Write a Servlet
      5. 18.5. Input, Output
      6. 18.6. Matters of State: Cookies, Hidden Variables, and the Dreaded “Back” Button
        1. 18.6.1. Cookies
      7. 18.7. Designing a BudgetPro Servlet
        1. 18.7.1. Prototype
        2. 18.7.2. Design
      8. 18.8. Review
      9. 18.9. What You Still Don’t Know
      10. 18.10. Resources
      11. 18.11. Exercises
    2. 19. JSP: Servlets Turned Inside Out
      1. 19.1. What You Will Learn
      2. 19.2. Servlets Turned Inside Out: JSP
      3. 19.3. How to Write a JSP Application
        1. 19.3.1. Scriptlet
        2. 19.3.2. Declaration
        3. 19.3.3. Expression
        4. 19.3.4. Directive
        5. 19.3.5. New Syntax
        6. 19.3.6. JavaBeans in JSP
        7. 19.3.7. Tag Libraries
      4. 19.4. Using JSP with BudgetPro
      5. 19.5. Review
      6. 19.6. What You Still Don’t Know
      7. 19.7. Resources
      8. 19.8. Exercises
    3. 20. Open Source Web Application Servers
      1. 20.1. What You Will Learn
      2. 20.2. Downloading JBoss
      3. 20.3. Be an Enabler, or “Let’s Be Codependent!”
        1. 20.3.1. Nonroot-Installed Software
        2. 20.3.2. Finer Grained Control
      4. 20.4. Installing JBoss
      5. 20.5. Things That Make It Go
        1. 20.5.1. System V Init System
        2. 20.5.2. RedHat/Fedora chkconfig
        3. 20.5.3. Other Distributions
        4. 20.5.4. IDE Integration
      6. 20.6. Disposition of Forces
      7. 20.7. Apache Geronimo
      8. 20.8. Installing Geronimo
      9. 20.9. Running the Geronimo Server
      10. 20.10. Review
      11. 20.11. What You Still Don’t Know
      12. 20.12. Resources
  9. V. Developing Enterprise Scale Software
    1. 21. Introduction to Enterprise JavaBeans
      1. 21.1. What You Will Learn
      2. 21.2. Expanding to EJBs
        1. 21.2.1. EJB Concepts
          1. 21.2.1.1. The Life Cycle of an Enterprise JavaBean
            1. Creation
            2. Destruction
            3. Passivation and activation
            4. Context switching
          2. 21.2.1.2. The EJB Container
        2. 21.2.2. Bean Types
          1. 21.2.2.1. Session Beans
            1. Stateless Session Beans
              1. What they are
              2. Why you would use them
            2. Stateful Session Beans
              1. What they are
              2. Why you would use them
          2. 21.2.2.2. Entity Beans
            1. What they are
            2. Why you would use them
          3. 21.2.2.3. Message Beans
            1. What they are
            2. Why you would use them
        3. 21.2.3. Under the Hood
      3. 21.3. What’s in a Name? An Introduction to JNDI
        1. 21.3.1. Naming and Directory System Concepts
        2. 21.3.2. Common Directory Services
          1. 21.3.2.1. Domain Name Service (DNS)
          2. 21.3.2.2. Filesystems
          3. 21.3.2.3. LDAP
          4. 21.3.2.4. Novell Directory Service (NDS)
          5. 21.3.2.5. Microsoft’s Active Directory
        3. 21.3.3. Putting a Face to a Name: JNDI
          1. 21.3.3.1. A Sample JNDI Program
            1. The GetDomain main() method
            2. Establishing an initial context
            3. Going from the initial context to a DNS entry
          2. 21.3.3.2. Learning More about JNDI
        4. 21.3.4. Using JNDI with JBoss
      4. 21.4. Review
      5. 21.5. What You Still Don’t Know
      6. 21.6. Resources
    2. 22. Building an EJB
      1. 22.1. What You Will Learn
      2. 22.2. EJBs: You Don’t Know Beans?
        1. 22.2.1. SessionBean
        2. 22.2.2. EJBObject
        3. 22.2.3. EJBHome
        4. 22.2.4. Summarizing the Pieces
        5. 22.2.5. EJBLocalHome and EJBLocalObject
        6. 22.2.6. Compiling Your Beans
      3. 22.3. Review
      4. 22.4. What You Still Don’t Know
      5. 22.5. Resources
    3. 23. Deploying EJBs
      1. 23.1. What you Will Learn
      2. 23.2. Lend Me Your EAR: Enterprise Packaging and Deployment
        1. 23.2.1. What’s in an EJB-JAR File
        2. 23.2.2. Using Our Bean
        3. 23.2.3. Packaging the Servlet
          1. 23.2.3.1. What is in the WAR File
          2. 23.2.3.2. Weaving the Web
          3. 23.2.3.3. Connecting the Pieces
      3. 23.3. Deploying the EAR
        1. 23.3.1. JBoss
        2. 23.3.2. Geronimo
      4. 23.4. Maintaining a Distributed Application
        1. 23.4.1. Ant and CVS
        2. 23.4.2. XDoclet
      5. 23.5. Abstracting Legacy Applications
      6. 23.6. Review
      7. 23.7. What you Still Don’t Know
      8. 23.8. Resources
    4. 24. Parting Shots
      1. 24.1. The Future’s So Bright, I Squint and Look Confused
      2. 24.2. Our Book Is Yours
      3. 24.3. Came the Revolution
      4. 24.4. What You Still Don’t Know
      5. 24.5. Resources
    5. A. ASCII Chart
    6. B. A Java Swing GUI for BudgetPro
    7. C. GNU General Public License
      1. Preamble
      2. Terms and Conditions for Copying, Distribution, and Modification
      3. No Warranty
      4. How to Apply these Terms to Your New Programs