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:
Can you explain the main concepts covered in this chapter?
Can you write code examples demonstrating these concepts?
Do you understand when and why to use these features?
Can you explain the benefits and tradeoffs?
🔄 Quick Revision Points
📝 Practice Exercises
Write your own code examples for each key concept
Modify existing examples to test edge cases
Explain concepts to someone else
Create a small project using these concepts
🔗 Related Chapters
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.javafiles.Classes Directory: Create a folder (e.g.,
classes) for your compiled.classfiles.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
classesdirectory) .
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.
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) .
Run the
jarTool: 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 (namedapp1.jar).
Running a JAR
Command Line:
java -jar app1.jar.GUI: On most systems, you can simply double-click the
.jarfile.
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 namedcom/headfirstjava.
Declaration: Put the
packagestatement 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, thenheadfirstjava, then theMyApp.classfile) .
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
The Client: The user clicks a link to a
.jnlpfile.The Browser: The browser downloads the
.jnlpfile and starts the JWS Helper App (which comes installed with Java).The Helper App: It reads the
.jnlpfile to find the executable JAR, downloads it, and runs themain()method.
The .jnlp File
.jnlp FileThe 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
JAR: Create an executable JAR of your application.
JNLP: Write the
.jnlpfile describing the app.Server: Upload both the JAR and the
.jnlpfile to your web server.MIME Type: Configure your web server to handle the
application/x-java-jnlp-fileMIME type so the browser knows what to do with it.HTML: Create a web page with a link to the
.jnlpfile.
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 usecom.domain.MyApp.Separation: Always keep source code and compiled code separate to prevent version control and deployment headaches.
Last updated