Newsletters Subscriptions Media Kit About Us Contact Search Home

mgo
OS/400 Edition
Volume 3, Number 45 -- July 16, 2003

Redirecting Java's Standard Output


Hey, David:

When I call a Java program using Qshell, I can redirect stdout and stderr to files [see "Stream I/O in Qshell"]. Is there any way to redirect output to a file when you call a Java program from an RPG IV program? I used to be able to hit "print screen" before my logging messages scrolled off of the screen, but we upgraded our system and now it is darn near impossible to catch the output.

--Lyle


Java supplies several methods that allow you to specify your own standard input and output files. The following Java class will redirect stderr and stdout to a temporary file:

package demo;

import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintStream;

public class StandardOutput {
  public static void main(String[] args) {
  sendOutput("before");
      redirect();
  sendOutput("after");
      System.exit(0);
  }

/**
 * Method sendOutput writes data to stderr and stdout.
 * @param output
 */
  public static void sendOutput(String output) {
    System.err.println("stderr: " + output);
    System.out.println("stdout: " + output);
  }

  /**
   * Method redirect stdout and stderr
   *    to default temporary directory.
   */
  public static void redirect() {
      redirect(null);
  }

  /**
   * Method redirect stdout and stderr
   *  to specified directory.
   * @param tempDirectory
   */
public static void redirect(File directory) {
  try {
    File tempFile = File.createTempFile(
          "stderr", "", directory);
    System.setErr(
        new PrintStream(new FileOutputStream(tempFile)));
    tempFile = File.createTempFile("stdout", 
                 "", directory);
    System.setOut(
        new PrintStream(new FileOutputStream(tempFile)));
      }
    catch (Throwable t) {
      System.err.println(
        "Error overriding standard output to file.");
      t.printStackTrace(System.err);
      }
  }
}

The redirect method does the work in this program. The sendOutput and main methods are included for debugging and testing. Calling the redirect method without a directory name will create temporary files in the default temporary directory with names that start with stderr and stdout, followed by number. The number is generated by Java to ensure that temporary files are unique.

The following RPG IV code shows how you can use the StandardOutput class in an RPG program:

*******************************************
*  TSTSTDOUT - Demonstrate use of         *
*  StandardOutput java class from RPTGIV  *
*******************************************
DStandardOutput$redirect...              
D             PR              STATIC EXTPROC(
D                               *JAVA:
D                               'demo.StandardOutput':
D                               'redirect')
DStandardOutput$sendOutput...  
D             PR              STATIC EXTPROC(
D                               *JAVA:
D                               'demo.StandardOutput':
D                               'sendOutput')
D                         O   CLASS(*JAVA:'java.lang.String')
D                             CONST
DnewString    PR          O   EXTPROC(
D                              *JAVA:
D                               'java.lang.String':
D                               *CONSTRUCTOR)
D                      100A   CONST VARYING
Dbefore       S           O   CLASS(*JAVA:'java.lang.string')
Dafter        S           O   CLASS(*JAVA:'java.lang.string')
 /free
   before = newString('before');
   StandardOutput$sendOutput(before);
   StandardOutput$redirect();
   after = newString('after');
   StandardOutput$sendOutput(after);
   *INLR = *ON;
 /end-free

Running this RPGIV program will cause output written after the redirect call to go to temporary files in the /tmp directory. Use the following steps to compile and run these programs on your iSeries system.

Copy the StandardOutput Java source to a demo directory on your iSeries system. On my system I created demo in an examples directory. Start Qshell using the QSH command. Change to the directory containing the demo directory, and set your class path and compile the SystemOutput using the following commands:

cd /examples
export -s CLASSPATH=.
javac demo/StandardOutput.java

You should now have a StandardOutput.class file in your demo directory. If you are curious, you can call the main method using java demo.StandardOutput.

Now copy the RPG IV source to a source member and create a program using the Create Bound RPG (CRTBNDRPG) command:

CRTBNDRPG 
  PGM(mylib/TSTSTDOUT) 
  SRCFILE(mylib/QRPGLESRC) 
  SRCMBR(TSTSTDOUT) 
 DFTACTGRP(*NO)

Finally, set your CLASSPATH using the add environment variable (ADDENVVAR) command:

 ADDENVVAR ENVVAR('CLASSPATH') VALUE('/examples')

Now call the program. If you run the Work Link (WRKLNK) command and navigate to /tmp, you will find the redirected output.

--David


Sponsored By
COMMON

COMMON Fall 2003 Conference and Expo
IT in the Palm of Your Hand
September 7 - 11, in Orlando, Florida.

· Education: Hundreds of sessions over five days
· Networking: Meet more than 3,000 IT professionals, IBM executives, and developers
· Influence IBM: Attend sound-off sessions
· First-Hand Look at Latest Products: Talk to the industry's top solution providers

COMMON is the best value in IT education, so don't miss out!

Click and visit www.common.org for details!


THIS ISSUE
SPONSORED BY:

Advanced Systems Concepts
COMMON


BACK ISSUES

TABLE OF
CONTENTS

Grep and Database Files

Redirecting Java's Standard Output

Reader Feedback and Insights: SQL Versus DDS


Editors
Howard Arner
Joe Hertvik
Ted Holt
David Morris
Shannon O'Donnell

Managing Editor
Shannon Pastore

Publisher and
Advertising Director:

Jenny Thomas

Advertising Sales Representative
Kim Reed

Contact the Editors
Do you have a gripe, inside dope or an opinion?
Email the editors:
editors@itjungle.com

Copyright © 1996-2008 Guild Companies, Inc. All Rights Reserved.