I'm trying to connect to an AS/400 server using java, and would like to run some simple commands.
My imports are:
import com.ibm.as400.access.AS400;
import com.ibm.as400.access.AS400Message;
import com.ibm.as400.access.CommandCall;
This is what I have thus far:
AS400 as400 = null;
Scanner scanner = new Scanner(System.in);
try {
as400 = new AS400(host);
as400.setUserId(user);
as400.setPassword(pass);
CommandCall cmd = new CommandCall(as400);
while(true) {
System.out.println("Ready for input, type \"quit\" to end session");
String commandStr = scanner.nextLine();
System.out.println("Command entered: " + commandStr);
if(commandStr.equals("quit")) {
break;
}
System.out.println("Executing: " + commandStr);
boolean success = cmd.run(commandStr.toUpperCase());
System.out.println("Finished execution");
if (success) {
System.out.println("Command Executed Successfully.");
}else{
System.out.println("Command Failed!");
}
// Get the command results
System.out.println("Getting output");
AS400Message[] messageList;
messageList = cmd.getMessageList();
for (AS400Message message : messageList){
System.out.println(message.getText());
}
}
}catch(UnknownHostException uh){
System.out.println("Unknown host");
}catch(Exception e) {
e.printStackTrace();
}finally{
scanner.close();
try {
as400.disconnectAllServices();
}catch(Exception e) {}
}
When I try to run DSPLIBL however: I get a blank output.
Ready for input, type "quit" to end session
dsplibl
Command entered: dsplibl
Executing: dsplibl
Finished execution
Command Executed Successfully.
Getting output
Ready for input, type "quit" to end session
Everything else seems okay however. CRTLIB library name works just fine, and it returns an output message. Invalid commands also returns messages saying that the input is invalid. It's just DSPLIBL not giving me an output.
Any ideas on what is wrong?
The docs says specifically that CommandCall
allows a Java™ program to call a non-interactive IBM® i command.
DSPLIBL is an interactive command.
The "output" you're successfully returning for CRTLIB is a completion message returned by that command.
Check out the getUserLibraryList() method of the Job object