Xiao Qing
作者Xiao Qing·2022-10-19 10:31
系统工程师·浪潮商用机器有限公司

如何用Java从客户端远程执行IBM i的命令

字数 3343阅读 4787评论 0赞 3

远程执行IBM i的命令这一特性对于开发IBM i的客户端应用具有非常重要的意义。

用Java从客户端执行IBM i的命令并不复杂,其实只需调用IBM i提供的CommandCall系统API函数就可轻松实现。

CommandCall是IBM i Java Toolbox提供的类,它以非交互方式调用IBM i的命令,也就是说不能以交互方式运行,被执行命令的返回结果可以通过AS400Message对象来获取。

CommandCall API函数的输入包括:

1.需要执行命令的字符串。

2.执行这条命令的AS/400对象。

命令字符串可以通过 CommandCall的setCommand() 方法或 run()方法在构造函数上设置。命令运行后,Java 程序使用 getMessageList()方法来检索由命令产生的IBM i消息。
下面举例来说明利用Java程序从客户端执行IBM i的命令。

下载JT400.JAR并将其解压缩:
1)从AS/400上用FTP DOWNLOAD JT400.JAR到PC端。
路径:/QIBM/ProdData/HTTP/Public/jt400/lib/jt400.jar
2)在PC端建立目录:c:\\com\\ibm\\as400
3)将jt400.jar复制到指个目录下。
4)将jt400.jar在此目录下解压缩,生成两个目录COM和META-INF
修改CLASSPATH.(以WIN2K为例)
将.;d:\\java400\\jt400.jar加入“环境变量”“CLASSPATH”中

JAVA源代码:

//////////////////////////////////////////////////////////////////////////////////
//
// Command call example. //
// This source is an example of IBM Toolbox for Java "CommandCall" 
//    Edited by XiaoQnig 2022/10
//
//////////////////////////////////////////////////////////////////////////////////

import java.io.*;
import java.util.*;
import com.ibm.as400.access.*;
public class CommandCallExample extends Object
{
   public static void main(String[] parmeters)
   {
 
      BufferedReader inputStream = new BufferedReader(new InputStreamReader(System.in),1);
 
      String systemString  = null;
      String commandString = null;
 
      System.out.println( " " );
 
      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);
   }
}

现在简单介绍一下这个样例程序,这个程序大略分三步:
第一步创建AS/400通讯对象。

AS400 as400 = new AS400(systemString);

第二步创建CommandCall类的实例,将AS/400的通讯对象传递给构造函数。

CommandCall command = new CommandCall( as400 );

第三步执行CommandCall对象的run方法,run方法接收参数然后执行,如果执行成功返回布尔值true,否则返回false。

     if (command.run(commandString))

         System.out.print( "Command successful" );

     else

         System.out.print( "Command failed" );





运行结果:

可以看到命令执行成功,xqlib2库被创建。

在执行开始阶段,会弹出Signon画面,提示输入IBM i的系统名或IP地址、用户名和口令,登录系统,执行命令。

最后到AS/400命令行执行DSPLIB XQLIB2,检查一下XQLIB2是否被创建。

如上图所示,XQLIB2已被成功创建。

仅供参考

如果觉得我的文章对您有用,请点赞。您的支持将鼓励我继续创作!

3

添加新评论0 条评论

Ctrl+Enter 发表

作者其他文章

相关文章

X社区推广