如何从Toad调用shell脚本

时间:2016-09-19 09:31:57

标签: oracle unix

我尝试了以下链接的步骤:

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

它包含在

  • 使用

    创建存储为BASH_OS函数的java对象
    CREATE OR REPLACE AND COMPILE JAVA SOURCE NAMED "BASH_OS"..
    
  • 然后用

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

但它显示

the object BASH_OS does not exist.

任何人都可以帮助我了解如何使用此方法从Toad执行shell脚本吗?

或者是否还有其他方法可以从Toad调用shell脚本?

1 个答案:

答案 0 :(得分:0)

引用您的链接,我发现第13行中的类名称对于创建BASH_OS是错误的。

类名称应为“ BASH_OS”,而不是“ Host”。由于博客上没有评论功能,因此我将固定代码复制到此处。

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());
    }
  }

};