Example 2. An example using -argfile/@
To avoid specifying file names on the command line,
list source files in a line-delimited text argfile.
Source file paths may be absolute or relative to the argfile,
and may include other argfiles by @-reference.
The following file sources.lst
contains absolute and relative files and @-references:
Gui.java
/home/user/src/Library.java
data/Repository.java
data/Access.java
@../../common/common.lst
@/home/user/src/lib.lst
view/body/ArrayView.java
Compile the files using either the -argfile or @ form:
ajc -argfile sources.lst
ajc @sources.lst
Argfiles are also supported by jikes and javac, so you
can use the files in hybrid builds. However, the support varies:
- Only ajc accepts command-line options
- Jikes and Javac do not accept internal @argfile references.
- Jikes and Javac only accept the @file form on the command line.
Example 3. An example using -injars and -aspectpath
Bytecode weaving using -injars:
AspectJ 1.1 supports weaving from input zip or jar files containing
class files. Using input jars is like compiling the corresponding
source files, and all binaries are emitted to output. Although
Java-compliant compilers may differ in their output, ajc should
take as input any class files produced by javac, jikes, eclipse,
and, of course, ajc. Aspects included in -injars will be woven into
like other .class files, but they will specify any crosscutting
code (i.e., they will not be woven into other types). To use
aspects in their binary form to specify crosscutting,
see -aspectpath below.
Aspect libraries using -aspectpath:
AspectJ 1.1 supports weaving from read-only libraries containing
aspects. Like input jars, they affect all input; unlike input
jars, they themselves are not affected or emitted as output.
Sources compiled with aspect libraries must be run with the same
aspect libraries on their classpath.
The following example builds the tracing example in a
command-line environment; it creates a read-only aspect library,
compiles some classes for use as input bytecode, and
compiles the classes and other sources with the aspect library.
The tracing example is in the AspectJ distribution
({aspectj}/doc/examples/tracing). This uses the following files:
aspectj1.1/
bin/
ajc
lib/
aspectjrt.jar
examples/
tracing/
Circle.java
ExampleMain.java
lib/
AbstractTrace.java
TraceMyClasses.java
notrace.lst
Square.java
tracelib.lst
tracev3.lst
TwoDShape.java
version3/
Trace.java
TraceMyClasses.java
Below, the path separator is taken as ";", but file separators
are "/". All commands are on one line. Adjust paths and
commands to your environment as needed.
Setup the path, classpath, and current directory:
cd examples
export ajrt=../lib/aspectjrt.jar
export CLASSPATH="$ajrt"
export PATH="../bin:$PATH"
Build a read-only tracing library:
ajc -argfile tracing/tracelib.lst -outjar tracelib.jar
Build the application with tracing in one step:
ajc -aspectpath tracelib.jar -argfile tracing/notrace.lst -outjar tracedapp.jar
Run the application with tracing:
java -classpath "$ajrt;tracedapp.jar;tracelib.jar" tracing.ExampleMain
Build the application with tracing from binaries in two steps:
(a) Build the application classes (using javac for demonstration's sake):
mkdir classes
javac -d classes tracing/*.java
jar cfM app.jar -C classes .
(b) Build the application with tracing:
ajc -injars app.jar -aspectpath tracelib.jar -outjar tracedapp.jar
Run the application with tracing (same as above):
java -classpath "$ajrt;tracedapp.jar;tracelib.jar" tracing.ExampleMain
Run the application without tracing:
java -classpath "app.jar" tracing.ExampleMain