|
Learning Java by Example
by Shannon O'Donnell
[The code for this article is available for download.]
Sometimes all you want is for someone to simply show you how to do something because you don't have
the time to research it on your own. Sometimes we all feel that way, because we have way too much to do.
So what the heck? Let's take one article and forget about the how's and the why's and instead concentrate
only on the how-to. In this article, I'm going to show you how to use Java to accomplish some useful and
unique tasks.
Executing Commands on a PC Using Java
Here's one way to execute PC commands from Java. Compile the sample Java application on your PC and
run it by typing the following:
java ExecuteCommands <Path_Command>
Where "Path" is the path (i.e., C:\winnt), and "Command" is the executable file you want to run (i.e.,
NOTEPAD.EXE).
ExecuteCommands.txt
Displaying AS/400 Spool Files from a PC
Want to display spool files from the AS/400 on your user's PC, from within a Java application? Let the
iSeries Toolbox for Java VACESS classes named AS400DetailsPane and VprinterOutput do all the hard
work for you. Here's how.
Compile the Java code and execute it by typing the following:
java AS400SpooledFilesDemo
You'll be prompted to log on to your iSeries, and the spool files for the user who logged on will be
displayed in an AS400DetailsPane.
AS400SpooledFilesDemo.txt
Creating a New Image Based on an Existing Image
If you've ever had occasion to duplicate JPEG image files, you know that you can do so in a variety of
ways. You could, for example, use Windows Explorer to copy one image to another, or you could use a
photo editor such as Paint Shop Pro. But what if you need to programmatically duplicate an image? Once
again, Java comes to the rescue!
This Java application will accept the name of an existing image file (JPEG, GIF), and the name
of the new JPEG file to be created from the old image; it then creates the new JPEG based on the existing
image file.
Compile the source code and run the Java application:
java createNewImageFromOldImage pathOldImage pathNewImage
Where "path" is the qualified path to the image file (i.e., C:\), "OldImage" is the name of an existing image
file (i.e., Drawing.JPG), and "NewImage" is the name of the new image file you want to create (i.e,
DuplicateJPG).
createNewImageFromOldImages.txt
Just the Beginning
It's amazing what you can do with Java. These three short examples are merely the tip of the iceberg. Use
these examples as a beginning for building your own cool Java applications. And if you come up with
something really unique, send it to me at sodonnell@itjungle.com and we might even
publish it!
Editor's Note: This article has been edited since its original publication. The article incorrectly
stated that the command java DisplayAS400SpooledFiles should be used to display spool files from the AS/400 on a
user's PC, from within a Java application. The command is, in fact, java AS400SpooledFilesDemo.
Guild Companies regrets the errors. [Corrections made 4/25/02]
|