JDK 5.0プロパティ、ProcessBuilderを使用してローカルコマンドを実行

28088 ワード

  1 import java.io.BufferedReader;
  2 
  3 import java.io.BufferedWriter;
  4 
  5 import java.io.File;
  6 
  7 import java.io.IOException;
  8 
  9 import java.io.InputStream;
 10 
 11 import java.io.InputStreamReader;
 12 
 13 import java.io.OutputStreamWriter;
 14 
 15 import java.util.ArrayList;
 16 
 17 import java.util.Arrays;
 18 
 19 import java.util.Iterator;
 20 
 21 import java.util.List;
 22 
 23 import java.util.Map;
 24 
 25  
 26 
 27 /**
 28 
 29  *  J2SE5.0    Runtime exec        .
 30 
 31  *  J2Se5.0  ,    ProcessBuilder      
 32 
 33  *           ,          、     
 34 
 35  *   PorcessBuilder  Windows     "ipconfig/all"  ,       MAC  
 36 
 37 */
 38 
 39 /**      
 40 
 41  *                ProcessBuilder  ,  start      ,      ,    Process  
 42 
 43  * ProcessBuilder environment             ,    Map,        
 44 
 45  * ProcessBuilder directory        
 46 
 47  * Process getInputStream            ,getErrorStream            
 48 
 49 */
 50 
 51 public class UsingProcessBuilder {
 52 
 53        /**  Windows       MAC  */
 54 
 55        public static List<String> getPhysicalAddress(){
 56 
 57               Process p = null;
 58 
 59               List<String> address = new ArrayList<String>(); //      
 60 
 61               try{
 62 
 63                      p = new ProcessBuilder("ipconfig","/all").start(); //  ipconfig/all  
 64 
 65               }catch(IOException e){
 66 
 67                      return address;
 68 
 69               }
 70 
 71               byte[] b = new byte[1024];
 72 
 73               int readbytes = -1;
 74 
 75               StringBuffer sb = new StringBuffer();
 76 
 77               //       
 78 
 79               // JAVA IO ,       JVM  ,            
 80 
 81               InputStream in = p.getInputStream();
 82 
 83               try{
 84 
 85                      while((readbytes = in.read(b)) != -1){
 86 
 87                             sb.append(new String(b,0,readbytes));
 88 
 89                      }
 90 
 91               }catch(IOException e1){
 92 
 93               }finally {
 94 
 95                      try{
 96 
 97                             in.close();
 98 
 99                      }catch (IOException e2){
100 
101                      }
102 
103               }
104 
105               //        ,      
106 
107               String rtValue = sb.toString();
108 
109               int i = rtValue.indexOf("Physical Address. . . . . . . . . :");
110 
111               while (i > 0){
112 
113                      rtValue = rtValue.substring(i + "Physical Address. . . . . . . . . :".length());
114 
115                      address.add(rtValue.substring(1,18));
116 
117                      i = rtValue.indexOf("Physical Address. . . . . . . . . :");
118 
119               }
120 
121               return address;
122 
123        }
124 
125        /**          ,     C:/temp ,             */
126 
127        public static boolean executeMyCommand1(){
128 
129               //         
130 
131               ProcessBuilder pb = new ProcessBuilder("myCommand","myArg1","myArg2");
132 
133               Map<String, String> env = pb.environment(); //       
134 
135               //         
136 
137               env.put("VAR1", "myValue");
138 
139               env.remove("VAR0");
140 
141               env.put("VAR2", env.get("VAR1") + ";");
142 
143               //      ,         
144 
145               Iterator it=env.keySet().iterator();
146 
147               String sysatt = null;
148 
149               while(it.hasNext())
150 
151               {
152 
153                      sysatt = (String)it.next();
154 
155                      System.out.println("System Attribute:"+sysatt+"="+env.get(sysatt));
156 
157               }
158 
159               pb.directory(new File("C:/temp"));
160 
161               try{
162 
163                      Process p = pb.start(); //      
164 
165                      //        
166 
167                      if(p.waitFor() != 0){
168 
169                             //          0,          
170 
171                             //           
172 
173                             InputStream error = p.getErrorStream();
174 
175                             //do something
176 
177                      }
178 
179                      InputStream sdin = p.getInputStream(); //           
180 
181                      //do something
182 
183               }catch(IOException e){
184 
185               }catch(InterruptedException e){
186 
187               }
188 
189               return true;
190 
191        }
192 
193        public static void executeMyCommand2(){
194 
195               ProcessBuilder pb = null;
196 
197               String sysatt = null;
198 
199               try
200 
201         {
202 
203             //        
204 
205             pb = new ProcessBuilder("cmd.exe");
206 
207             //           
208 
209             Map<String, String> env = pb.environment();
210 
211             Iterator it=env.keySet().iterator();
212 
213             while(it.hasNext())
214 
215             {
216 
217                  sysatt = (String)it.next();
218 
219                 System.out.println("System Attribute:"+sysatt+"="+env.get(sysatt));
220 
221             }
222 
223             //      
224 
225             pb.directory(new File("d://myDir"));
226 
227             Process p = pb.start();
228 
229             //     Windows    
230 
231             BufferedWriter bw=new BufferedWriter(newOutputStreamWriter(p.getOutputStream()));
232 
233             //'/r/n'           
234 
235             bw.write("test.bat /r/n");
236 
237             bw.write("ping -t www.yahoo.com.cn /r/n");
238 
239             //flush()        
240 
241             bw.flush();
242 
243             //         
244 
245             InputStream is = p.getInputStream();
246 
247             InputStreamReader isr = new InputStreamReader(is, "GBK");
248 
249             BufferedReader br = new BufferedReader(isr);
250 
251             String line;
252 
253             while ((line = br.readLine()) != null)
254 
255             {
256 
257                 System.out.println(line);
258 
259             }
260 
261         }
262 
263         catch (Exception e)
264 
265         {
266 
267             e.printStackTrace();
268 
269         }
270 
271        }
272 
273        public static void main(String[] args){
274 
275               List<String> address = UsingProcessBuilder.getPhysicalAddress();
276 
277               for(String add : address){
278 
279                      System.out.printf("      : %s%n",add);
280 
281               }
282 
283               executeMyCommand1();
284 
285               executeMyCommand2();
286 
287        }
288 
289 }

 
ソース:http://www.cnblogs.com/taven/archive/2011/12/17/2291460.html