вторник, 27 января 2026 г.

UTLcmd PL/SQL package as wrapper of Java RunTime.exec method with read stdin, stdout, stderr outputs

Go to required Container(PDB)

SQL>
DEFINE s_container='CDB$ROOT';
--DEFINE s_container='PDB1';
--DEFINE s_container='PDB2';


ALTER SESSION SET container = &&s_container;

show con_name

Original UTLcmd PL/SQL package as wrapper of Java RunTime.exec method

/*
   Build by Vadim Loevski of Quest Software.
*/   
CREATE OR REPLACE AND RESOLVE JAVA SOURCE NAMED "UTLcmd" AS
import java.lang.Runtime;
public class UTLcmd
{
  public static void execute (String command)
  {
   try
      {
       Runtime rt = java.lang.Runtime.getRuntime();
       rt.exec(command);
      }
   catch(Exception e)
   {
    System.out.println(e.getMessage());
    return;
   }
  }
}
/

CREATE OR REPLACE PACKAGE UTLcmd IS
  PROCEDURE execute (cmd IN VARCHAR2) AS LANGUAGE JAVA NAME
           'UTLcmd.execute(java.lang.String)';
END;
/


/*======================================================================
| Supplement to the fifth edition of Oracle PL/SQL Programming by Steven
| Feuerstein with Bill Pribyl, Copyright (c) 1997-2009 O'Reilly Media, Inc. 
| To submit corrections or find more code samples visit
| http://oreilly.com/catalog/9780596514464/
*/

Improved version of UTLcmd PL/SQL package as wrapper of Java RunTime.exec method with read stdin, stdout, stderr outputs

CREATE OR REPLACE AND RESOLVE JAVA SOURCE NAMED "UTLcmd" AS
import java.lang.Runtime;
import java.lang.Process;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class UTLcmd
{
  public static void execute (String command)
  {
   try
      {
        Runtime rt = java.lang.Runtime.getRuntime();
        Process proc = rt.exec(command);

        BufferedReader stdInput = new BufferedReader(new 
             InputStreamReader(proc.getInputStream()));
        
        BufferedReader stdError = new BufferedReader(new 
             InputStreamReader(proc.getErrorStream()));
        
        // Read the output from the command
        System.out.println("Here is the standard output of the command:\n");
        String s = null;
        while ((s = stdInput.readLine()) != null) {
            System.out.println(s);
        }
        
        // Read any errors from the attempted command
        System.out.println("Here is the standard error of the command (if any):\n");
        while ((s = stdError.readLine()) != null) {
            System.out.println(s);
        }       
      }
   catch(Exception e)
   {
    System.out.println(e.getMessage());
    return;
   }
  }
}
/
 
CREATE OR REPLACE PACKAGE UTLcmd IS
  PROCEDURE execute (cmd IN VARCHAR2) AS LANGUAGE JAVA NAME
           'UTLcmd.execute(java.lang.String)';
END;
/

🛈 Create Java source "UTLcmd" and PL/SQL package ULTcmd in required schema (SCOTT for example). In this article both versions like "Original" & "Improved with read stdin, stdout, stderr outputs" was created and tested in SYS/SYSTEM schemas, in this case provide next grant to required user/schema to able execute PL/SQL UTLcmd package:

GRANT EXECUTE ON UTLcmd TO SCOTT;

Example of using improved version of UTLcmd PL/SQL package with read stdin, stdout, stderr outputs

CALL DBMS_JAVA.SET_OUTPUT (1000000);
exec UTLcmd.execute('du -hs /u01');
Here is the standard output of the command:
141G	/u01
exec UTLcmd.execute('ls -lht /u01');
Here is the standard output of the command:

total 24K
drwxr-xr-x 9 oracle oinstall 4.0K Jan 26  2026 app
Next command don't works, I don't know why but looks like it don't understand "*"
exec UTLcmd.execute('du -hs /u01/*');
Here is the standard output of the command:

Here is the standard error of the command (if any):

/bin/du: cannot access '/u01/*': No such file or directory

Комментариев нет:

Отправить комментарий