Example: Using JobLog to display messages in the job log

Note: Read the Code example disclaimer for important legal information.
///////////////////////////////////////////////////////////////////////////
//
// This program is an example of the job log fuction of the
// IBM Toolbox for Java.  It will display the messages in the job
// log for a job that belongs to the current user.
//
// Command syntax:
//    jobLogExample system userID password
//
// (Password is optional)
//
/////////////////////////////////////////////////////////////////////////

import java.lang.*;
import java.util.*;
import com.ibm.as400.access.*;

public class jobLogExample
{


    public static void main (String[] args)
    {
        // If a system and user were not specified, display help text and exit.
        if (args.length < 2)
        {
            System.out.println("Usage:  jobLogExample system userid <password>");
            return;
        }

        String userID = null;

        try
        {
            // Create an AS400 object.  The system name was passed
            // as the first command line argument.  If a userid
            // and password were passed on the command line,
            // set those as well.
            AS400 system = new AS400 (args[0]);

            if (args.length > 1)
            {
               userID = args[1];
               system.setUserId(userID);
            }

            if (args.length > 2)
               system.setPassword(args[2]);


            // Create a job list object.  This object will be used to get
            // the list of active jobs on the system.  Once the list of
            // jobs is retrieved, the program will find a job for the
            // current user.
            JobList jobList = new JobList(system);

            // Get the list of active jobs on the AS/400
            Enumeration list = jobList.getJobs();

            boolean Continue = true;

            // Look through the list to find a job for the current user.
            while (list.hasMoreElements() && Continue)
            {
               Job j = (Job) list.nextElement();

               if (j.getUser().trim().equalsIgnoreCase(userID))
               {
                  // A job matching the current user was found.  Create
                  // a job log object for this job.
                  JobLog jlog = new JobLog(system, j.getName(), j.getUser(), j.getNumber());

                  // Enumerate the messages in the job log then print them.
                  Enumeration messageList = jlog.getMessages();

                  while (messageList.hasMoreElements())
                  {
                     AS400Message message = (AS400Message) messageList.nextElement();
                     System.out.println(message.getText());
                  }

                  // We found one job matching the current user so exit.
                  Continue = false;
               }
            }
        }
        catch (Exception e)
        {
           System.out.println ("Error: " + e.getMessage ());
        }

        System.exit(0);
    }

}