阅读背景:

Java如何调用Linux命令

来源:互联网 


(方法一)合适Java调用本地的Liunx不必任何依附包

    /***
     * 第一种办法,仅仅履行命令不必关注返回成果
     * @throws Exception
     */
    public static void exeCmd() throws Exception{
        Runtime r = Runtime.getRuntime();
        //履行linux命令,不关怀返回成果,此处,可以履行一个shell脚本,或python脚本
        Process p = r.exec("tesseract 12.jpg  ko ");
        p.waitFor();
    }


    /**
     * 第二种办法,须要履行命令完后的返回成果
     * @return  result
     * @throws Exception
     */
    public static String getCodeResult()throws Exception{
        exeCmd();
        //履行一个命令须要展现返回成果的
        Runtime r = Runtime.getRuntime();
        Process p = r.exec("cat ko.txt ");
        p.waitFor();
        BufferedReader b = new BufferedReader(new InputStreamReader(p.getInputStream()));
        String line = "";
        StringBuffer sb=new StringBuffer();
        while ((line = b.readLine()) != null) {
          sb.append(line).append("\n");
        }
       System.out.println("result: "+sb.toString());
        b.close();
        return sb.toString();

    }

    /***
     * 第一




你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: