Release Your Code — Chapter 17
After writing, testing and refining your code you need to provide it to the end users. This chapter looks at how to organize, package, and deploy your Java code. It will look into local, semi-local, and remote deployment options including executable jars, JavaWeb Start, RMI, and Servlets. Also, organizing and packaging your code (regardless of the ultimate deployment choice) will also be looked at. After which would be the phase of maintenance.
A Java program is a bunch of classes. That’s the output of your development. The process of deployment consists of a bunch of steps.
- Local — The entire application runs on the end-user’s computer, as a stand-alone, probably GUI, program, deployed as an executable JAR
- Combination of local and remote — The application is distributed with a client portion running on the user’s local system, connected to a server where other parts of the application are running
- Remote — The entire Java application runs on a server system, with the client accessing the system through some non-Java means, probably a web browser.
Packges can prevent name conflicts, but only if you choose a package name that’s guaranteed to he unique, The best way to do that is to preface your packages with your reverse domain name. You must put a class into a directory structure that matches the package hierarchy. The -d flag tells the complier, “Put the class into its package directory structure, using the class specified after the -d as the root directory. If the directories aren’t there, create them first and then put the class In the right place”.
Making an executable JAR with packages
- Make sure all of your class files are within the correct package structure, under the classes directory.
- Create a manifest.txt file that states which class has the main() method, and be sure to use the fully-qualified class name. Put the manifest tile into the classes directory.
- Run the Jar tool to create a JAR file that contains the package directories plus the manifest. The only thing you need to include is the ‘com’ directory, and the entire package (and all classes) .
Few more important points about deployment
- Organize your project so that your source code and class files are not in the same directory.
- A standard organization structure is to create a project directory, and then put a source directory and a classes directory inside the project directory.
- Organizing your classes into packages prevents naming collisions with other classes. If you prepend your reverse domain name on to the front of a class name. To put a class in a package, put a package statement at the top of the source code file, before any import statements
- To be in a package, a class must be in a directory structure that exactly matches the package structure. For a class, com.wickedlysmartFoo. the Foo class must be in a directory named wickedlysmart (which is in a directory named com)
- To make your compiled class land in the correct package directory structure under the classes directory, use the -d compiler flag.
- To run your code, cd to the classes directory, and give the fully-qualified name of your class.
- You can bundle your classes into JAR (Java Archive) flies.
- You can make an executable JAR file by putting a manifest into the JAR that states which class has the main() method. To create a manifest file, make a text file with an entry.
- The entire package directory structure (and only the directories matching the package) must be immediately inside the JAR file.
- To run an executable JAR file, type:
java -jar MyJar.jar
Java Web Start technology lets you deploy a stand-alone client application from the Web. Java Web Start includes a ‘helper app’ that must be installed on the client (along with Java). A Java Web Start (JWS) app has two pieces: an executable JAR and a .jnlp file. A.jnlp file is a simple XML document that describes your JWS application. It includes tags for specifying the name and location of the JAR, and the name of the class with the main() method. When a browser gets a .jnlp file from the server (because the user clicked on a link to the .jnlp file), the browser starts up the JWS helper app. The JWS helper app reads the .jnlp file and requests the executable JAR from the Web server. When the JWS gets the JAR, it invokes the main() method as specified inthe .jnlp file.