- /***
- * 第一种方法,仅仅执行命令无须关注返回结果
- * @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();
Java如何调用Linux命令
最新推荐文章于 2024-08-17 21:45:00 发布