Aspectj Cookbook

Recipe 3.1. Deploying a Command-Line AspectJ Application

Problem

You want to deploy a simple AspectJ application to be deployed and then run from the command line.

Solution

A straightforward Java application usually requires nothing more than a Java Runtime Environment on the target machine and the classes of your application added to the Java classpath before you can run your application.

AspectJ requires that the Java Runtime Environment be at Version 1.1 or later, and it needs the additional aspectjrt.jar library added to the classpath to support the aspect-oriented features of your AspectJ application.

Discussion

Using the simple application developed in Recipe 2.2, the following steps create a directory containing all of the necessary deployment files needed to run your AspectJ application:

  1. Create a new directory to contain your runtime deployment that is separate from your source directories. For this example, name the directory deployment.

  2. Create a subdirectory of the top-level deployment directory and name it classes.

  3. Place all of your application's compiled .class files in the classes directory, taking care to maintain your package directory structure. For this example, the code from Recipe 2.2 is being used, so the directory structure should look like Figure 3-1.

    Figure 3-1. The directory structure after you have placed the .class files within your deployment area

    You can perform this by either manually copying each of the packages of .class files or by building your application with the ajc tool with the destination flag, -d, set to the classes directory:

    > ajc -classpath %MY_CLASSPATH% -d %PROJECT_ROOT_DIRECTORY%/deployment/classes com/oreilly/aspectjcookbook/MyClass.java com/oreilly/aspectjcookbook/HelloWorld. java

    If you get an error that states that aspectjrt.jar cannot be found, then check out Recipe 2.2 where the instructions on how to use the ajc command explain how to overcome this problem.

  4. Create a subdirectory beneath the top level deployment directory called lib.

  5. Copy the %ASPECTJ_INSTALLATION_DIRECTORY%/lib/aspectjrt.jar file to the new lib directory.

  6. Your final deployment setup should look something like that shown in Figure 3-2.

    Figure 3-2. The directory structure after the runtime AspectJ library has been added

  7. You can copy the deployment directory to any target machine with a compatible Java Runtime Environment correctly installed and you should be able to run your application using the java command:

    java -classpath %MY_CLASSPATH% com.oreilly.aspectjcookbook.MyClass

    The classpath on your target machines must contain aspectjrt.jar within the deployment/lib directory and point to the classes directory. For example if you are running the java command from within the deployment directory then your classpath content would be something like classes/.:lib/aspectjrt.jar remembering to replace : with ; on Windows.

See Also

Recipe 2.2 covers the basics of how to use the ajc command to compile your AspectJ applications; Java in a Nutshell by David Flanagan (O'Reilly) provides detailed information on the entire set of Java command-line tools including java.

Категории