java 执行系统命令的方法

来源:互联网 发布:node vendor 编辑:程序博客网 时间:2024/06/05 16:52

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

01
import java.io.IOException;
02
import java.io.InputStream;
03
 
04
public class MainEntry {
05
 
06
    public static voidmain(String[] args) {
07
       executeCmd("ls -oa");
08
    }
09
 
10
    private static voidexecuteCmd(String string) {
11
 
12
       InputStream pipedOut = null;
13
 
14
       try {
15
           ProcessaProcess = Runtime.getRuntime().exec(string);
16
 
17
          aProcess.waitFor();
18
           pipedOut =aProcess.getInputStream();
19
           bytebuffer[] = new byte[2048];
20
           int read =pipedOut.read(buffer);
21
 
22
           // Replacefollowing 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 voidmain(String[] args) {
010
 
011
       // the command to execute
012
 
013
       executeCmd("ls -oa");
014
 
015
    }
016
 
017
  
018
 
019
    private static voidexecuteCmd(String string) {
020
 
021
       InputStream pipedOut = null;
022
 
023
       try {
024
 
025
           ProcessaProcess = Runtime.getRuntime().exec(string);
026
 
027
  
028
 
029
           // Thesetwo thread shall stop by themself when the process end
030
 
031
           ThreadpipeThread = newThread(newStreamGobber(aProcess.getInputStream()));
032
 
033
           ThreaderrorThread = newThread(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 InputStreamPipe;
068
 
069
  
070
 
071
    publicStreamGobber(InputStream pipe) {
072
 
073
       if(pipe == null) {
074
 
075
           throw newNullPointerException("bad pipe");
076
 
077
       }
078
 
079
       Pipe = pipe;
080
 
081
    }
082
 
083
  
084
 
085
    public void run(){
086
 
087
       try {
088
 
089
           bytebuffer[] = 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