java 执行系统命令的方法


有两种执行系统命令的方法,简单的方法会挂起JVM

01
import java.io.IOException;
02
import java.io.InputStream;
03
 
04
public class MainEntry {
05
 
06
    public static void main(String[] args) {
07
        executeCmd("ls -oa");
08
    }
09
 
10
    private static void executeCmd(String string) {
11
 
12
        InputStream pipedOut = null;
13
 
14
        try {
15
            Process aProcess = Runtime.getRuntime().exec(string);
16
 
17
            aProcess.waitFor();
18
            pipedOut = aProcess.getInputStream();
19
            byte buffer[] = new byte[2048];
20
            int read = pipedOut.read(buffer);
21
 
22
            // Replace following code with your intends processing tools
23
            while(read >= 0) {
24
                System.out.write(buffer, 0, read);              
25
                read = pipedOut.read(buffer);
26
            }
27
        } catch (IOException e) {
28
            e.printStackTrace();
29
        } catch (InterruptedException ie) {
30
            ie.printStackTrace();
31
        } finally {
32
            if(pipedOut != null) {
33
                try {
34
                    pipedOut.close();
35
                } catch (IOException e) {
36
                }
37
            }
38
        }
39
    }
40
}
正确的方法使用进程提供的线程去读取InputStream。

001
import java.io.IOException;
002
 
003
import java.io.InputStream;
004
 
005
   
006
 
007
public class MainEntry {
008
 
009
    public static void main(String[] args) {
010
 
011
        // the command to execute
012
 
013
        executeCmd("ls -oa");
014
 
015
    }
016
 
017
   
018
 
019
    private static void executeCmd(String string) {
020
 
021
        InputStream pipedOut = null;
022
 
023
        try {
024
 
025
            Process aProcess = Runtime.getRuntime().exec(string);
026
 
027
   
028
 
029
            // These two thread shall stop by themself when the process end
030
 
031
            Thread pipeThread = new Thread(newStreamGobber(aProcess.getInputStream()));
032
 
033
            Thread errorThread = new Thread(newStreamGobber(aProcess.getErrorStream()));
034
 
035
             
036
 
037
            pipeThread.start();
038
 
039
            errorThread.start();
040
 
041
             
042
 
043
            aProcess.waitFor();
044
 
045
        } catch (IOException e) {
046
 
047
            e.printStackTrace();
048
 
049
        } catch (InterruptedException ie) {
050
 
051
            ie.printStackTrace();
052
 
053
        }
054
 
055
    }
056
 
057
}
058
 
059
   
060
 
061
   
062
 
063
class StreamGobber implements Runnable {
064
 
065
   
066
 
067
    private InputStream Pipe;
068
 
069
   
070
 
071
    public StreamGobber(InputStream pipe) {
072
 
073
        if(pipe == null) {
074
 
075
            throw new NullPointerException("bad pipe");
076
 
077
        }
078
 
079
        Pipe = pipe;
080
 
081
    }
082
 
083
   
084
 
085
    public void run() {
086
 
087
        try {
088
 
089
            byte buffer[] = new byte[2048];
090
 
091
   
092
 
093
            int read = Pipe.read(buffer);
094
 
095
            while(read >= 0) {
096
 
097
                System.out.write(buffer, 0, read);
098
 
099
   
100
 
101
                read = Pipe.read(buffer);
102
 
103
            }
104
 
105
        } catch (IOException e) {
106
 
107
            e.printStackTrace();
108
 
109
        } finally {
110
 
111
            if(Pipe != null) {
112
 
113
                try {
114
 
115
                    Pipe.close();
116
 
117
                } catch (IOException e) {
118
 
119
                }
120
 
121
            }
122
 
123
        }
124
 
125
    }
126
 
127
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值