当前位置: 代码迷 >> Oracle开发 >> oracle的存储过程中调用shell,该如何处理
  详细解决方案

oracle的存储过程中调用shell,该如何处理

热度:41   发布时间:2016-04-24 06:30:59.0
oracle的存储过程中调用shell
先贴一下我自己的测试代码,之前在网上查的。但是始终测试不对。请哪位大神帮忙看看。注意是proc调shell。不是shell调proc!!测试机y有jre,但是没有jdk。不知道是不是这个的原因。之前有个包的实现,也做了测试。最后跑job的时候也报错。
1,先写一个java source
create or replace and compile java source named execshellcmd as
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

public class Execshellcmd {
  public static String callShell(String path) {
    String message = "";

   try {
      Process process = Runtime.getRuntime().exec(path);
      int waitFor = process.waitFor();

      InputStream is = process.getErrorStream();
      InputStreamReader isr = new InputStreamReader(is);
      BufferedReader br = new BufferedReader(isr);
      String line = null;
      while ((line = br.readLine()) != null) {
        message += line;
      }

      message += " Process waitFor=" + waitFor;
      int exitValue = process.exitValue();
      message += " Process exitValue=" + exitValue;
    } catch (Exception e) {
      message = e.getMessage();
      e.printStackTrace();
    }

    return message;
  }

}
2,写一个函数去调用java source
create or replace function fun_callshell (p_command  IN  VARCHAR2) return varchar2
AS LANGUAGE JAVA
NAME'Execshellcmd.callShell (java.lang.String) return java.lang.String';
3,执行一个脚本,脚本内容就是创建一个文件 echo aaa>b.txt
select fun_callshell (‘home/oracle/b.sh’) from dual;
执行结果:
exec failed: /home/oracle/b.sh, Exec format error Process waitFor=255 Process exitValue=255



------解决思路----------------------
系统命令能成功调用并看到结果了?
注意下路径和环境变量
  相关解决方案