Java如何调用Linux命令

由于有不少java代码,格式可能在手机上显示比较乱,可以点击最下面阅读原文查看比较整洁的格式
(方式一)适合Java调用本地的Liunx无须任何依赖包
Java代码
- /***
- * 第一种方法,仅仅执行命令无须关注返回结果
- * @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();
- }
(方式二)适合远程调用不在同一台机器上的Linux命令,这种方式需要使用