Recipe3.2.Deploying an AspectJ Application as a Fully Contained Executable JAR File


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:

  1. Take a copy of the deployment directory as it was created in Recipe 2.1.

  2. 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

  3. 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.

  4. Copy the deployment/lib/org directory and all of its contents into the deployment/classes directory

  5. 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.

  6. 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


  7. Create a subdirectory of the deployment directory called build.

  8. 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/

  9. 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

  10. 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.



AspectJ Cookbook
Aspectj Cookbook
ISBN: 0596006543
EAN: 2147483647
Year: 2006
Pages: 203
Authors: Russ Miles

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net