|
Java Concepts for iSeries Programmers
by Kevin Vandever
In this, just our third, issue of Midrange Programmer, OS/400 Edition, my co-editor, Shannon O'Donnell,
and I, with help from many talented others, have thrown a lot of good technical information at you. Much of this
information has been centered around Java and Java-related tools. So instead of giving you a half-melted box of
stale chocolates for Valentine's Day, I thought I would cover the basics of Java and provide you with some
definitions and simple examples, so that you can properly handle the more advance techniques. Besides, Shannon
took the chocolates and has already managed to smear them all over his keyboard.
Java Basic Terms
Before we get too deeply into it, I will define some very general, Java-related terms that you will hear and see over
and over again as you delve into the Java world:
Java is a language based loosely on the C, C++, and Smalltalk languages. It was developed at Sun Microsystems in 1991, but not as an IT language; rather, it was
designed to run on intelligent consumer electronics such as toasters and wristwatches.
The .java extension lets you know that the file is a Java source file. You must compile the *.java source
file to create a Java *.class file (i.e., byte code).
The .class extension tells you the file is a binary representation of a Java source file. It is very similar to
an object on the AS/400, such as a *PGM or *FILE object. A *.class file is made up of byte code, which is nothing
more than the compiled Java source file. (More about classes later in the article.)
You will use a Java Development Kit to build your own Java applications. A JDK consists of the core
Java APIs (classes). JDKs are available in a variety of releases (such as 1.1.8, 1.2, and 1.3). You can look at a Java
release much like the release of a new version of OS/400.
A Java Runtime Environment differs from a JDK, in that JREs only offer the runtime, not the compile
time or development, environment.
The Java Virtual Machine is an operating system, so to speak, under which the JRE and JDK can run.
The JVM runs over other operating systems, which allows Java's byte code to run on that platform, too. OS/400
even goes a step further. Using the Create Java Program (CRTJVAPGM) command, you can compile java byte code
into a native service program, which will run more efficiently. The byte code remains in tact, in case you want port
the code to another platform.
A Java application is a stand-alone program, much like an RPG program you'd write for the
AS/400.
A Java applet is also a stand-alone program that runs only in a client's browser.
A Java servlet is a stand-alone program that runs on the server side of the application and receives
requests from, or sends data to, the client.
A JavaServer Page is much like HTML, in that it is tag-based and runs on the client's Web browser. It
contains static content, such as HTML and XML, as well as dynamic content, called JSP elements. (For a closer
look at JSPs, check out "JavaServer Pages 101".)
Java 2 Enterpise Edition is the latest Java software development kit. It's synonymous with JDK for
developing and deploying multi-tier enterprise applications using a set of standard, modular-based components and
the tools to service those components.
An Enterprise JavaBean is a Java class that is part of the J2EE standard. Its purpose is to deal with
complex issues in your enterprise application, such as security, database access, and transactional handling.
The CLASSPATH tells the JVM where to find the Java classes you want to run or import on a compile.
It is much like the library list on an iSeries.
A Java Archive file is a compression file used to package Java classes, documentation, and anything else
that is needed for the particular application. A JAR file is the same as a standard Zip file.
The Web Archive file was introduced with J2EE as an effective way to package Web applications. WAR
files contain all the necessary Web resources, such as JSPs, static HTML pages, and other client-related files, as well
as client-side classes, such as Java applets and server-side utility classes, like Java Beans and shopping carts. WAR
files have a specific structure that adheres to the standard set by J2EE.
An Enterprise Archive file is the final step in packaging and deploying (installing) an enterprise
application. Like a WAR file, an EAR file has a specific structure set by J2EE. It is made up of WAR files and JAR
files necessary for your enterprise application. You will probably not deal with EARs and WARs very much until
you develop and deploy a large Web/enterprise application, but at least you'll know what they are.
Object-Oriented Programming Concepts
Programming languages are programming languages, right? Once you learn the syntax of a new language, all the
rest of the rules apply, such as looping, conditioning, and mathematical operations. The same is true for Java. It is a
programming language that does the same types of things that other programming languages do, so I am not going
to cover that stuff here. However, there is something that makes Java different than most other programming
languages, and that is object-oriented programming. It is very important that you understand OO concepts, if you
really want to maximize Java's capabilities. With that said, I am going provide some more terms (sorry!) and a little
explanation of each:
Object--Well, you can't have OO technology without objects, can you? Sun defines an object as "a software bundle of variables and related
methods." An object possesses state and behavior. State is contained in an object's variables, while behavior is
contained in an object's methods. A method is nothing more than a function or subroutine inside a class that helps
determine state. I realize this may be overused, but objects exist all around us, and OO has patterned its objects to
behave like real-world objects. A poodle is an object; its color, number of legs, and demeanor are all part of its
state. Its behavior is defined by things it can do, such as bark, eat, play, and chew up the legs on your antique table.
Java objects contain variables to hold the state and methods to define the behavior.
Class--A class is defined by Sun as "a blueprint, or prototype, that defines the variables and the
methods common to all objects of a certain kind." So, continuing my poodle example, if the poodle is the object,
dog is the class; that is, poodles belong to a class of dogs. Classes are the blueprints and objects are instances of that
blueprint. In the Java world, you compile classes, and most of the time you don't refer to those classes; you refer to
instances of those classes--the objects. I say most of the time because, unlike in the real world, where, for example,
you wouldn't live in the blueprint of a house, there are cases when you do access the class itself instead of the
object. (I'll explain that in a bit.)
Messages--Messages are simply the way to allow Java objects to communicate with one another.
Inheritance--You have probably heard this term bantered around quite a bit. Inheritance is a key
component of OO technology. It allows classes to be created from other classes and inherit the state and behavior of
the source class. The new class is called the subclass, and the class the subclass was created from is the superclass.
Subclasses inherit variables and methods from the superclass, but they are not limited to them. They can have their
own set of variables and methods that help further define the class and make it more specific than the superclass. An
often-used analogy to the real world is that of the bicycle. If a bicycle represents the superclass, then roads,
mountains, and tandem bikes represent the subclass. While they all inherited state and behavior from the bicycle,
they have their own states and behaviors that make them unique. For example, a tandem bicycle has two seats.
Interface--While inheritance is probably the most oft-bantered OO term, interface is definitely the least;
however, it is just as important and should be understood as such. An interface is a device, or protocol, in which two
unrelated objects interact with each other. This interface must be agreed upon by both objects. For a bicycle class to
be able to interact with a sales program at a retail store, there has to be an interface. You don't want to create
variables and methods inside your bicycle class that don't really have much to do with the actual bicycle, such as
price and stock number. However, setting and getting prices for individual stock numbers is how a sales program
works. Therefore, you need an interface set up by the sales program, and the bicycle class must agree to implement
(use) all the constants and methods inside that interface. In that way, the bicycle class can then set a price and stock
number, and the sales program can get that price and stock number all by using the interface. Nothing has changed
inside the bicycle class or the sales program.
Encapsulation--Encapsulation means to encase, or contain, something within. In OO technology, this
means that the guts of how something is accomplished is encapsulated inside the object. For example, if you have a
Java application on your desktop that returns information about a jazz festival, you don't know (unless you wrote it)
or care that that application may be accessing multiple servers, interfacing with legacy systems, or performing a
local database lookup. To you, it looks as though that data is encapsulated within that object or set of objects.
Polymorphism--This is the coolest of the OO terms, and it's also a cool technology. It basically means
that if you make the same request to differing objects, those objects will return information appropriate to the
request. OS/400 does something like this, even though it is not an OO operating system in the same way that Java is
an OO language. When you use the Display Object Description (DSPOBJD) command, you will get different types
of information returned to you depending on the object type entered on the command.
Overloading--Overloading is the opposite of polymorphism. It states that an object will act differently
depending on the request sent to it. In OO technology, overloading is successful if the object understands the intent
of the request. The terms, or parameters, that accompany a request allow the object to detect the intent of the
request. For example, you might want to overload the add method in an object to behave differently depending on
the request sent to it. Let's say you call a Java method, and the first time you don't pass it any variables:
Mynumber = add2Numbers();
In this example, the method named add2Numbers will not receive any information from the caller. So you might
have some code within the add2Numbers method that sends some type of message if nothing is passed to it. Using
the concept of overloading, you can follow the above line of code with a line of code such as this:
Mynumber = add2Numbers(15, 30);
This time the add2Numbers method would probably add values 15 and 30 and return the result. As you can see, the
requests but do something a little different each time, depending upon how you call them.
I'll Have Some Java, Please.
OK, now that you're armed with terminology, you're ready to code. First, however, you must install Java on your
Windows, Unix, or Linux platform. To do this, check out Sun's download site to select the version you want to install and the
platform you want to install to. For detailed installation instructions, visit the installation site for your particular
platform. For example, the windows install instructions can be found at http://java.sun.com/j2se/1.3/install-windows.html.
Chances are that Java is already installed on the iSeries, but if it's not, you can install it yourself by copying the JAR
files to the Integrated File System on your iSeries, or by loading the appropriate IBM CD and installing from there.
Let's Code a Java App
Now, I know many of you have seen the "Hello World" application, used to demonstrate how Java works. That
particular Java application simply prints out "Hello World" to the screen. Well, I'm not going to do that. I like to
be different, daring. Instead, I am going to show you how to code a more appropriate Java application, one that
prints "Happy Valentine's Day" instead. Actually, I am going to show you that Java code, but I will also include a
Java program, available for download,
that was published in our first issue and used to call an RPG program. The following is the Java code necessary to
print "Happy Valentine's Day":
/**
* The ValentineApp class displays
* "Happy Valentine's Day!" to the standard output.
*/
class ValentineApp {
public static void main(String[] args) {
System.out.println("Happy Valentine's Day!"); //Display the string.
}
}
If you look closely at the comments, you'll see they are shown two ways. There are actually three ways to show
comments in Java. The first is /** text */. This way allows you to block a bunch of text together and show it as
comments. However, this method carries another advantage. When the javadoc program--which is a Java application
included with every Java Development Kit, and which can be run from any DOS command line to create instant
documentation of your Java code--is run against this class, it will take any comments signified by /** text */ and
turn them into HTML java documentation. If you simply want to comment a block of text without creating HTML
documentation from it, you can use the second comment type, /* text */. The third way to comment text is used for
short or single line comments. In that case, you just use the double slash (//) followed by the text.
The first line of real Java code is the class definition. A class is always defined by the class name and then variables
and methods enclosed within curly braces:
class name {
variables and methods
}
The next line is the main method of the class. Every class must contain the main method, which is used to decide
what the Java application will do. The main method in Java is much like your mainline code in an RPG program.
You can have logic in your main method, as I do in mine, or simply call other methods from the main method. There
is stuff to the left and to the right of main method identifier, main. To the left, you will see public static void.
Public means that this class can be called from any object. You can also use private, which means only this
class can call the method; protected, which means the class and subclass, as well as any class within the
package, can call the method; and package, which says that the method can be called from the class and from
within the package, but not from the subclass. There's a little more to it than that, but for now understand that there
are four different entries that can control method access.
Static defines the method as a class method. Class methods and variables have memory allocated for them
once, when the class is first called, no matter how many instances of that class have been created. This is much like
ILE service programs. The memory is allocated once, and the service program is bound by reference to those
programs that need it. If you don't use static, the method would be an instance method. Memory for instance
methods and variables is allocated for every instance of that class. This is how modules are sometimes used in ILE.
They are bound by copy, meaning that every program that binds-by-copy to a module will make a copy of that
module at compile time.
Void is the last entry to the left of main. Void means that nothing is going to be returned by this method call.
If you wanted to return a value from the method, you would define the return variable instead of using void.
The stuff within the parentheses, to the right of main, is the parameters that can be passed into the main method. In
this case, the main method accepts a string array (as signified by the brackets) called args. This means that you can
pass multiple command line arguments, separated by a space, and they will get stored in the string array, args. In this
case, we do nothing with command line argument, so they are ignored if passed.
The last thing to discuss in my Java application is the actual print to standard output. The form of the statement is
class.variable.method (parameter to pass to method). So, we are calling the println method, passing the string we
want to print, using the out variable, which defaults to standard output, from the System class.
Compiling and Running Java
There are a couple of things to keep in mind before compiling and running your application. If you are planning to
run your app in a Windows environment, you must first tell the operating system, and possibly your development
environment, how to find your Java environment. I coded and compiled my Java application using CODE/400 on
Windows 2000. Before I could successfully do this, however, I had to set my PATH environment variable, to tell
CODE/400 where to find my Java environment. If you use WebSphere development tools, you can add the
following to your PATH environment variable:
SET PATH=%PATH%;C:/WDT400/Studion35/bin
Your setup might be different, or you might not use CODE/400 as your development environment, so make sure to
find where your javac command is stored before modifying your PATH environment variable. The next thing you'll
want to do is set your CLASSPATH environment variable, to tell it where your toolbox JAR files, or any other
classes that are not part of the JDK or JRE, reside. In the Valentine example, setting the PATH is enough, because it
only uses core Java classes. However, I've also included a Java application that is used to call an RPG program.
(You can find the details about that program in "Calling a Program Using the iSeries
Toolbox for Java".) To successfully compile and run that application, you will need JAR files from the Java
tookit. Again, if you use the WebSphere development tools, specifically CODE/400, those JAR files will reside in
C:\WDT400\java. So the entry in my CLASSPATH environment variable would be the following:
SET CLASSPATH=%CLASSPATH%;C:\WDT400\java\jt400.jar;C:\WDT400\java\data400.jar
On the iSeries, the CLASSPATH is configured automatically when you install the Java Development Kit and the
iSeries Toolbox licensed programs. If you have other Java classes that you want to access, or you've copied the
toolbox or JDK to your iSeries instead of installing from CD, you can modify the CLASSPATH either in the Qshell
environment (STRQSH) or from a command line using the Work with Environment Variables (WRKENVVAR)
command, or in a CL program using the ADDENVVAR command.
Compiling and Running the ValentineApp Class
To compile your Java application from a DOS command line, open a DOS window and type javac, followed
by the name of the Java source code, in this case ValentineApp.java:
javac ValentineApp.java
Case is important; you must type it exactly as it is named, followed by the .java extension.
If you use CODE/400 to compile this java app, click on the Code/400 ACTIONS menu item, then select Compile
Local. You can prompt it or not; it doesn't matter for this example. When the code has compiled cleanly, click on
ACTIONS, then select Run Local, and you'll see the output from this program.
You can also compile and run this program on the iSeries. To do so, you must be in the Qshell environment
(STRQSH). Once there, type javac from the Qshell command entry screen. To run your application, simply
type java ValentineApp (don't include the .java extension).
After you've successfully compiled your application, you're ready to go; just type java ValentineApp, and
you should receive your valentine greeting.
Tip of the Iceberg
I've given you a lot of information, but it's only the beginning. As you can see, it's pretty easy to get an application
up and running, but to take advantage of a full-fledged OO application, you must really understand OO concepts.
You must also learn about constructors and dig a little deeper into class variables and methods, versus instance
variables and methods. And when all that's done, you can concentrate on Java syntax, because that's the easy part.
Kevin Vandever is a lead IT engineer for Boise Cascade Office Products in Itasca, Illinois, and co-editor of
Midrange Programmer, OS/400 Edition. He can be reached at kvandever@itjungle.com.
|