How to Execute an Operating System Command From Oracle Reports

How to Execute an Operating System Command From Oracle Reports

Titleimage

Posted by Marion Pertin on 2015:08:21 14:41:33

APPLIES TO

Oracle Reports Developer - Version 9.0.4.0 to 11.1.1.4.0 [Release Oracle9i to 11g]
Information in this document applies to any platform.

GOAL

In Oracle Forms, the builtin HOST can be used to execute an indicated operating system command.

This note explains how to use Java in Oracle Reports to perform a similar function .

Caution

The sample programs in this article are provided for educational purposes only and are NOT supported by Oracle Support Services. They have been tested internally, however, and work as documented. We do not guarantee that they will work for you, so be sure to test them in your environment before relying on them.

FIX

Import the java class : java.lang.Runtime

  1. Menu Program -> Import Java Classes ...
  2. Enter 'java.lang.Runtime' in field 'Import Classes:'
  3. Press

Use the function exec (to call this function, it is necessary to retrieve the "runtime" object)
Example:

function BeforeReport return boolean is
  rt ORA_JAVA.JOBJECT;
  proc ORA_JAVA.JOBJECT;
begin
  rt := RUNTIME.GetRuntime();
  proc := RUNTIME.exec(rt,'cmd /c set > e:\temp\getruntime.txt');

  return (TRUE);
end;

This example will return the environment settings for a windows environment.

If you want to wait for the "OS command" completion, import the java class java.lang.Process and use the method waitfor (This is required if you want to read the file generated by the OS command) :

function BeforeReport return boolean is 
  rt ORA_JAVA.JOBJECT; 
  proc ORA_JAVA.JOBJECT; 
  l_rc NUMBER;
begin 
  rt := RUNTIME.GetRuntime(); 
  proc := RUNTIME.exec(rt,'cmd /c set > e:\temp\getruntime.txt'); 
  l_rc := PROCESS.waitfor (proc); 

  return (TRUE); 
end;

Here below, an example for UNIX systems to retrieve the environment settings :

 function BeforeReport return boolean is   
  rt ORA_JAVA.JOBJECT;   
  proc ORA_JAVA.JOBJECT;   
  cmdarray ORA_JAVA.JOBJECT;   
  l_temp_outfile VARCHAR2(2048);   
  l_rc NUMBER;   
BEGIN   
-- java.lang.Runtime and java.lang.Process must be imported.   

begin   
l_temp_outfile := SRW.CREATE_TEMPORARY_FILENAME;   

rt := RUNTIME.GetRuntime();   

-- Create a STRING ARRAY to pass to the method exec   

cmdarray := ORA_JAVA.NEW_STRING_ARRAY(3);   
ORA_JAVA.SET_STRING_ARRAY_ELEMENT (cmdarray, 0, '/bin/sh');   
ORA_JAVA.SET_STRING_ARRAY_ELEMENT (cmdarray, 1, '-c');   
ORA_JAVA.SET_STRING_ARRAY_ELEMENT (cmdarray, 2, 'env >' || l_temp_outfile || ' 2>&1');   

proc := RUNTIME.exec(rt,cmdarray);   

-- Wait for the process completion   
l_rc := PROCESS.waitfor (proc);   

exception   
when others then null;   
end;   

return(TRUE);   
end;

 

Posted by Marion Pertin on 2015:08:21 14:41:33

Return to Blog