Hello, World!
This tutorial starts out with the classic "Hello, world!" example.
The HelloWorldAgent is a simple example of the correct form for writing an agent. Using your favorite text editor, create a new file named HelloWorldAgent.java and type (or paste) the following source code for the HelloWorldAgent.
import com.cometway.ak.Agent;
/** Everyone's favorite written as an agent. */
public class HelloWorldAgent extends Agent
{
public void start()
{
println("Hello, world!");
}
}
The class HelloWorldAgent needs to extend a class called Agent from the com.cometway.ak package and includes an import statement for this specific class. The Agent class provides for a start() method where the agent will begin execution. The HelloWorldAgent overrides this with its own start method that prints the message "Hello, world!" to the console before exiting.
The println method is a convenience method provided by the Agent class that is reserved for reporting important events to the console.
Compiling the HelloWorldAgent
Now that you have created the HelloWorldAgent.java source file containing the source code listed above, the next thing you will need to do is compile it. The process of compiling checks your code for errors and then outputs a .class file to your ~/classes directory. The following output demonstrates using the jc command to compile
HelloWorldAgent.java from a UNIX command-line:
[localhost:ak/demos/helloworld] cometdev% jc HelloWorldAgent.java
Compiling...
HelloWorldAgent.java
[localhost:ak/demos/helloworld] cometdev%
At this point, your HelloWorldAgent should be compiled. If you did not receive any errors after using the jc HelloWorldAgent.java command, you should find the file HelloWorldAgent.class created by the jc script in your ~/classes directory.
You can use the following command to see if a class file was generated:
[localhost:ak/demos/helloworld] cometdev% ls -l ~/classes
total 8
-rw-r--r-- 1 cometdev staff 329 Apr 4 15:31 HelloWorldAgent.class
[localhost:ak/demos/helloworld] cometdev%
If you don't see the HelloWorldAgent.class file, it is probably because you have compiler errors. Read the error output carefully, and correct the problems in the HelloWorldAgent.java file before trying jc again. Don't worry if you get errors the first time -- compiling code is often a trial and error process.
Expert Note:
The jc command is actually a shell script that makes it easier to use the javac command. It works by directing the output of all class files to a central directory. This helps you separate your class files from your source files, avoiding accidental deletion or source. Also, you will only need your ~/classes directory on your CLASSPATH, reducing the number of CLASSPATH problems you
might have.
The jc script is installed in your ak/bin/ directory and is the command-line equivalent to:
cometdev% javac -d ~/classes -classpath ~/classes HelloWorldAgent.java
Running the HelloWorldAgent
Now that your agent has been successfully compiled, we are ready to give it a trial run. The Agent Kernel is responsible for creating and executing your agent, but first we need a way to tell it which agent needs to be run. Since we only need to run the very simple HelloWorldAgent, we can use this simple command to get it going:
[localhost:ak/demos/helloworld] cometdev% ak HelloWorldAgent
[AK] Comet Way Agent Kernel 2.1 Final 4-10-2003
[000_AgentKernel] Starting on 2003/04/04 15:58:52.296 EST
[000_AgentKernel] Creating agent com.cometway.xml.XMLStartupAgent
[001_XMLStartupAgent] Starting on 2003/04/04 15:58:52.326 EST
[001_XMLStartupAgent] Loading agents from ak.xstartup
[001_XMLStartupAgent] Loading agents from .
[000_AgentKernel] Creating agent HelloWorldAgent
[1003_HelloWorldAgent] Starting on 2003/04/04 15:58:52.359 EST
[1003_HelloWorldAgent] Hello, world!
[localhost:ak/demos/helloworld] cometdev%
If your output looks like the output above, congratulations, you've written your first agent!
The ak command is always used to start the Agent Kernel. In its simplest form, it takes the classnames of agents as parameters. Each specified agent is executed in the order it was specified. When you use the ak command, its output first displays which version of the Comet Way Agent Kernel you are using, followed by internal output from the Agent Kernel. You will see when your HelloWorldAgent is created and started, and shortly thereafter, your agent's greeting.
You will notice that the "Hello, world!" is preceded by a tag that reads "[1003_HelloWorldAgent]". This tag is output everytime your agent uses its println() method and is how you can quickly find and follow its output.
Expert Note:
The ak script is a convenience tool that makes it easier to run the startup the Agent Kernel. It automatically configures the Java CLASSPATH by loading classes from the ak.jar file and your ~/classes directory.
The ak script is installed in your ak/bin/ directory and is the command-line equivalent to:
cometdev% java -classpath ~/classes:~/projects/ak/ak.jar com.cometway.ak.AK HelloWorldAgent
Description: This is a basic Hello, world! example.
Updated: Mon May 19 06:31:26 EDT 2008