25
Thu, Apr
1 New Articles

Take the Java Plunge

Java
Typography
  • Smaller Small Medium Big Bigger
  • Default Helvetica Segoe Georgia Times

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:jdklibclasses.zip;c:myclasses

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

ADDENVVAR ENVVAR(CLASSPATH) +

VALUE(‘/’)

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

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

CLASSPATH(‘/’)

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/.lib/.file/JavaApp.mbr

In this instance, and 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

BLOG COMMENTS POWERED BY DISQUS

LATEST COMMENTS

Support MC Press Online

$0.00 Raised:
$

Book Reviews

Resource Center

  • SB Profound WC 5536 Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application. You can find Part 1 here. In Part 2 of our free Node.js Webinar Series, Brian May teaches you the different tooling options available for writing code, debugging, and using Git for version control. Brian will briefly discuss the different tools available, and demonstrate his preferred setup for Node development on IBM i or any platform. Attend this webinar to learn:

  • SB Profound WP 5539More than ever, there is a demand for IT to deliver innovation. Your IBM i has been an essential part of your business operations for years. However, your organization may struggle to maintain the current system and implement new projects. The thousands of customers we've worked with and surveyed state that expectations regarding the digital footprint and vision of the company are not aligned with the current IT environment.

  • SB HelpSystems ROBOT Generic IBM announced the E1080 servers using the latest Power10 processor in September 2021. The most powerful processor from IBM to date, Power10 is designed to handle the demands of doing business in today’s high-tech atmosphere, including running cloud applications, supporting big data, and managing AI workloads. But what does Power10 mean for your data center? In this recorded webinar, IBMers Dan Sundt and Dylan Boday join IBM Power Champion Tom Huntington for a discussion on why Power10 technology is the right strategic investment if you run IBM i, AIX, or Linux. In this action-packed hour, Tom will share trends from the IBM i and AIX user communities while Dan and Dylan dive into the tech specs for key hardware, including:

  • Magic MarkTRY the one package that solves all your document design and printing challenges on all your platforms. Produce bar code labels, electronic forms, ad hoc reports, and RFID tags – without programming! MarkMagic is the only document design and print solution that combines report writing, WYSIWYG label and forms design, and conditional printing in one integrated product. Make sure your data survives when catastrophe hits. Request your trial now!  Request Now.

  • SB HelpSystems ROBOT GenericForms of ransomware has been around for over 30 years, and with more and more organizations suffering attacks each year, it continues to endure. What has made ransomware such a durable threat and what is the best way to combat it? In order to prevent ransomware, organizations must first understand how it works.

  • SB HelpSystems ROBOT GenericIT security is a top priority for businesses around the world, but most IBM i pros don’t know where to begin—and most cybersecurity experts don’t know IBM i. In this session, Robin Tatam explores the business impact of lax IBM i security, the top vulnerabilities putting IBM i at risk, and the steps you can take to protect your organization. If you’re looking to avoid unexpected downtime or corrupted data, you don’t want to miss this session.

  • SB HelpSystems ROBOT GenericCan you trust all of your users all of the time? A typical end user receives 16 malicious emails each month, but only 17 percent of these phishing campaigns are reported to IT. Once an attack is underway, most organizations won’t discover the breach until six months later. A staggering amount of damage can occur in that time. Despite these risks, 93 percent of organizations are leaving their IBM i systems vulnerable to cybercrime. In this on-demand webinar, IBM i security experts Robin Tatam and Sandi Moore will reveal:

  • FORTRA Disaster protection is vital to every business. Yet, it often consists of patched together procedures that are prone to error. From automatic backups to data encryption to media management, Robot automates the routine (yet often complex) tasks of iSeries backup and recovery, saving you time and money and making the process safer and more reliable. Automate your backups with the Robot Backup and Recovery Solution. Key features include:

  • FORTRAManaging messages on your IBM i can be more than a full-time job if you have to do it manually. Messages need a response and resources must be monitored—often over multiple systems and across platforms. How can you be sure you won’t miss important system events? Automate your message center with the Robot Message Management Solution. Key features include:

  • FORTRAThe thought of printing, distributing, and storing iSeries reports manually may reduce you to tears. Paper and labor costs associated with report generation can spiral out of control. Mountains of paper threaten to swamp your files. Robot automates report bursting, distribution, bundling, and archiving, and offers secure, selective online report viewing. Manage your reports with the Robot Report Management Solution. Key features include:

  • FORTRAFor over 30 years, Robot has been a leader in systems management for IBM i. With batch job creation and scheduling at its core, the Robot Job Scheduling Solution reduces the opportunity for human error and helps you maintain service levels, automating even the biggest, most complex runbooks. Manage your job schedule with the Robot Job Scheduling Solution. Key features include:

  • LANSA Business users want new applications now. Market and regulatory pressures require faster application updates and delivery into production. Your IBM i developers may be approaching retirement, and you see no sure way to fill their positions with experienced developers. In addition, you may be caught between maintaining your existing applications and the uncertainty of moving to something new.

  • LANSAWhen it comes to creating your business applications, there are hundreds of coding platforms and programming languages to choose from. These options range from very complex traditional programming languages to Low-Code platforms where sometimes no traditional coding experience is needed. Download our whitepaper, The Power of Writing Code in a Low-Code Solution, and:

  • LANSASupply Chain is becoming increasingly complex and unpredictable. From raw materials for manufacturing to food supply chains, the journey from source to production to delivery to consumers is marred with inefficiencies, manual processes, shortages, recalls, counterfeits, and scandals. In this webinar, we discuss how:

  • The MC Resource Centers bring you the widest selection of white papers, trial software, and on-demand webcasts for you to choose from. >> Review the list of White Papers, Trial Software or On-Demand Webcast at the MC Press Resource Center. >> Add the items to yru Cart and complet he checkout process and submit

  • Profound Logic Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application.

  • SB Profound WC 5536Join us for this hour-long webcast that will explore:

  • Fortra IT managers hoping to find new IBM i talent are discovering that the pool of experienced RPG programmers and operators or administrators with intimate knowledge of the operating system and the applications that run on it is small. This begs the question: How will you manage the platform that supports such a big part of your business? This guide offers strategies and software suggestions to help you plan IT staffing and resources and smooth the transition after your AS/400 talent retires. Read on to learn: