Introduction To Java Programming

Fall COMMON 1999

Session 21LF

Session 33LF

Session 41LF

Introduction To Java Programming

Introduction

4

Goal

4

Tools

4

Setup

4

Hints

4

Getting Started: Java Programming Tips

5

Anatomy of a Java Program

5

Some AS/400 Specifics

6

Exercise 1: Creating and Running the HelloWorld Application on Windows

8

What This Exercise Is About

8

What You Should Be Able To Do

8

Introduction

8

Exercise Instructions

8

Open An Editor

8

Enter And Compile The Source

8

Run The HelloWorld Application

9

Hints

9

Exercise 2: Running the Hello World Application On the AS/400

10

What This Exercise Is About

10

What You Should Be Able To Do

10

Introduction

10

Exercise Instructions

10

Use the emulator to sign-on to the AS/400

10

Go back to the MS-DOS prompt of exercise 1

10

Return to the emulator

11

Hints

11

Exercise 3: AS/400 Java Programs

12

What This Exercise Is About

12

What You Should Be Able To Do

12

Introduction

12

Exercise Instructions

12

CRTJVAPGM, DSPJVAPGM

12

DLTJVAPGM

13

Exercise 4: Create Your Own Home Page

14

What This Exercise Is About

14

What You Should Be Able To Do

14

Introduction

14

Exercise Instructions

14

Start Netscape and Composer

14

Adding Text To Your Web Page

14

Adding A Link In Your Web Page To Another Web Page

15

Saving Your Web Page

16

Exercise 5: Creating The Hello World Applet

17

What This Exercise Is About

17

What You Should Be Able To Do

17

Introduction

17

Create The HelloWorldApplet Source File

17

Enter And Compile The Source

17

Insert The HelloWorldApplet Into Your Home Page

18

Test The HelloWorldApplet

19

Exercise 6: Creating The Hello World Servlet

20

What This Exercise Is About

20

What You Should Be Able To Do

20

Introduction

20

Create The HelloWorldServlet Source File

20

Enter And Compile The Source

20

Place the HelloWorldServlet onto the IBM Web Sphere web server.

21

Run the HelloWorldServlet

22

Bonus Lab 1: QShell

23

What This Exercise Is About

23

What You Should Be Able To Do

23

Introduction

23

Using QShell

23

Introduction

Java is an exciting programming language that is quickly being accepted in the computing industry. Java offers a programmer the flexibility to create full fledged applications (both client and server), applets that can run inside of a World Wide Web browser and servlets that can run inside web server applications.

Goal

In this lab, you will work through a series of exercises that provide you with an opportunity to create Java applications that run on the AS/400 as well as Windows 95. You will also explore the differences between a Java applet, a Java application, and a Java servlet.

Tools

The tools that you will use for the exercises in this lab for the most part are quite primitive. The most basic editor and command line compiler are used to create Java source code and compile your Java programs. On Windows you will use Notepad or edit. To compile and run your Java code you will use the Java Development Kit.

Setup

During this lab you will need a userid and password for the AS/400 and you will need to know the name of the AS/400 system. The userid and password will be JAVAxx where xx is the number 01 to 99 (two digits number). This information will be given to you by your instructor. Please fill in this information below so that you have it for reference during the lab.

The name of my AS/400 is _________________________ (case sensitive)

My AS/400 userid is _______________________________

My AS/400 password is ____________________________

The AS/400 directory is /LABxxx/Javaxx (Javaxx is your ID)

The AS/400 library is LABxxx

The PC directory is C:\LABxxx

Hints

The most important tip that you should remember while performing the following exercises is that the Java language is HIGHLY case sensitive. Please be extra careful to double check the case of all code that you enter to ensure that you've input your code correctly.

At the end of the first two exercises is a list of additional hints. These hints apply to all exercises. Check them out if you have trouble with an exercise.

Getting Started: Java Programming Tips

Anatomy of a Java Program

A simple Java program is shown below.

// This is a simple Java program

// With this style of comments the double slash must be on every line

/*                               
   With this style of comments you just need the beginning slash-star
   and the ending star-slash
*/

// There are two ways to use this class.  Since it has a "main" method, 
// it can be run from the command line.  It also has public constructors
// so it can be used by other Java classes.  If used by other classes, 
// main will not be called.


// This class uses classes in the java.io package
import java.io.*;

public class SimpleProgram extends Object
{  
          // The number of times this class is constructed. Since this
          // variable is static, there is one value for all classes.
   private static int useCount = 0;

         // The value passed when another object constructs this class.  
         // Each class gets its own copy of this variable.
   private int valuePassedOnConstructor = 0;

        // A class constructor with one parameter.  Constructor names 
        // match the class name.
   public SimpleProgram(int value)
   {
      valuePassedOnConstructor = value;
      useCount++;
   }

        // Main is the method called if the program is started from the
        // command line ("java SimpleProgram").  Note it is static so it
        // cannot access non-static variables.  
   public static void main(String[] args)
   {
            // Create a SimpleProgram object
       SimpleProgram sp = new SimpleProgram(100);

            // Call routine Main in SimpleProgram
       sp.Main(args);

            // Create another object to increment the use count
       SimpleProgram sp2 = new SimpleProgram(200);
       sp2.Main(args);

            // Print the number of time this class was constructed.
       System.out.println("Count = " + useCount);
   }



       // This method is private so it can be called only by other 
       // methods in SimpleProgram. Java is case sensitive so methods
       // can have the same name differing only in case.  Except for  
       // main this is not recommended, however.

  private void Main(String[] args)
  {

          // Enclose the code in a try/catch block.  If anything goes 
          // wrong the catch block will print a stack trace which  
          // includes class/method names and line numbers.
     
     try 
     {
            // Print the number of times this class is created
       System.out.println("Parameter = " + ValuePassedOnConstructor);
     }
     catch (Exception e)
     {
        e.printStackTrace();
     }
}
    }
                                

Some AS/400 Specifics

The Integrated File System on the AS/400 To understand how Java programs are stored and run on the AS/400, a knowledge of the AS/400's integrated file system is helpful. The integrated file system, or IFS, is a part of the OS/400 operating system that supports stream I/O and storage management similar to Unix and PC operating systems. Key features of the IFS include a hierarchical directory structure and support for storing information in stream files. Benefits of the IFS include fast access to OS/400 data and efficient handling of stream data including images and audio. All Java programs and libraries that reside on the AS/400 are stored in the Integrated File System. Libraries and files are accessible via the QSYS.LIB directory. An example of a directory tree is shown below: AS/400 Directory Tree Structure - QSYS.LIB - QOpenSys - UserDir1 - file1 - file2 - userDir2 - file2

Java Programs on the AS/400 All Java Virtual Machines (JVM) convert from Java bytecodes to machine instructions. Most JVMs convert at run time. The bytecodes are converted to machine instructions but the machine instructions are not saved. The bytecodes must be converted every time the class is run. In order to make Java run faster on the AS/400, the class file can be converted once and the machine instructions saved for future use. The converted version is called an AS/400 Java Program. An AS/400 Java Program is a preverified, compiled version of a class file. The AS/400 command CRTJVAPGM creates the Java Program. The original bytecodes still exist in the IFS. In fact, the AS/400 Java Program isn't visible in IFS. Only commands like CRTJVAPGM, DSPJVAPGM and DLTJVAPGM allow you to interact with the Java Program. The AS/400 keeps track of the two versions (bytecodes and AS/400 machine instructions) and discards the AS/400 Java Program if the bytecodes are changed.

Exercise 1: Creating and Running the HelloWorld Application on Windows

What This Exercise Is About

In this exercise you will create a simple "Hello World" Java application on Windows 95.

What You Should Be Able To Do

At the end of this exercise, you should be able to:

Introduction

Let's create your very first Java application. It's just a simple program that will print out the words "Exercise 1 -- Hello World".

Exercise Instructions

Open An Editor

  1. Open an MS-DOS prompt. (Press the Start button, follow the menu to Programs and choose MS-DOS prompt)
  2. Create a directory in which to place your work. Run the following command to create the directory.
    mkdir c:\LABxxx
  3. Change to the directory you created in the previous step.
    cd \LABxxx
  4. Open an editor to enter your Java source. The source file is named HelloWorld.java. This is done by using the following command. Alternatively, you can use Notepad instead of Edit.
    edit HelloWorld.java

Enter And Compile The Source

Type in the follow following source code. Please note the punctuation as well as the capitalization. Both are very important elements of Java's syntax.
public class HelloWorld 
{
	public static void main(String[] args)
	{
	System.out.println("Exercise 1 -- Hello World!");
	}
}

                                        
  1. Under the File pulldown menu, choose Save. Then under the File pulldown menu, choose Exit. You should be back at the MS-DOS command line.
  2. It's now time to compile your source code into Java bytecodes using the javac compiler. Issue the follow command at the MS-DOS prompt.
    javac HelloWorld.java
  3. If the source code compiles successfully, you will be presented again with another MS-DOS prompt. If there are errors in your source code, they will be displayed.
  4. If you have any errors in your source code, re-edit the HelloWorld.java file to fix the errors. If you have an error, something was probably spelled incorrectly, capitalized incorrectly or punctuation was not quite right.
  5. After you compile with no errors, you will notice a new file exists in your directory called HelloWorld.class. Issue the 'dir' command to see it.

Run The HelloWorld Application

Once you are able to successfully compile HelloWorld.java, it is time to run the Application.
  1. At the MS-DOS prompt issue.
    java HelloWorld
  2. You should now see the message
    Exercise 1 -- Hello World!
(Leave the MS-DOS box open. You will use it in other exercises.)

Hints

  1. PCs use the backslash as the separator character.
  2. File name case is important. The case must match the class name (the "public class HelloWorld" statement in the file ) and must match when running the java and javac commands.
  3. The Java language is case sensitive so the program must be typed correctly.
  4. No file extension is entered when running the java command.

Exercise 2: Running the Hello World Application On the AS/400

What This Exercise Is About

This exercise will demonstrate the 'write once, run anywhere' ability of Java. In this exercise you will run the "Hello World" Java application on the AS/400.

What You Should Be Able To Do

At the end of this exercise, you should be able to:

Introduction

In the previous exercise you created a Java program and ran it on the PC. Now you will run the same program on the AS/400.

Exercise Instructions

Use the emulator to sign-on to the AS/400

To run a Java program on the AS/400 the program must be in the integrated file system (IFS) of the AS/400. Sign-on to the AS/400, then create a directory to store the Java program. Run the following two commands where Javaxx is your id: MKDIR DIR('/LABxxxx') MKDIR DIR('/LABxxx/Javaxx')

Go back to the MS-DOS prompt of exercise 1

You will now copy the Java program from the PC to the AS/400. Java programs are stored in .class files. You will FTP the file from the PC to the AS/400. (Another/better option would be to use a Client Access/400 network drive to copy the file. To keep it simple you will use FTP in this lab). From the DOS prompt, enter the following commands where system is the name of your AS/400 and Javaxx is your userid.
  1. ... Make sure you are in the directory containing the exercises (C:\LABxxx)
  2. ftp system
  3. ... Enter your userid and password when prompted
  4. cd /. You must cd to "/" to go to the root directory of IFS.
  5. cd /LABxxx/Javaxx
  6. put HelloWorld.java
  7. bin
  8. put HelloWorld.class
  9. quit

Return to the emulator

The Java program is now on the AS/400. Now use Java on the AS/400 to run the program. The first step is to tell Java the location of your program. Java uses the CLASSPATH environment variable to find Java programs. You will tell it to look in /LABxxx/Javaxx. CLASSPATH is case sensitive so make sure you type it in uppercase. As on the PC, you run a Java program with the java command. From the AS/400 command prompt enter the following commands.
  1. ADDENVVAR CLASSPATH '/LABxxx/Javaxx'
  2. java HelloWorld
As in the last exercise, Exercise 1 -- Hello World should be displayed. Press F3 to exit the Java shell and return to an AS/400 command prompt. (Leave the emulator open. You will use it in other exercises.)

Hints

  1. The AS/400 uses the forward slash as the separator character.
  2. When running ftp, you must do the "cd /". By default, the target is an AS/400 library. The "cd /" switches the target so it is now the IFS.
  3. When running ftp, you must do the "bin" to transfer in binary mode.
  4. CLASSPATH must be upper case.
  5. Java programs on the AS/400 run in a "Java Shell". You must exit the shell before running AS/400 commands.

Exercise 3: AS/400 Java Programs

What This Exercise Is About

Java class files contain bytecodes. All platforms that support Java must convert these bytecodes into machine instructions. The AS/400 has two ways of doing this -- interpreted (convert when the class file is run) and static (convert once and save the converted version). In this exercise you will examine the CRTJVAPGM, DSPJVAPGM and DLTJVAPGM commands on the AS/400. These commands deal with static conversion.

What You Should Be Able To Do

At the end of this exercise, you should be able to:

Introduction

All Java Virtual Machines (JVM) convert from Java bytecodes to machine instructions. Most JVMs convert at run time. The bytecodes are converted to machine instructions but the machine instructions are not saved. The bytecodes must be converted every time the class is run. In order to make Java run faster on the AS/400, the class file can be converted once and the machine instructions saved for future use. The converted version is called an AS/400 Java Program. An AS/400 Java Program is a preverified, compiled version of a class file. The AS/400 command CRTJVAPGM creates the Java Program. The original bytecodes still exist in the IFS. In fact, the AS/400 Java Program isn't visible in IFS. Only commands like CRTJVAPGM, DSPJVAPGM and DLTJVAPGM allow you to interact with the Java Program. The AS/400 keeps track of the two versions (bytecodes and AS/400 machine instructions) and discards the AS/400 Java Program if the bytecodes are changed. The AS/400 Java Program is created in one of two ways: by explicitly using the CRTJVAPGM command on a class/jar file, or when the file is first used. In this exercise we will look at some of the options of how AS/400 Java Programs are created. DSPJVAPGM will display the attributes of a named class/jar file in IFS. DLTJVAPGM will destroy the Java program that is associated with a class/jar file in IFS.

Exercise Instructions

CRTJVAPGM, DSPJVAPGM

We will work with the class files we created in the previous exercises.
  1. If not already signed on, sign on to the AS/400 and set up the CLASSPATH environment variable. Enter
ADDENVVAR ENVVAR(CLASSPATH) VALUE('/LABxxx/Javaxx')
  1. Change directory to the location of the class files. Enter
    cd '/LABxxx/Javaxx'
  2. Now you will use the Java application HelloWorld you created in an earlier exercise. As was mentioned in the introduction, Java programs are created when a class file is used for the first time. Since you have already executed this Java application, there is already a Java program for the class file HelloWorld.class. Let's take a look at it. Enter
    DSPJVAPGM
    CLSF(HelloWorld.class)
  3. Notice the various fields, most importantly the optimization field. Optimization level 10 is the default when a Java program is created. Let's make our Java program faster.
  4. Type CRTJVAPGM CLSF(HelloWorld.class) and hit F4 to prompt on it.
    1. Notice that optimization level 40 is the highest level available. Also note optimization level 10 or *INTERPRET is best when you are debugging your application. Optimization level 40 is best for speed.
    2. Notice that you may enable performance collection hooks. When you place these hooks into your Java programs, you can use the Performance Explorer to collect performance information on your Java applications.
    3. Enter 40 for the optimization level and press ENTER.
  5. Now rerun your HelloWorld application by entering
    java HelloWorld
    Press F3 to exit the Java shell and return to an AS/400 command prompt.
  6. The first thing you probably will notice is that your Java application isn't running noticeably faster. Here's why. The class file HelloWorld.class is only one of many class files that are used when your Java application is run. The other class files are located in the jar file named classes.zip in the IFS directory /QIBM/ProdData/Java400/jdk11x/lib. Let's see what optimization level they are at. Run
DSPJVAPGM CLSF('/QIBM/ProdData/Java400/jdk11x/lib/classes.zip')

DLTJVAPGM

When you delete a class file from IFS, if there was a Java program associated with the class file, it will be deleted as well. But what if you want to save the class file and only get rid of the Java program associated with it? To do this, there is the DLTJVAPGM command. Let's try it out now on your HelloWorld application. Run DLTJVAPGM CLSF('HelloWorld.class') Your Java program should now be deleted. Let's use DPSJVAPGM to double check. Issue DSPJVAPGM CLSF('HelloWorld.class') The next time HelloWorld is run, a new Java Program will be created and saved at optimization 10.

Exercise 4: Create Your Own Home Page

What This Exercise Is About

Before we are able to create any Java Applets, you will need a home page on which to place them. In this lab you will create an HTML document for the Java Applets that you will create and reuse in the next exercises.

What You Should Be Able To Do

At the end of this exercise, you should be able to create a Web page using Netscape.

Introduction

World Wide Web pages are written using a language called HyperText Markup Language, better know as HTML. Learning HTML is comparable to learning another programming language. Thankfully HTML editors have entered in the market in recent years. These editors allow you to create your very own home page without having to learn any HTML. Composing a home page becomes as easy as writing a letter in your favorite word processor.

Exercise Instructions

Start Netscape and Composer

  1. Double Click on the Netscape Communicator icon that is located on your desktop.
  2. From the Communicator pulldown menu in Netscape choose the Composer menu item.
  3. You are now ready to start creating your own home page. Pictured is the Netscape Composer.

Adding Text To Your Web Page

  1. Let's put a title on this web page. We'll want to put this in a bigger headline font. Select Heading 1 in the leftmost drop down list box (Hint: It's located right under the icon labeled "New").
  2. Type "My Home Page" in the editor and hit ENTER.

Adding A Link In Your Web Page To Another Web Page

  1. Now we will make a link to the AS/400 Home Page. Press the Link button from the toolbar.
  2. Fill in the dialog as pictured and click on "OK"

Now your web page should look something like this:

Saving Your Web Page

Last you need to save your home page since it will be used in the next exercise.

  1. Click on Save from the Toolbar. Save your page to directory C:\LABxxx (the same directory where you placed your HelloWorld Java code from Exercise 1).
  2. Name your HTML file HomePage and click on the Save button.
  3. Next you will be presented with a dialog to give your page a title. Click on OK again.

Exercise 5: Creating The Hello World Applet

What This Exercise Is About

Applets are Java programs that are dynamically downloaded over the World Wide Web and run in your browser. In this exercise you will create a very simple Hello World Applet and place it on the home page you created in the previous exercise.

What You Should Be Able To Do

At the end of this exercise, you should be able to:

Introduction

The source code to create an Applet is very similar to the HelloWorld application that you created earlier. When you create a Java Applet, you must extend the java.applet.Applet class. This means you are going to use the java.applet.Applet class as a reusable part and extend it to do what you want. In this case you will make it print out the word "Hello World" in the browser.

Create The HelloWorldApplet Source File

  1. Open an MS-DOS prompt. (Press the Start button, follow the menu to Programs and choose MS-DOS prompt) or use the MS-DOS prompt from the previous exercise.
  2. If you opened a new prompt, change directory to the directory that holds the exercises. Enter
    cd \LABxxx
  3. Open the MS-DOS editor to create a new piece of Java source named HelloWorldApplet.java. This is done by using the following command.
    edit HelloWorldApplet.java

Enter And Compile The Source

Type the following source code. Please note the punctuation as well as the capitalization.


import java.awt.Graphics;

public class HelloWorldApplet extends java.applet.Applet

public void paint(Graphics g)
{
g.drawString("Applet -- Hello World!",5 ,25);
}
}
                                        
  1. Under the File pulldown menu, choose Save. Then under the File pulldown menu, choose Exit. You should be back at the MS-DOS command line.
  2. It's now time to compile your source code into Java bytecodes using the javac compiler. Issue the following command at the MS-DOS prompt:
    javac HelloWorldApplet.java
  3. If the source code compiles successfully, you will be presented again with the MS-DOS prompt. If there are errors in your source code, they will be displayed.
  4. If you have any errors in your source code, re-edit the HelloWorldApplet.java file to fix the errors. If you have an error, something was probably spelled incorrectly, capitalized incorrectly or punctuation was not quite right.
  5. After you successfully compile the source file, you will notice a new file your directory called HelloWorldApplet.class. Issue the 'dir' command to see it.

Insert The HelloWorldApplet Into Your Home Page

  1. Start Netscape.
  2. From the Communicator pulldown menu click on the Composer menu item.
  3. In the Netscape Composer window, from the File pulldown menu click on the Open Page... menu item. Click on the Choose File... button. Select the home page you created in the previous exercise. It should be C:\LABxxx\HomePage. Click on the Open button once you have selected the file. Lastly click on the Open button in the Open Page dialog. Your home page should appear in the editor.
  4. Pick a location where you would like the Applet to appear on your home page. (Below the link to the AS/400 home page would be a good spot). Click once to place the cursor in the location you have chosen.
  5. From the Insert pulldown menu click on the HTML Tag... menu item. For the tag enter
    <applet code="HelloWorldApplet.class" width=300 height=300>
  6. Click OK.
  7. You just entered the applet tag, now you must end the tag. The cursor should be at the end of the box. From the Insert pulldown menu click on the HTML Tag... menu item. For the tag enter
    </applet>
  8. Click OK.
  9. Your HTML document should now look like this picture.

Test The HelloWorldApplet

Exercise 6: Creating The Hello World Servlet

What This Exercise Is About

Servlets are Java programs that run inside Java-enabled web servers. Servlets provide a middle-tier of communication between a browser application and a host's protected resources. The servlet accepts requests from the browser client, processes the requests (which may involve host database updates) and then sends HTML responses back to the client. In this exercise you will create a very simple Hello World servlet and run it from an AS/400 web server.

What You Should Be Able To Do

At the end of this exercise, you should be able to:

Introduction

The source code to create a servlet uses the Java Servlet API. The Servlet API is a Standard Extension to Java that provides the classes used in servlet development. All servlets implement the Servlet interface within the Java Servlet API. You will use the javax.servlet.http.HttpServlet class as a reusable part and extend it to implement your servlet. The HttpServlet class is a common protocol used between browsers and servlets. In this case you will make the HttpServlet class print out "Hello World" in the browser.

Create The HelloWorldServlet Source File

  1. Open an MS-DOS prompt. (Press the Start button, follow the menu to Programs and choose MS-DOS prompt) or use the MS-DOS prompt from the previous exercise.
  2. If you opened a new prompt, change directory to the directory that holds the exercises. Enter
    cd \LABxxx
  3. Open the MS-DOS editor to create a new piece of Java source named HelloWorldServlet.java. This is done by using the following command.
    edit HelloWorldServlet.java

Enter And Compile The Source

Type the following source code. Please note the punctuation as well as the capitalization.

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class HelloWorldServlet extends HttpServlet
  	{
         public void doGet (HttpServletRequest req, HttpServletResponse res)
                  throws ServletException, IOException

         {
             PrintWriter out;

             res.setContentType("text/html");
             out=res.getWriter();
             out.println("<html>");
             out.println("<head><title>Hello World Servlet</title></head>");
             out.println("<body>");
             out.println("<h1>Hello World</h1>");
             out.println("</body></html>");
             
}
}
					
    1. Under the File pulldown menu, choose Save. Then under the File pulldown menu, choose exit. You should be back at the MS-DOS command line.
    2. It's now time to compile your source code into Java bytecodes using the javac compiler. Issue the following command at the MS-DOS prompt:
      javac HelloWorldServlet.java
    3. If the source code compiles successfully, you will be presented again with the MS-DOS prompt. If there are errors in your source code, they will be displayed.
    4. If you have any errors in your source code, re-edit the HelloWorldServlet.java file to fix the errors. If you have an error, something was probably spelled incorrectly, capitalized incorrectly or punctuation was not quite right.
    5. After you successfully compile the source file, you will notice a new file your directory called HelloWorldServlet.class. Issue the 'dir' command to see it .

Place the HelloWorldServlet onto the IBM Web Sphere web server.

Only one version of the HelloWorldServlet.class file is needed on the web server. A copy of the servlet you just created has already been put on the web server and is ready for your use. The servlet has been stored on the AS/400 in the following IFS directory: /QIBM/ProdData/IBMWebAS/Servlets/HelloWorldServlet.class This directory stores servlets in use on the AS/400 system by the IBM Web Sphere web server. Please do not copy your version of the HelloWorldServlet to the AS/400 web server.

Run the HelloWorldServlet

To run the HelloWorldServlet you need to call it from your web browser.
  1. Double click on the Netscape Communicator icon that is located on your desktop.
  2. Enter the URL location of your servlet. Into the Location: box enter:
    http://system/servlet/HelloWorldServlet
  3. This causes the servlet to run. The Hello World message should be displayed in your browser.

Bonus Lab 1: QShell

What This Exercise Is About

Introduce you to QShell on the AS/400.

What You Should Be Able To Do

At the end of this exercise, you should be able to use QShell to compile and run Java programs.

Introduction

The 'command line' way to run Java programs in Windows is via the MS-DOS prompt. In Unix, the typescript window is used to run command line programs. The home for Java programs on the AS/400 is the Java shell. In previous labs you ran Java programs on the AS/400 via the AS/400 Java command. You may have noticed that while running the program you are switched to the Java shell. You pressed F3 to exit the shell and return to the AS/400 command prompt. Another way to get into this Java shell is by running QShell. QShell is a full function shell environment, but for the purpose of this lab it is simply a convenient place to run Java programs.

Using QShell

From the AS/400 command prompt, enter QSH. This starts the shell interpreter. If you are used to Windows or Unix, the shell has a more familiar look and feel. Do the following from the shell:

  1. Set up the CLASSPATH environment variable. Enter
    export CLASSPATH=/LABxxx/Javaxx
  2. Change directory to the directory containing your lab exercises. Enter
    cd /LABxxx/Javaxx
  3. Display the contents of the directory. Notice by default the shell uses Unix commands. Enter
    ls
  4. Run one of your Java programs. Enter
    java HelloWorld
  5. You can also compile inside the shell Enter
    javac HelloWorld.java
  6. Press F3 when you are ready to exit the shell.