next up previous contents
Next: Communication between mobile agents Up: Executing programs Previous: Test it out   Contents

Source code

 package org.kaariboga.agents;

 import java.io.IOException;
 import java.lang.Runtime;
 import org.kaariboga.core.Kaariboga;
 import org.kaariboga.core.KaaribogaEvent;


 /**
  * The Executer is able to execute a program on a remote machine.
  * This one only works when the server is started with the
  * -soff option which disables the security manager:
  * java org.kaariboga.server.Boga -soff
  */
 public class Executer extends Kaariboga
 {
     /**
      * Runtime object of the current JVM.
      * Declared as an object varible to demonstrate the use of transient.
      */
     transient private Runtime rntime;

     /**
      * The command to be executed.
      */
     String command;

     /**
      * Creates a new executer and sets the command to execute.
      * For security reasons it is hard coded as the windows
      * paintbrush program.
      */
     public Executer (String name){
         super("Executer_" + name);
         this.command = "pbrush";
     }

     /**
      * Executes the given command if the base was started with the
      * security off option (-soff).
      */
     public void run(){
         System.out.println("Going to execute! " + command);
         rntime = Runtime.getRuntime();
         try{
             rntime.exec(command);
         }
         catch(IOException e){
             System.err.println("Error executing other program!");
         }
     }
 }
Here we use a Runtime object to execute a command.
    transient private Runtime rntime
The Runtime object is declared as transient because it can't be transfered to other machines. All objects declared as transient are ignored when the agent travels to another server. In the run method you will find the code to execute a command.
    rntime = Runtime.getRuntime();
    try{
        rntime.exec(command);
    }
    catch(IOException e){
        System.err.println("Error executing other program!");
    }
Here we get a Runtime object from the JVM and use it to execute a command.

Dirk 2002-09-01