| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 
 |  
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.StringBufferInputStream;
import java.util.HashMap;
 
/*
 * Created on 13.09.2005
 *
 * To change the template for this generated file go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
 
/**
 * @author marfab001
 * 
 * To change the template for this generated type comment go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
public class TestProcess {
 
    private Runtime runtime;
    Process process = null;
    ByteArrayOutputStream error;
 
    public TestProcess() {
       runtime = Runtime.getRuntime();
       error = new ByteArrayOutputStream(1024);
    }
 
    public int execute(String[] args,
                       final OutputStream outStream,
                       final InputStream inpStream,
                       final OutputStream errStream,
                       final long timeout) throws IOException {
    	process = runtime.exec(args, null, null);
 
    	new Thread() {
            public void run() {
            	try {
					BufferedInputStream errStreamProcess =
					    new BufferedInputStream(process.getErrorStream());
					byte[] buffer = new byte[128];
					int read;
 
					while ((read = errStreamProcess.read(buffer, 0, buffer.length)) != -1) {
                        if (errStream != null) {
                        	errStream.write(buffer, 0, read);
                        }
					}
					errStreamProcess.close();
				} catch (IOException ioe) {
					// TODO Auto-generated catch block
					ioe.printStackTrace();
				}
            }
    	}.start();
 
 
        new Thread() {
            public void run() {
                try {
                    BufferedInputStream outStreamProcess =
                        new BufferedInputStream(process.getInputStream());
                    byte[] buffer = new byte[128];
                    int read;
 
                    while ((read = outStreamProcess.read(buffer, 0, buffer.length)) != -1) {
                        if (outStream != null) {
                        	outStream.write(buffer, 0, read);
                        }
                    }
                    outStreamProcess.close();
                } catch (IOException ioe) {
                    // TODO Auto-generated catch block
                    ioe.printStackTrace();
                }
            }
        }.start();
 
        if (inpStream != null) {
        new Thread() {
        	public void run() {
        		try {
					BufferedOutputStream inpStreamProcess = 
					    new BufferedOutputStream(process.getOutputStream());
					byte[] buffer = new byte[256];
					int read;
 
					while ((read = inpStream.read(buffer, 0, buffer.length)) != -1) {
					    inpStreamProcess.write(buffer, 0, read);
					}
				} catch (IOException ioe) {
					// TODO Auto-generated catch block
					ioe.printStackTrace();
				}
            }
        }.start();
        }
 
        int status = -1;
        if (timeout > 0L) {
          Thread waitFor = new Thread() {
            public void run() {
                try {
                	process.waitFor();
                }
                catch(InterruptedException ie) {
                	ie.printStackTrace();
                }
            }
        };
        waitFor.start();
 
        try {
			waitFor.join(timeout);
            try {
            	status = process.exitValue();
            }
            catch (IllegalThreadStateException itse) {
             process.destroy();   
            }
		} catch (InterruptedException ie) {
			ie.printStackTrace();
		}
        }
        else if (timeout == 0L) {
            try {
				status = process.waitFor();
			} catch (InterruptedException ie) {
				// TODO Auto-generated catch block
				ie.printStackTrace();
			}
    }
 
        return status;
    }
 
	public static void main(String[] args) {
       TestProcess process = new TestProcess(); 
       try {
        ByteArrayOutputStream out = new ByteArrayOutputStream(5 * 1024);
        String[] cmd = {"mspaint"};
		process.execute(cmd, out, null, null, 5000);
        String result = out.toString();
 
        String resultLine[] = result.split(System.getProperty("line.separator"));
        HashMap envMap = new HashMap();
        for (int i = 0; i < resultLine.length; ++i) {
            String env =  resultLine[i];
            int pos = env.indexOf("=");
            if (pos != -1) {
                String key = env.substring(0, pos);
                String value = env.substring(pos+1);
                envMap.put(key, value);
            }
            else {
                System.out.println(resultLine[i]);
            }
        }
 
	} catch (IOException ioe) {
		// TODO Auto-generated catch block
		ioe.printStackTrace();
	}
	}
} |