iSeries Toolbox for Java: Running an iSeries Command
by Kevin Vandever
[The code for this article is available for download.]
When it comes to accessing iSeries services from Java, there's not much that the iSeries Toolbox can't do. In previous articles, I showed you how to call programs and service programs, how to write to and read from data queues, and how to wrap GUIs around some of those services. As powerful as those tools are, they can be made even more powerful with the help of a tool that will do some of the dirty work and prepare your application environment. The CommandCall class is that tool.
The Java Code
Take a look at the Java source code, CommandCallExample. The first three lines import the necessary classes and objects that you will need. The first two are core Java APIs, and the third is a package from the iSeries Toolbox that contains the necessary access JAR files. This Java application will contain one class, as indicated by the following class definition:
public class CommandCallExample
It will also contain one method, as indicated by the following method definition:
public static void main(String parmameters)
A Java source member can contain multiple class and method definitions. Please refer to "Java Concepts for iSeries Programmers" for a more detailed discussion on classes and methods. For now, let's get on with this application.
The first thing I am doing inside the main method is to create a BufferedReader object called inputStream. There's a lot going on with this line of code, but to put it simply, think of it as defining your input parms for an RPG program. This line will allow me to accept input from the command line into a BufferedReader called inputStream and use it inside my application. If you look back at some of my other articles, I have accepted input parameters a little differently, using the parameters inside my main method definition. That works, too; however, the technique I am showing you here affords you an easy way to prompt the user for information. It also allows you to enter parameters with embedded white space (space and tab keys) and still treat the entry as one parameter.
Next, I declare some string objects that will eventually contain the input from the user. I then use a try-catch block to prompt the user for a system name and OS/400 command. The readLine method from the inputStream object will wait for input from the user. Once the user enters something on the command line, after being prompted to do so by the System.out.print line, that input will be assigned to either systemString or commandString.
Now it's time to use some tools from the toolbox. First, I am going to create an instance of the AS400 class called as400 and pass to the new object the system name or IP address entered by the user. If you've read my previous toolbox articles, you've seen this code before. I will reiterate, however, that you can also add a user ID and password to the parameter list of the as400 object, but in this example, I will let the system prompt for it. Next, I will create an instance of the CommandCall class called command and pass to it the as400 object created just prior.
Now I'm ready to run a command. To accomplish this, I use another try-catch block. Inside the try block I use the run method from the command object and pass it the command entered by the user, which has been assigned to the commandString object. You will first be shown an applet asking for your user ID and password. Once you've entered that information, if your command is successful, you will get a message stating so. If it failed, you'll get a message about that, too. Also, as an added bonus, the application will list any additional messages sent from the iSeries that will provide you more detail on why your command might have failed, or provide additional detail about your success.
To accomplish the additional message logic, I create an instance of the AS400Message class called message list, run the getMessageList method to retrieve all the messages, and then loop through the array of messages, displaying each one as I go. Again, if you've read any of my previous toolbox articles, you've seen this message logic before.
Compiling and Running
Once you've downloaded the source code, make sure to set your CLASSPATH correctly. If you're not sure what a CLASSPATH is, or if you want more information on it, check out "How Do I Set My CLASSPATH? Let Me Count the Ways." Once your CLASSPATH is set, you can compile the Java source code using the javac command:
To run your application, use the java command:
You will be prompted for a system name. You can give the known name of your iSeries or give an IP address. You will then be prompted for a command. Type your command as you would on the iSeries (minus the prompting capability). Then you will be prompted for your user ID and password in a Java applet. Type that information and away you go. You will receive messages telling you the status of your command run. You can run almost any OS/400 command, assuming you have authority. However, if you run a command that would normally present you with a green-screen display, you will receive a "successful" message, but not get any other output. If, for example, you type DSPLIBL as your command, a message will appear saying that your command was successful, but you will not get to see your library list, because OS/400 couldn't display the 5250 data stream used by the green screen. One solution is to not run commands that display output in this manner, or probably a more appropriate way is to use the *PRINT option to write the output to a printer file, like so:
Then you can use the toolbox to check out that printer file (I will write about that tool sometime soon).
If you've kept up with my series, you may be starting to build your own handy dandy Java toolbox (yes, my kids used to watch Blue's Clues) for your specific applications' needs. The command call tool makes a nice supplement to the data queue and program call applications because it can be used to setup your application environment. You may need to create temporary files, set your library list, or disable the profile of that pesky user that keeps on, uh, sorry, wrong article. Anyway, give it a try and mix it with the other tools from previous articles. And, of course, tune in for more tools.
Contact the Editors
Last Updated: 6/6/02
Copyright © 1996-2008 Guild Companies, Inc. All Rights Reserved.