|
|||||||
|
|
![]() |
|
|
|
|
||
|
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
|
Editors
Contact the Editors |
| Copyright © 1996-2008 Guild Companies, Inc. All Rights Reserved. |