1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 package installer;
23 import java.util.*;
24 import java.io.*;
25 public class ExecCmd
26 {
27 
exec( String cmd, String[] env )28     public boolean exec( String cmd, String[] env )
29     {
30        System.out.println("About to exectute " + cmd);
31        final Process p;
32        boolean result = false;
33        try
34        {
35            Runtime rt = Runtime.getRuntime();
36            p=rt.exec( cmd, env );
37            new Thread(new Runnable() {
38                public void run()
39                {
40                    BufferedReader br_in = null;
41                    try
42                    {
43                        br_in = new BufferedReader(new InputStreamReader(p.getInputStream()));
44                        String buff = null;
45                        while ((buff = br_in.readLine()) != null)
46                        {
47                            System.out.println("Process out :" + buff);
48                            /*try
49                            {
50                                Thread.sleep(100);
51                            }
52                            catch(Exception e) {}*/
53                        }
54                        System.out.println("finished reading out");
55                     }
56                     catch (IOException ioe)
57                     {
58                         System.out.println("Exception caught printing javac result");
59                         ioe.printStackTrace();
60                     }
61                     finally
62                     {
63                        if ( br_in != null )
64                        {
65                            try
66                            {
67                                br_in.close();
68                            }
69                            catch( Exception e ) {} // nothing can be done
70                        }
71                     }
72                } } ).start();
73 
74             new Thread(new Runnable() {
75                 public void run() {
76                 BufferedReader br_err = null;
77                 try {
78                     br_err = new BufferedReader(new InputStreamReader(p.getErrorStream()));
79                     String buff = null;
80                     while ((buff = br_err.readLine()) != null) {
81                     System.out.println("Process err :" + buff);
82                     /*try {Thread.sleep(100); } catch(Exception e) {}*/
83                  }
84                  System.out.println("finished reading err");
85                  } catch (IOException ioe) {
86                     System.out.println("Exception caught printing javac result");
87                     ioe.printStackTrace();
88                  }
89                  finally
90                  {
91                     if ( br_err != null )
92                     {
93                         try
94                         {
95                             br_err.close();
96                         }
97                         catch( Exception e ) {} // nothing can be done
98                     }
99                  }
100             } }).start();
101             int exitcode = p.waitFor();
102             if ( exitcode != 0 )
103             {
104                 System.out.println("cmd [" + cmd + "] failed" );
105                 result= false;
106             }
107             else
108             {
109                 System.out.println("cmd [" + cmd + "] completed successfully");
110                 result= true;
111             }
112         }
113         catch (Exception e) {
114           System.out.println("Exception");
115           e.printStackTrace();
116        }
117        System.out.println("command complete");
118        return result;
119     }
120 }
121 
122