githubEdit

Ch 17: Release Your Code

Source: Head First Java, Second Edition | Pages: 615-640

🎯 Learning Objectives

Packages, JARs, and deployment

📚 Key Concepts

  • Package naming

  • Creating packages

  • Compiling with packages

  • JAR files

  • Creating JARs

  • Manifest file

  • Executable JARs

  • Java Web Start


📖 Detailed Notes

1. Package naming

Essential concept for mastering Java and OOP.

Example:

2. Creating packages

Essential concept for mastering Java and OOP.

Example:

3. Compiling with packages

Essential concept for mastering Java and OOP.

Example:

4. JAR files

Essential concept for mastering Java and OOP.

Example:

5. Creating JARs

Essential concept for mastering Java and OOP.

Example:

6. Manifest file

Essential concept for mastering Java and OOP.

Example:

7. Executable JARs

Essential concept for mastering Java and OOP.

Example:

8. Java Web Start

Essential concept for mastering Java and OOP.

Example:


💡 Important Points to Remember

  • way back in chapter 6 when we discussed how

  • everything in this chapter assumes that the

  • when we say JAR in all caps, we’re referring


✅ Self-Check Questions

Test your understanding:

  1. Can you explain the main concepts covered in this chapter?

  2. Can you write code examples demonstrating these concepts?

  3. Do you understand when and why to use these features?

  4. Can you explain the benefits and tradeoffs?

🔄 Quick Revision Points


📝 Practice Exercises

  1. Write your own code examples for each key concept

  2. Modify existing examples to test edge cases

  3. Explain concepts to someone else

  4. Create a small project using these concepts

Review related concepts from other chapters to build comprehensive understanding.


For complete details, diagrams, and all examples, refer to Head First Java Second Edition, pages 615-640.

Chapter 17: Release Your Code — Study Notes

This chapter focuses on the final stage of development: Deployment. It covers how to organize your files, package them into a single executable file (JAR), and distribute them to users, either locally or over the web using Java Web Start.


1. Deployment Options

Once your code is finished, you need to get it into the user's hands. There are two primary ways to deploy a Java application:

  • Local Deployment (Executable JAR): The user runs the application solely on their local machine.

  • Remote Deployment (Java Web Start): The application is launched from a web browser, but it runs as a standalone application on the user's machine.


2. Organizing Your Code

Before deployment, you should separate your source code (.java files) from your compiled code (.class files) to keep your project clean.

  • Source Directory: Create a folder (e.g., source) for your .java files.

  • Classes Directory: Create a folder (e.g., classes) for your compiled .class files.

  • Compiling: Use the -d (directory) flag to tell the compiler where to put the output.

    Bash

    (This command tells the compiler to put the resulting class files into the classes directory) .


3. JAR Files (Java ARchives)

A JAR file is a single file that holds all your application's classes and resources (like images and sounds). It effectively "zips" your project into one convenient package.

Making an Executable JAR

To make a JAR "executable" (so the user can double-click it to run), you must tell the JVM which class contains the main() method.

  1. Create a Manifest File: Make a text file (e.g., manifest.txt) containing the following line:

    Plaintext

    (Note: You must hit return after this line so the file doesn't end on the text) .

  2. Run the jar Tool: Use the command line to create the JAR.

    Bash

    • -c: Create a new archive.

    • -v: Verbose (print details to screen).

    • -m: Include manifest information.

    • -f: Output to a file (named app1.jar).

Running a JAR

  • Command Line: java -jar app1.jar.

  • GUI: On most systems, you can simply double-click the .jar file.


4. Packages

Packages are used to prevent naming collisions. If you write a class named Account and someone else writes a class named Account, packages distinguish them (e.g., com.mybank.Account vs. com.otherbank.Account).

Rules for Packages

  • Reverse Domain Name: The standard convention is to use your domain name in reverse (e.g., com.headfirstjava.projects).

  • Directory Structure: The directory structure MUST match the package structure.

    • If your class is in package com.headfirstjava, the source file must be in a directory named com/headfirstjava.

  • Declaration: Put the package statement at the very top of your source file.

    Java

Compiling and Running with Packages

  • Compiling: You must be in the directory above the package root.

    Bash

  • Running: You must use the fully qualified name.

    Bash

    (The JVM looks inside the current directory for a folder named com, then headfirstjava, then the MyApp.class file) .


5. Java Web Start (JWS)

JWS allows users to launch your application by clicking a link on a web page. Once downloaded, the app runs outside the browser, just like a standard application.

How it Works

  1. The Client: The user clicks a link to a .jnlp file.

  2. The Browser: The browser downloads the .jnlp file and starts the JWS Helper App (which comes installed with Java).

  3. The Helper App: It reads the .jnlp file to find the executable JAR, downloads it, and runs the main() method.

The .jnlp File

The Java Network Launch Protocol file is an XML document that describes your application. It includes:

  • The location of the JAR file on the web server.

  • The name of the class with the main() method.

Steps to Deploy JWS

  1. JAR: Create an executable JAR of your application.

  2. JNLP: Write the .jnlp file describing the app.

  3. Server: Upload both the JAR and the .jnlp file to your web server.

  4. MIME Type: Configure your web server to handle the application/x-java-jnlp-file MIME type so the browser knows what to do with it.

  5. HTML: Create a web page with a link to the .jnlp file.


6. Revision Checklist

  • java -d: Remember that this flag builds the directory structure for you if it doesn't exist.

  • Manifest newline: The most common mistake when making a JAR is forgetting the newline at the end of the manifest file.

  • Fully Qualified Names: Once a class is in a package, you can no longer refer to it as just MyApp. You must use com.domain.MyApp.

  • Separation: Always keep source code and compiled code separate to prevent version control and deployment headaches.

Last updated