1 package installer;
2 import java.util.*;
3 import java.io.*;
4 public class ExecCmd
5 {
6 
7     public boolean exec( String cmd, String[] env )
8     {
9        System.out.println("About to exectute " + cmd);
10        final Process p;
11        boolean result = false;
12        try
13        {
14            Runtime rt = Runtime.getRuntime();
15            p=rt.exec( cmd, env );
16            new Thread(new Runnable() {
17                public void run()
18                {
19                    BufferedReader br_in = null;
20                    try
21                    {
22                        br_in = new BufferedReader(new InputStreamReader(p.getInputStream()));
23                        String buff = null;
24                        while ((buff = br_in.readLine()) != null)
25                        {
26                            System.out.println("Process out :" + buff);
27                            /*try
28                            {
29                                Thread.sleep(100);
30                            }
31                            catch(Exception e) {}*/
32                        }
33                        System.out.println("finished reading out");
34                     }
35                     catch (IOException ioe)
36                     {
37                         System.out.println("Exception caught printing javac result");
38                         ioe.printStackTrace();
39                     }
40                     finally
41                     {
42                        if ( br_in != null )
43                        {
44                            try
45                            {
46                                br_in.close();
47                            }
48                            catch( Exception e ) {} // nothing can be done
49                        }
50                     }
51                } } ).start();
52 
53             new Thread(new Runnable() {
54                 public void run() {
55                 BufferedReader br_err = null;
56                 try {
57                     br_err = new BufferedReader(new InputStreamReader(p.getErrorStream()));
58                     String buff = null;
59                     while ((buff = br_err.readLine()) != null) {
60                     System.out.println("Process err :" + buff);
61                     /*try {Thread.sleep(100); } catch(Exception e) {}*/
62                  }
63                  System.out.println("finished reading err");
64                  } catch (IOException ioe) {
65                     System.out.println("Exception caught printing javac result");
66                     ioe.printStackTrace();
67                  }
68                  finally
69                  {
70                     if ( br_err != null )
71                     {
72                         try
73                         {
74                             br_err.close();
75                         }
76                         catch( Exception e ) {} // nothing can be done
77                     }
78                  }
79             } }).start();
80             int exitcode = p.waitFor();
81             if ( exitcode != 0 )
82             {
83                 System.out.println("cmd [" + cmd + "] failed" );
84                 result= false;
85             }
86             else
87             {
88                 System.out.println("cmd [" + cmd + "] completed successfully");
89                 result= true;
90             }
91         }
92         catch (Exception e) {
93           System.out.println("Exception");
94           e.printStackTrace();
95        }
96        System.out.println("command complete");
97        return result;
98     }
99 }
100 
101