简体   繁体   中英

How to call shell script from Toad

I tried with the below linked steps:

https://slobaray.com/tag/execute-shell-script-from-plsql/

It consists in

  • creating a java object stored as BASH_OS function with

     CREATE OR REPLACE AND COMPILE JAVA SOURCE NAMED "BASH_OS".. 
  • Then to execute the java with

      CREATE OR REPLACE PROCEDURE unix_command (p_command IN VARCHAR2) AS LANGUAGE JAVA NAME 'BASH_OS.executeCommand (java.lang.String)'; 

But It shows that

 the object BASH_OS does not exist. 

Can any one help me on how we can execute the shell script from the Toad with this method?

or Is there any other method to call shell script from Toad?

Referring your link, I found that the class name in line 13 is wrong for creating BASH_OS.

The class name should be "BASH_OS" instead of "Host". Since there is no comment feature on the blog, so i copy the fixed code here.

CREATE OR REPLACE AND COMPILE JAVA SOURCE NAMED "BASH_OS" AS
/******************************************************************************
       NAME:       BASH_OS
       PURPOSE:    To perform the shell command using Java class 

       REVISIONS:
       Ver        Date        Author    Description
       ---------  ----------  ------   ---------------------------------------------
       0.1        <<>>        S.Ray      Initial Version

    ******************************************************************************/
    import java.io.*;
/*  public class Host {   <-- THIS IS WRONG */
public class BASH_OS {
  public static void executeCommand(String command) {
  try {
      String[] finalCommand;
       {
        finalCommand = new String[3];
        finalCommand[0] = "/bin/sh";
         finalCommand[1] = "-c";
        finalCommand[2] = command;
      }

      final Process pr = Runtime.getRuntime().exec(finalCommand);
      pr.waitFor();

      new Thread(new Runnable(){
        public void run() {
          BufferedReader br_in = null;
          try {
            br_in = new BufferedReader(new  InputStreamReader(pr.getInputStream()));
            String buff = null;
            while ((buff = br_in.readLine()) != null) {
          System.out.println("Process out :" + buff);
              try {Thread.sleep(100); } catch(Exception e) {}
            }
            br_in.close();
          }
          catch (IOException ioe) {
            System.out.println("Exception caught printing process output.");
            ioe.printStackTrace();
          }
          finally {
            try {
              br_in.close();
            } catch (Exception ex) {}
          }
        }
      }).start();

      new Thread(new Runnable(){
        public void run() {
          BufferedReader br_err = null;
          try {
            br_err = new BufferedReader(new InputStreamReader(pr.getErrorStream()));
            String buff = null;
            while ((buff = br_err.readLine()) != null) {
              System.out.println("Process err :" + buff);
              try {Thread.sleep(100); } catch(Exception e) {}
            }
            br_err.close();
          }
          catch (IOException ioe) {
            System.out.println("Exception caught printing process error.");
            ioe.printStackTrace();
          }
          finally {
            try {
              br_err.close();
            } catch (Exception ex) {}
          }
        }
      }).start();
    }
    catch (Exception ex) {
      System.out.println(ex.getLocalizedMessage());
    }
  }

};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM