kawaii_xw 发表于 2013-1-27 06:12:11

执行一个命令

 
<div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!----> 1 import java.io.*;
2
3 public class ExecCmd{
4    
5     public static void main(String[] args){
6       try {
7             String command = "java";
8             Process p =    Runtime.getRuntime().exec(command);
9             
10             InputStream in = p.getInputStream();
11             OutputStream out = new FileOutputStream(new File("c:\\temp.txt"));
12             byte[] buffer = new byte[1024];
13             int c = 0;
14             while ((c = in.read(buffer)) != -1) {
15                 out.write(buffer,0,c);
16             }
17             in.close();
18             out.close();
19             // Wait for it to finish running
20             try {
21                   p.waitFor();
22             }
23             catch(InterruptedException ie) {
24                 System.out.println( ie );
25             }
26
27             // Check the return code
28             int ret = p.exitValue();
29             if(ret == 0){
30                 System.exit(0);
31             }
32    
33       } catch (IOException e) {}
34     }
35 }
页: [1]
查看完整版本: 执行一个命令