Package, Jars and deployment.

Gawesh Prabhashwara
3 min readMay 6, 2022

Head First Java Edition ii Chapter xvii,

*This chapter focuses on Java deployment. There are three deployment options available.

  1. Local: entire application runs on the end-users computer as a stand-alone, probably GUI, program.
  2. Combination of local and remote: The application is distributed with a client portion running on the user’s local system.
  3. Remote: The entire Java application runs on a server system.

JAR,

*You can organize your source code and class files as sources and classes in two different directories in your project directory.

*You can save the classes files in a separate location after compilation by using the -d flag variable with the following command.

*The classes files can then be used to run your program.

*Making a JAR executable,

Make sure all of your class files are in the classes directory.

2. Create a manifest.txt file that states which class has the main() method.

3.Run the jar tool to create a JAR file that contains everything in the classes directory, plus the manifest.

Put your classes in Packages,

*Packages can help you prevent name conflicts, but only if you give them names that are distinct. Prefixing your packages with your reverse domain name is the easiest approach to do this.

*A fully qualified name is the name of a class that includes the full directory of the class(package name and class name ) like java.util.ArrayList

*Packages name is just like reverse of the domain name.

*com. package-name and out it as the first statement in the class file.at the same time, the compiled class file must be in the same directory structure.

How to put a class in a package

  1. Choose a package name.
  2. Put a package statement in your class.
  3. Set up a matching directory structure.

*A class must be put in a directory structure that corresponds to the package hierarchy.

Making an executable JAR with packages,

*Make sure that all of your class files are in the right package, under the classes directory.

*Create a manifest.txt file that specifies which class contains the main() method, using the fully-qualified class name.

*To make a JAR file with the package directories and the manifest, use the jar tool.

Java Web Start,

*JWS allows you to download and run Java applications on your local machine from the web.

*Your application is launched for the first time from a Web browser with Java Web Start (JWS), but it operates as a stand-alone application (well, almost), free of the limits of the browser. The Java Virtual Machine (JVM) is a small Java application that manages the downloading, updating, and launching of your Java Web Start apps.

How Java Web Start works,

  1. The client clicks on a Web page link to your JWS application (a .jnlp file).
  2. The Web server (HTTP) gets the request and sends back a .jnlp file (this is NOT the JAR).
  3. Java Web Start is started up by the browser. The JWS helper app reads the .jnlp file, and asks the server for the MyApp.jar file.
  4. The Web server ‘serves’ up the requested .jar file.
  5. Java Web Start gets the JAR and starts the application by calling the specified main( ) method.

Reference,

[1]Sierra, Kathy, and Bert Bates. “Head First Java™ Second Edition.” (2021)

--

--