你好,游客 登录 注册 搜索
背景:
阅读新闻

Oracle Java Stored Procedure调用Unix shell

[日期:2013-03-06] 来源:Linux社区  作者:battlehawk [字体:大 中 小]

2.

CREATE OR REPLACE JAVA SOURCE NAMED "ExecOSCmd" as
import java.util.*;
import java.io.*;
public class ExecOSCmd {
    public static void main (String args[])
    {
        int rc = 0;
        if (args.length < 1) {
            System.out.println("USAGE: java ExecOSCmd \'cmd\' ");
            System.exit(1);
        }

        try {
            String cmd = args[0];
            FileOutputStream fos = null;
            if (args.length == 2 ) {
                fos = new FileOutputStream(args[1]);
            }
            Runtime rt = Runtime.getRuntime();
            Process p = rt.exec(cmd);


            CaptureStream err = new CaptureStream(p.getErrorStream(), "ERR");           
            Thread e = new Thread(err);

            /* NOTE we do not join the error thread.  If there was no
        Error it may never return.  We would wait forever for it to
        return thus janging this process */
            e.start();

            CaptureStream out = new CaptureStream(p.getInputStream(),
                                                  "OUT", fos);
            Thread o = new Thread(out);
            o.start();

            try {
                rc = p.waitFor();
                /* Handle exceptions for waitFor() */
            } catch (InterruptedException intexc) {
                System.out.println("Interrupted Exception on waitFor: " +
                                  intexc.getMessage());
            }

            if (fos !=null) {
                o.join();      // need to wait for the output to finish.
                fos.flush();
                fos.close();
            }

            System.out.println("ExitValue: " + rc);

        } catch (Throwable t) {
            System.out.println("ExitValue: " + rc);
            t.printStackTrace();
        }
    }
};
/

linux
相关资讯       Unix shell  Stored Procedure 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款