1 import java.io.BufferedReader;
2 import java.io.IOException;
3 import java.io.InputStream;
4 import java.io.InputStreamReader;
5
6 class StreamGobbler extends Thread {
7 InputStream is;
8 String type;
9
10 StreamGobbler(InputStream is, String type) {
11 this.is = is;
12 this.type = type;
13 }
14
15 public void run() {
16 try {
17 InputStreamReader isr = new InputStreamReader(is);
18 BufferedReader br = new BufferedReader(isr);
19 String line=null;
20 while ( (line = br.readLine()) != null)
21 System.out.println(type + ">" + line);
22 } catch (IOException ioe) {
23 ioe.printStackTrace();
24 }
25 }
26 }
27
28 public class BadExecJavac {
29 public static void main(String args[]) {
30 if (args.length < 1) {
31 System.out.println("USAGE: java GoodWindowsExec <cmd>");
32 System.exit(1);
33 }
34
35 try {
36 String osName = System.getProperty("os.name" );
37 String[] cmd = new String[3];
38
39 if( osName.equals( "Windows NT" ) ) {
40 cmd[0] = "cmd.exe" ;
41 cmd[1] = "/C" ;
42 cmd[2] = args[0];
43 } else if( osName.equals( "Windows XP" ) ) {
44 cmd[0] = "cmd.exe" ;
45 cmd[1] = "/C" ;
46 cmd[2] = args[0];
47 } else if( osName.equals( "Windows 95" ) ) {
48 cmd[0] = "command.com" ;
49 cmd[1] = "/C" ;
50 cmd[2] = args[0];
51 }
52
53 Runtime rt = Runtime.getRuntime();
54 System.out.println("Execing " + cmd[0] + " " + cmd[1]
55 + " " + cmd[2]);
56 Process proc = rt.exec(cmd);
57 // any error message?
58 StreamGobbler errorGobbler = new
59 StreamGobbler(proc.getErrorStream(), "ERROR");
60
61 // any output?
62 StreamGobbler outputGobbler = new
63 StreamGobbler(proc.getInputStream(), "OUTPUT");
64
65 // kick them off
66 errorGobbler.start();
67 outputGobbler.start();
68
69 // any error???
70 int exitVal = proc.waitFor();
71 System.out.println("ExitValue: " + exitVal);
72 } catch (Throwable t) {
73 t.printStackTrace();
74 }
75 }
76 }