MCPressonline.com  
Home Logout Help  / How do I...? Shop MC Press Shop IBM Press Site Map
  My Preferences  Check Messages  My Email  My Msg Center  My Subscriptions
Top Spots
Article Library
Code Library
Forums
Vendor Directory
Publications
Shop MC Store
Shop IBM Press
Polls
Feedback
Topics
Application Software
Database
eBusiness
Internet
Management
Networking
News & Analysis
Operating Systems
Printing
Products
Programming
Security
System Admin
Web Serving
WebSphere
Windows
About Us
Company Info
Contact Us
Media Kit - Advertise
Publications
An Open Letter to Midrange Computing Customers
MCPressOnline Privacy Statement and Usage Agreement

Send a live message

Who's here?
bbb

 [F] MCPressOnline  / Article Library  / Articles by Publication  / Midrange Computing Magazine  / February 1999  /

Take the Java Plunge

There's nothing to be afraid of. Dive right in!
by RICHARD SINN
Published 2/99
Book Blowout!

Power CL

Power CL

Regular: $99.00

Special: $24.95

Giving you proven CL programming techniques from experienced pros that take you beyond the fundamentals to a more advanced level of CL programming, this book offers advice about such topics as softcoding techniques, properly using overrides, debugging CL, and error handling. It will teach you to write better CL code.

This article introduces you to the basics of Java and then jumps right in with a tutorial on how to develop your first AS/400 Java application. First, it covers how to develop Java programs on the PC and deploy them on the AS/400; then, it covers how to develop and deploy Java programs from the AS/400 itself.

Since 1994, the World Wide Web has become the fastest growing information source in the world. However, most Web pages are still static with little or no interaction between users and the Web pages. There is a high demand for a way to program Web pages to make them interactive and secure, and Sun Microsystems’ Java application development language and architecture seem to be a perfect fit for Internet programming. After three alphas and two beta releases of Java Development Kit (JDK) in 1995, many major technology companies signed up for Java licenses. Some of these companies include such big names as IBM, Microsoft, Apple, Netscape, Inprise, and Symantec.

Version 1 of the JDK came out in 1996, and additional functions, such as the Just- In-Time (JIT) compiler, Java Database Connectivity (JDBC), and Object Serialization were added in later releases. OS/400 Version 4 Release 2 currently supports JDK Version 1.1.4.

All the hype that has swirled around Java can be captured in one sentence: Java is a simple, object-oriented, distributed, interpreted, robust, secure, architecture-neutral, portable, high-performance, multithreaded, and dynamic Internet programming language. In fact, for novice users, the most important feature of Java is its simplicity. I regularly have students in my college classes who, even though they are without previous programming experience, start to believe in themselves as programmers by the end of the course because of their newfound Java abilities. This article overviews Java and then provides a Java tutorial on how to write Java applications.

Java Features

A few years back, a programming language called C++ came into vogue within the computer industry. The main reason for its popularity was that it was object-oriented programming (OOP). The OOP capabilities of C++ enhanced code reuse to the point that this language saved companies money in development and maintenance costs. Although

C++ is still popular, its popularity has died down—mostly because C++ is difficult to learn and even more difficult to master. Java, on the other hand, is a full-fledged, object-oriented programming language that features the best of C++ without its pitfalls. Java has none of the complex mechanisms of C++ such as pointers, multiple inheritance, and templates. Even memory allocation, a standard part of a C++ programmer’s job, is automatically handled by Java’s runtime system.

Another important feature of Java is its ability to support distributed applications. In a client/server environment, Java provides the ability to store its executable files on the server. When a special program (called an applet in Java) is called, it will be distributed to the client machine and is then executed by the CPU of the client machine. Java applets deliver the promise of client/server because they enable applications to store critical data on the server while making full use of the network resource as client.

Basic Java Architecture

In traditional programming languages such as C, C++, FORTRAN, and COBOL, the development of a program can be viewed as a three-stage process. As illustrated in Figure 1, programmers will code their programs using a programming language in Stage 1 with their favorite editors. In Stage 2, a compiler will be used to convert the text of the programming language to a binary file (Stage 3). Compilers, such as ILE/C on the AS/400, Micro-soft Visual C++ on the PC, or GCC in UNIX, can be viewed as special programs that will understand the programmers’ text file in Stage 1 and convert the text files into binary files that will be understood by the platform CPU. In other words, even though the text file entered by the programmers in Stage 1 looks the same in different platforms, when the binary files are generated by the compilers, the programs (binary files) become platform-dependent. For example, you cannot run a PC-compiled program on the AS/400—that is, unless the programming language you use is Java.

In Java, the general programming model can be divided into four stages as illustrated in Figure 2. In Stage 1, you will use an editor to code up your Java program with .java as the extension for the file name. And, in Stage 2, we use the javac compiler from JDK to “compile” the Java program. The javac compiler is supported in whatever platform JDK is available (e.g., AIX, SPARC, AS/400, Windows NT, etc). The javac compiler will generate Java byte code (with .class as the extension), which is a low-level language understood by the Java Virtual Machine (JVM). Then, in Stage 4, the byte code will be interpreted to run the Java program. As a matter of fact, byte code can be viewed as the machine language that will be interpreted by the JVM. The only difference is that this platform (i.e., the JVM) is supported on multiple machine types (Windows, UNIX, AS/400, etc.). Thus, Java is supported as a multiplatform programming language: Write it once, and run it everywhere.

In fact, when a Web browser is advertised as a Java-enabled browser, it implies that there is a JVM built in with the browser. Thus, Java programs can be downloaded from the Internet and run on top of the JVM provided by a browser.

In the Java world, programs are either called Java applications or Java applets. In a nutshell, applets are programs run within the browsers, and applications are programs run on top of any platform. The table in Figure 3 summarizes the core difference between Java applications and applets.

Java’s CLASSPATH Environment Variable

Because both Java applications and applets are loaded dynamically as needed, you need to have a way to tell the Java runtime where the programs are located. The environment variable CLASSPATH serves as a perfect location indicator. The CLASSPATH environment variable contains a list of directories, .zip files, and .jar files where Java classes are to be found.

To display the CLASSPATH in Windows 95, under the MS-DOS prompt, type the following:

echo %CLASSPATH%
To change the CLASSPATH in Windows 95, put the following under autoexec.bat:
set CLASSPATH = c:\jdk\lib\classes.zip;c:\myclasses

On the AS/400, the CLASSPATH environment variable can be set using the following command:

ADDENVVAR ENVVAR(CLASSPATH) +

VALUE(‘/<directory>’)

Or it can be set when using the run Java CL command:

RUNJVA CLASS(‘/JavaApps/MyJavaApp.class’) +

CLASSPATH(‘/<directory>’)

Other operating systems and their Java environments will have different methods of setting the CLASSPATH, but all will honor the notion of the CLASSPATH.

A Windows Java Application

If you are going to program with Java on Windows 95 or NT, you can download the current release of JDK from the Sun Microsystems Web site at java.sun.c om/products/jdk. You can then follow the detailed instructions on how to install JDK under java.sun.com/products/ jdk/1.1/ README. Once JDK is installed correctly, you can start building your first Windows Java application.

You begin Java development by starting a DOS or Command window. To make your application more organized, you can create your own directory with the DOS Make Directory command:

mkdir C:\JavaApp
Then, you should change the current directory:
cd C:\JavaApp

Your next step is to create and edit a new file called JavaApp.java that is to contain the Java source code. You can use any editor of your choice; if you use Notepad, for instance, you simply specify the name of the Java source file:

notepad JavaApp.java

Then, from within your editor, type the following Java source code:

public class JavaApp
{

public static void main (String[] args)

{

System.out.println (“Hello World”);

}

}

Save those changes, which, from Notepad would be the File menu followed by the Save option.

Because Java is object-oriented, everything created in Java is a class and there is no difference in our three-line application. Line 1 is used to declare a new class:

public class JavaApp

Java’s reserved word public indicates this class gives public access authority, so users can actually run the application in this case. Java’s reserved word class indicates that we are declaring a class and the name will follow the word. JavaApp is the name of our class. The name of the class has to match the name of the file that contains the source code for the class, minus the .java extension. (In this example, the source code file name is JavaApp.java and the class name is JavaApp.) The curly brace that follows line 1 defines the scope of the class. There is always a closing curly brace at the end of the scope that matches the opening brace.

While line 1 is used to declare a class, line 2 is used to declare a method (or function) within the class. The keyword public indicates that the method has public access authority, so any user or program can call it. The keyword static indicates that this is a class method and main is the function name for your Java application. The main function is considered special with Java (and C) and is used only when writing an application; it is not used when writing an applet. This special function will be the entry point of the application. In other words, when the application is run, it will start executing from main. Again, the pair of opening and closing curly braces is used to define the scope of the function.

The single statement within the body of the main function prints the string that says “hello” to the world:

System.out.println (“Hello World”);

System.out.println is actually a function call with “Hello World” as the parameter. System is the name of a standard Java class, and the dot operator indicates one level of indirection that qualifies the out variable. That out variable is a special variable for program output, and println is the name of the function that handles printing.

Now, you are ready to compile your applications. Back in the DOS prompt, compile JavaApp.java:

javac JavaApp.java

If you see any error messages, double-check the Java source code and correct and fix any typos. As discussed above, the javac compiler will generate byte code that will be understood by the JVM. The file containing the byte code has the same name as the .java source code; thus, JavaApp.class will be generated. You can verify this with a qualified DOS directory command:

dir JavaApp.class

Now, you are ready to run your first application with the Java execution command:

java JavaApp

Notice that, although you had to specify the .java extension when you compiled your Java application, you do not specify .class extension when you execute it. You should see the text “Hello World” on your DOS window.

Java Applications on the AS/400

Now, create that same Java application on AS/400 and see how it works. On the AS/400, you’ll need at least OS/400 V4R2, and you’ll also have to install the AS/400 Developer Kit for Java (5769-JV1) and the QShell Interpreter (5769-SS1) program product (see “AS/400 Finally Gets a UNIX Face” elsewhere in this issue for an explanation of the QShell Interpreter). This AS/400 version of the Java Development Kit (JDK) comes free with V4R2. So, if you have OS/400 V4R2, you automatically have one of the strongest Java systems in the industry as well.

To begin the creation of your AS/400 Java application, you’ll need to create a directory in the AS/400 Integrated File System (AS/400 IFS) for your Java source code
(.java file) as well as for your compiled Java programs (.class file). You create an IFS directory the same way you would in DOS with the Make Directory command:

mkdir(‘JavaApp’)

At this point you could reuse the same JavaApp.java source file from your Windows example. From the PC, use FTP or Client Access/400 file transfer to transfer JavaApp.java to / JavaApp directory under IFS in the AS/400. Another option is to use good old SEU to enter your Java source and then use the OS/400 Copy to Stream File (CPYTOSTMF) command to copy the source member to an IFS stream file, which I will cover later in this article. At any rate, with the source file in the JavaApp directory, you are ready to compile your Java application.

First, you need to start the QShell environment with the Start QShell (STRQSH) command. Then, you can change the current directory to the path where you just stored the JavaApp.java file:

cd (‘/JavaApp)

To execute the Java compiler, use the same javac command as you did on Windows:

javac JavaApp.java.

If there are any errors in your source, they will be displayed on the QShell screen. Note that the QShell environment is a little bizarre in that when you execute the compile, it appears to run instantly. That is, the command line is input-enabled right after you execute the javac command. The thing is, that javac command runs in the background; it will not be complete until there is a dollar sign listed underneath the command execution:

> javac JavaApp.java

JavaApp.java:5: ‘;’ expected.

System.out.println (“Hello World”)

^

1 error

$

You’ll need to go back and reedit your source file to fix any errors. In my case, I forgot to insert a semicolon following the “Hello World” print line statement. After you have saved your changes, be sure to recopy the source file back into the IFS. QShell will put a dollar sign immediately under your javac statement if the compile was successful.

> javac JavaApp.java

$

Once you have a successful compilation, a file called JavaApp.class will be created inside of your IFS directory. (At this point, you should no longer be in the QShell environment and can exit with F3.) You can double-check the existence of the compiled Java application with the Work Link (WRKLNK) command. There should be two files in the directory. JavaApp.java is the source file that you compiled, and JavaApp.class is the generated byte code Java program.

Now, you are ready to run the JavaApp Java application on the AS/400. First, you need to create an environment variable called CLASSPATH. This environment variable is a list of IFS directories that Java searches to find class files to run. On the AS/400, the class

path variable can be created with the Add Environment Variable (ADDENVVAR) command:

ADDENVVAR ENVVAR(CLASSPATH) VALUE(‘/JavaApp’)

Be sure to use all capitals for the class path variable. If the class path environment variable already exists, you can edit it with the Work Environment Variable (WRKENVVAR) command.

Next, you can run the application with OS/400’s Run Java command:

RUNJVA JavaApp

The message “Hello World” should now be displayed. Optionally, from within the OS/400’s QShell environment, you could have used the same DOS commands we used earlier on the Windows system to set your class path and execute the Java application:

> set CLASSPATH=/JavaApp

$

> java JavaApp

Hello World

$

Editing Java Source on the AS/400

If a PC with FTP or Client Access/400 is not available so that you can use a Windows editor, you can still use your old and faithful AS/400 SEU to create and edit your Java source. Please note that SEU stores the Java source code inside of a source member instead of in IFS. When you use the Java compiler to compile your Java source, it needs to be in IFS. You will use the CPYTOSTMF command to copy the Java source from your source physical file into an IFS file. Using SEU, type in the same source as you did for the Windows Java application. With that completed, you then need to copy the source file from your library into IFS with the CPYTOSTMF command. To do that, enter the command and hit F4 to prompt. Then, enter the command’s From database file member prompt as follows:

/qsys.lib/<library>.lib/<source>.file/JavaApp.mbr

In this instance, <library> and <source> are the names of the library and source file that you previously placed, plus the Java source file member. Then, for the To stream file prompt, specify the IFS directory that the source member is to be copied to. Finally—and please don’t skip this important step—enter the Stream file code page prompt as 819:

CPYTOSTMF FROMMBR(‘/qsys.lib/sinnlib.lib/sinnsrc.file/

JavaApp.mbr’) +
TOSTMF(‘/donat/JavaApp.java’) +
STMFOPT(*REPLACE) +
STMFCODPAG(819)

This command will create a stream file in the IFS that contains the source statements from your QSYS library systems source member. You are now ready to compile your Java program on the AS/400.

Java: Pointing Due North

There is no question that Java is the technology that will lead the IT industry in a new direction. Java technology is changing the way people do business. Companies are securely opening the corporate network and empowering the supply channel, business

partners, and customers to interact. Your Java application solution is “anywhere, anytime, on anything.” Companies around the world use Java to create faster response time to market and to lower information technology costs, thereby increasing revenue while improving customer service. Java technology enables companies to create solutions that did not exist previously. Access to those solutions creates competitive advantages for business, and Java technology with AS/400 makes it happen.

You can gather more information on the current release of Java by visiting the Java Web site at java.sun.com. For more information on the AS/400 and Java, visit the AS/400 Web site at www.as400.ibm.com, and then click on products. For IBM Java enterprise solution, visit IBM Java Home at www. ibm.com/java/.

Stage 1 Stage 2 Stage 3

Compiler (Pentium) Binary File (Pentium) Your Code Compiler (OS/400) Binary File (OS/400) Void main()

{

}

...

Compiler (UNIX) Binary File (UNIX)

Figure 1: Traditional program development stages

Stage 1 Stage 2 Stage 3 Stage 4

Java Compiler / javac Java Interpreter on JVM (Pentium) (Pentium)

Java Code Java Compiler / javac Java Byte code Java Interpreter on JVM Class Hello (OS/400) (Platform Independent) (OS/400)

{

}

...

Java Compiler / javac Java Interpreter on JVM (UNIX) (UNIX)

Figure 2: Java program development stages

Applications Applets

Loaded from local or network storage Downloaded from server as needed Runs on platform directly Runs on client within browser Usually larger Usually smaller Unlimited file access Limited file access Unlimited network access Limited network access Entry point: main() method Entry point: extends java.applet.Applet class The program files are with .class extension and are called class files The program files are with .class extension and are called class files

Figure 3: Core differences between Java applications and applets

Rate it!
Rate it!
4-Excellent!
3-Good.
2-OK.
1-Not helpful.

Discuss this article

Save this article to your personal Message Center.

Email this page

Printer Friendly VersionPrinter-Friendly

Related Categories

  • Programming-Java-Fundamentals





  • Bookmark ]


    Previous story  |  Next


    © 2001 MC Press, LLC. All Rights Reserved.
    This article originally appeared in a Midrange Computing publication and is reproduced here for the exclusive use of registered users at www.MCPressOnline.com.


    My Links    

    Edit

    Previous story  |  Next

     Discuss
     Save
    Email this page

    Report a problem with an article

    Related Categories

  • Programming-Java-Fundamentals

  • Recently Viewed

    Take the Java Plunge