Recipe 3.2. Deploying an AspectJ Application as a Fully Contained Executable JAR File Problem You want to deploy an AspectJ application to be run as an executable JAR file. Solution Unpack the contents of the aspectjrt.jar file and then repack the AspectJ classes with your own application's classes into a single JAR file. To make the single JAR file executable, an appropriate manifest file should be included that lists the class that contains the standard public static void main(String[]) method entry point for running Java applications. Discussion Creating an executable JAR file is a popular way of deploying conventional Java applications. The JAR file format, by default, contains all of the necessary classes for a software component, and it has the potential to be configured as a packaged Java application that can be run simply by double-clicking on the file within most popular operating systems. The following steps manually create an executable JAR file for the application shown in Recipe 2.2: Take a copy of the deployment directory as it was created in Recipe 2.1. Unjar the contents of the aspectjrt.jar that is stored in the deployment/lib directory using the jar tool from the command line: jar -xvf aspectjrt.jar After the jar extraction has completed, you will find that two folders have been extracted within the deployment/lib directory, META-INF and org. The META-INF directory contains a MANIFEST.MF file containing the manifest information for the aspectjrt.jar. The org directory contains the .class files needed by the aspect-oriented mechanisms within your application to run when it is deployed. Copy the deployment/lib/org directory and all of its contents into the deployment/classes directory Create a manifest file called manifest in the new deployment directory, using a text editor, that contains similar information as that presented in Example 3-1. Example 3-1. An example of the contents of an executable .jar files manifest Manifest-Version: 1.0 Name: com/oreilly/aspectjcookbook/ Specification-Title: My simple AspectJ Application Specification-Version: 1.0 Specification-Vendor: Russ Miles Implementation-Title: com.oreilly.aspectjcookbook Implementation-Version: 1.0 Implementation-Vendor: Russ Miles Main-Class: com.oreilly.aspectjcookbook.MyClass It is important that the Main-Class information in the manifest file correctly points to the class that contains the public void main(String[]) method that starts up your application. Once you have created and saved the manifest file to the deployment directory the directory structure of deployment and deployment/classes directories should look like that shown in Figure 3-3. Figure 3-3. The content of the deployment and classes directories once you have copied over the org directory, which contains the AspectJ classes and creates a new manifest file
Create a subdirectory of the deployment directory called build. Run the following jar command from within the deployment directory to create the executable myapplication.jar file in the deployment/build directory: jar -cfm build/myapplication.jar manifest -C classes/ To run the executable .jar file, remembering that this example application is a console application and so has no GUI, you can either run the java command from the build directory using the -jar option as shown below or double-click on the myapplication.jar file. java -jar myapplication.jar If you are using Mac OS X and decide you are going to double-click the myapplication.jar, then you could run the Console application first, located in the /Applications/utilities directory, to see the output of your application running as shown in Figure 3-4. Figure 3-4. The Console application in Mac OS X showing the output of double-clicking the executable myapplication.jar
See Also Java in a Nutshell by David Flanagan (O'Reilly) provides detailed information on the entire set of Java command-line tools, including java and jar. |