Create, compile, and run a HelloWorld Java program

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:

  1. Map a network drive to your iSeries™ server.
  2. Create a directory on your iSeries server for your Java applications.
  3. 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.
    1. 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.
    2. Make sure that your file contains this source code:
           class HelloWorld {
                public static void main (String args[]) {
                     System.out.println("Hello World");
                }
           }
  4. Compile the source file.
    1. 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.
    2. Enter the Start Qshell (STRQSH) command to start the Qshell Interpreter.
    3. Use the change directory (cd) command to change the current directory to the integrated file system directory that contains the HelloWorld.java file.
    4. Enter javac followed by the name of the file as you have it saved on your disk. For example, enter javac HelloWorld.java.
  5. Set the file authorities on the class file in the integrated file system.
  6. Optimize the Java application.
    1. 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.

    2. Press the Enter key.

      A message appears, stating that a Java program has been created for your class.

  7. Run the class file.
    1. Ensure that your Java classpath is set up correctly.
    2. 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.
    3. "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.
    4. Press F3 (Exit) or F12 (Disconnect) to return to the command entry display.

You can also easily compile, optimize, and run your Java application using iSeries Navigator, a graphical user interface for performing tasks on your iSeries server. For instructions, see iSeries Navigator commands that are supported by Java. For more information on iSeries Navigator, including installation information, see Getting to know iSeries Navigator.

Related concepts
Create a directory on your iSeries server
Related tasks
Install IBM Developer Kit for Java
Run your first Hello World Java program
Map a network drive to your iSeries server
Related reference
Create and edit Java source files