shell java 执行

java shell 命令 

Java代码    收藏代码
  1. package com.taskschedule.util;  
  2.   
  3. import java.io.BufferedReader;  
  4. import java.io.InputStreamReader;  
  5. import java.util.ArrayList;  
  6. import java.util.List;  
  7.   
  8. import com.taskschedule.util.StreamGobbler;  
  9.   
  10. /** 
  11.  *  
  12.  * @author baoyou E-mail:curiousby@163.com 
  13.  * @version 2016年11月2日 下午1:54:49 
  14.  * desc: 
  15.  */  
  16. public class ShellProcess {  
  17.   
  18.     private static ShellProcess instance;  
  19.   
  20.     public static ShellProcess getInstance() {  
  21.         if (instance == null) {  
  22.             return new ShellProcess();  
  23.         }  
  24.         return instance;  
  25.     }  
  26.   下载
  27.     /** 
  28.      * 执行相应shell脚本 
  29.      * @param args 执行脚本的参数,[0]path:shell脚本路径;[1~n]脚本入参 
  30.      * @return     返回码,0:成功  1:失败 
  31.      */  
  32.     public int runShell(String[] args) {  
  33.         int runRes = SystemGlobal.FAILED;  
  34.         try {  
  35.             Process process = Runtime.getRuntime().exec(args);//调用相应shell脚本  
  36.             new StreamGobbler(process.getInputStream(), StreamGobbler.INFO).start();  
  37.             new StreamGobbler(process.getErrorStream(), StreamGobbler.ERROR).start();  
  38.             runRes = process.waitFor();  
  39.         } catch (Exception e) {  
  40.             e.printStackTrace();  
  41.         }  
  42.         return runRes;  
  43.     }  
  44.   
  45.     /** 
  46.      * 执行相应shell命令 
  47.      * @param cmd 执行的命令 
  48.      * @return    返回码,0:成功  1:失败 
  49.      */  
  50.     public int runShell(String cmd) {  
  51.         int runRes = SystemGlobal.FAILED;  
  52.         try {  
  53.             Process process = Runtime.getRuntime().exec(cmd);//执行shell命令  
  54.             new StreamGobbler(process.getInputStream(), StreamGobbler.INFO).start();  
  55.             new StreamGobbler(process.getErrorStream(), StreamGobbler.ERROR).start();  
  56.             runRes = process.waitFor();  
  57.         } catch (Exception e) {  
  58.             e.printStackTrace();  
  59.         }  
  60.         return runRes;  
  61.     }  
  62.   
  63.     /** 
  64.      * 自动根据运行时环境执行shell命令 
  65.      * @param args shell命令以空格分割后的list 
  66.      * @return     返回码,0:成功  1:失败 
  67.      */  
  68.     public int runShell(List<String> args) {  
  69.         int runRes = SystemGlobal.FAILED;  
  70.         try {  
  71.             ProcessBuilder pb = new ProcessBuilder(args);  
  72.             Process process = pb.start();  
  73.             new StreamGobbler(process.getInputStream(), StreamGobbler.INFO).start();  
  74.             new StreamGobbler(process.getErrorStream(), StreamGobbler.ERROR).start();  
  75.             runRes = process.waitFor();  
  76.         } catch (Exception e) {  
  77.             e.printStackTrace();  
  78.         }  
  79.         return runRes;  
  80.     }  
  81.   下载
  82.     /** 
  83.      * 执行shell命令,并获取返回结果 
  84.      * @param args shell命令以空格分割后的list 
  85.      * @return     执行shell命令后的返回结果(按行分割后的list),如果发生异常,返回空List 
  86.      */  
  87.     public List<String> runShellWithResult(List<String> args) {  
  88.         List<String> results = new ArrayList<String>();  
  89.         try {  
  90.             ProcessBuilder pb = new ProcessBuilder(args);  
  91.             Process process = pb.start();  
  92.             BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));  
  93.             String data = null;  
  94.             while ((data = br.readLine()) != null) {  
  95.                 results.add(data);  
  96.             }  
  97.             new StreamGobbler(process.getInputStream(), StreamGobbler.INFO).start();  
  98.             new StreamGobbler(process.getErrorStream(), StreamGobbler.ERROR).start();  
  99.             process.waitFor();  
  100.         } catch (Exception e) {  
  101.             e.printStackTrace();  
  102.             return new ArrayList<String>();  
  103.         }  
  104.         return results;  
  105.     }  
  106. }  

 

 

Java代码    收藏代码
  1. package com.taskschedule.util;  
  2.   
  3. import java.io.BufferedReader;  
  4. import java.io.IOException;  
  5. import java.io.InputStream;  
  6. import java.io.InputStreamReader;  
  7. import org.apache.log4j.Logger;  
  8.   
  9. /** 
  10.  *  
  11.  * @author baoyou E-mail:curiousby@163.com 
  12.  * @version 2016年11月2日 下午1:54:57 
  13.  * desc: 
  14.  */  
  15. public class StreamGobbler extends Thread {  
  16.     private InputStream is;  
  17.     private int type;  
  18.     private static Logger logger = Logger.getLogger(StreamGobbler.class);  
  19.   
  20.     public static final int INFO = 0;  
  21.     public static final int ERROR = 1;  
  22.   
  23.     public StreamGobbler(InputStream is, int type) {  
  24.         this.is = is;  
  25.         this.type = type;  
  26.     }  
  27.   下载
  28.     @Override  
  29.     public void run() {  
  30.         try {  
  31.             InputStreamReader isr = new InputStreamReader(is);  
  32.             BufferedReader br = new BufferedReader(isr);  
  33.             String line = null;  
  34.             while ((line = br.readLine()) != null) {  
  35.                 if (type == INFO) {  
  36.                     logger.info(line);  
  37.                 } else if (type == ERROR) {  
  38.                     logger.error(line);  
  39.                 }  
  40.             }  
  41.         } catch (IOException ioe) {  
  42.             ioe.printStackTrace();  
  43.         }  
  44.     }  
  45. }  

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值