Creating the simple, Hello World Java™ program is a great place to start when
becoming familiar with the IBM® Developer Kit for Java.
To create, compile, and run your own Hello World Java program,
perform the following steps:
- Map a network drive to your iSeries™ server.
- Create a directory on your iSeries server for
your Java applications.
- Create the source file as an American Standard Code for Information
Interchange (ASCII) text file in the integrated file system. You can either
use an integrated development environment (IDE) product or a text-based editor
such as Windows® Notepad to code your Java application.
- Name your text file HelloWorld.java. For more
information about how you can create and edit your file, see Create
and edit Java source files.
- Make sure that your file contains this source code:
class HelloWorld {
public static void main (String args[]) {
System.out.println("Hello World");
}
}
- Compile the source file.
- Enter the Work with Environment Variable (WRKENVVAR) command
to check the CLASSPATH environment variable. If the CLASSPATH variable does
not exist, add it and set it to '.' (the current directory). If the CLASSPATH
variable does exist, make sure that the '.' is at the beginning of the path
name list. For details about the CLASSPATH environment variable, see Java classpath.
- Enter the Start Qshell (STRQSH) command to start the Qshell
Interpreter.
- Use the change directory (cd) command to change the current
directory to the integrated file system directory that contains the HelloWorld.java file.
- Enter javac followed by the name of the file
as you have it saved on your disk. For example, enter javac HelloWorld.java.
- Set the file authorities on the class
file in the integrated file system.
- Optimize the Java application.
- On the QSH Command Entry line, type:
system "CRTJVAPGM '/mydir/myclass.class' OPTIMIZE(20)"
where mydir is the path name of the directory in which your Java application
is saved, and where myclass is the name of your compiled Java application.
Note: You can specify an optimization level of up to 40.
An optimization level of 40 increases the efficiency of the Java application,
but it also limits debug capabilities. In the early stages of developing a Java application,
you may want to set your optimization level at 20 so you can more easily debug
your application. See the CRTJVAPGM command and the OPTIMIZE parameter for more
information.
- Press the Enter key.
A message appears,
stating that a Java program has been created for your class.
- Run the class file.
- Ensure that your Java classpath is
set up correctly.
- On the Qshell command line, type java followed
by HelloWorld to run your HelloWorld.class with
the Java virtual
machine. For example, enter java HelloWorld. You can also
use the Run Java (RUNJVA)
command on your iSeries server to run HelloWorld.class.
- "Hello World" prints to your screen if everything
was entered correctly. The shell prompt (by default, a $) appears, indicating
that the Qshell is ready for another command.
- Press F3 (Exit) or F12 (Disconnect) to return to the command
entry display.