Example: Using CommandCall
This IBM® Toolbox for Java™ example program prompts the user for the name of the server and the command to run, then prints the result of the command.
Note: Read the Code
example disclaimer for important legal information.
//////////////////////////////////////////////////////////////////////////////////
//
// Command call example. This program prompts the user
// for the name of the server and the command to run, then
// prints the result of the command.
//
// This source is an example of "CommandCall"
//
//////////////////////////////////////////////////////////////////////////////////
import java.io.*;
import java.util.*;
import com.ibm.as400.access.*;
public class CommandCallExample extends Object
{
public static void main(String[] parmeters)
{
// Created a reader to get input from the user
BufferedReader inputStream = new BufferedReader(new InputStreamReader(System.in),1);
// Declare variables to hold the system name and the command to run
String systemString = null;
String commandString = null;
System.out.println( " " );
// Get the system name and the command to run from the user
try
{
System.out.print("System name: ");
systemString = inputStream.readLine();
System.out.print("Command: ");
commandString = inputStream.readLine();
}
catch (Exception e) {};
System.out.println( " " );
// Create an AS400 object. This is the system we send the command to
AS400 as400 = new AS400(systemString);
// Create a command call object specifying the server that will
// recieve the command.
CommandCall command = new CommandCall( as400 );
try
{
// Run the command.
if (command.run(commandString))
System.out.print( "Command successful" );
else
System.out.print( "Command failed" );
// If messages were produced from the command, print them
AS400Message[] messagelist = command.getMessageList();
if (messagelist.length > 0)
{
System.out.println( ", messages from the command:" );
System.out.println( " " );
}
for (int i=0; i < messagelist.length; i++)
{
System.out.print ( messagelist[i].getID() );
System.out.print ( ": " );
System.out.println( messagelist[i].getText() );
}
}
catch (Exception e)
{
System.out.println( "Command " + command.getCommand() + " did not run" );
}
System.exit(0);
}
}