1 2 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 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
|
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.StringBufferInputStream;
import java.io.StringReader;
/*
* 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;
private Process process = null;
private InputStream input = null;
private OutputStream output = null;
private OutputStream error = null;
private long timeout = 0;
public TestProcess() {
runtime = Runtime.getRuntime();
}
public int execute(String[] args) throws IOException {
int status = -1;
/* Creation du sous-processus */
process = runtime.exec(args, null, null);
/* Consomme la sortie d'erreur */
createProdConsThread(process.getErrorStream(), error, 1024).start();
/* Consomme la sortie standard */
createProdConsThread(process.getInputStream(), output, 1024).start();
if (input != null) {
/* Fournit les données à l'entrée standard */
createProdConsThread(input, process.getOutputStream(), 1024).start();
}
/* */
if (timeout > 0L) {
Thread subProcess = createSubProcess(process);
subProcess.start();
try {
subProcess.join(timeout);
try {
status = process.exitValue();
}
catch (IllegalThreadStateException itse) {
process.destroy();
status = process.exitValue();
}
}
catch (InterruptedException ie) {
ie.printStackTrace();
}
}
/* On attend que le sous-processus se termine */
else if (timeout == 0L) {
try {
status = process.waitFor();
}
catch (InterruptedException ie) {
// TODO Auto-generated catch block
ie.printStackTrace();
}
}
return status;
}
/**
* @return Returns the error.
*/
public OutputStream getError() {
return error;
}
/**
* @param error The error to set.
*/
public void setError(OutputStream errStream) {
this.error = errStream;
}
/**
* @return Returns the input.
*/
public InputStream getInput() {
return input;
}
/**
* @param input The input to set.
*/
public void setInput(InputStream inpStream) {
this.input = inpStream;
}
/**
* @return Returns the output.
*/
public OutputStream getOutput() {
return output;
}
/**
* @param output The output to set.
*/
public void setOutput(OutputStream outStream) {
this.output = outStream;
}
/**
* @return Returns the timeout.
*/
public long getTimeout() {
return timeout;
}
/**
* @param timeout The timeout to set.
*/
public void setTimeout(long timeout) {
this.timeout = timeout;
}
private Thread createProdConsThread(final InputStream input,
final OutputStream output,
final int bufferSize) {
Thread thread = new Thread() {
public void run() {
try {
BufferedInputStream outStreamProcess =
new BufferedInputStream(input);
byte[] buffer = new byte[bufferSize];
int read;
while ((read = outStreamProcess.read(buffer, 0, buffer.length)) != -1) {
if (output != null) {
output.write(buffer, 0, read);
}
}
outStreamProcess.close();
}
catch (IOException ioe) {
// TODO Auto-generated catch block
ioe.printStackTrace();
}
}
};
return thread;
}
private Thread createSubProcess(final Process process) {
return new Thread() {
public void run() {
try {
process.waitFor();
}
catch(InterruptedException ie) {
ie.printStackTrace();
}
}
};
}
public static void main(String[] args) throws Exception{
String[] cmd = {"cmd", "/c",
"dir", "C:\\Programme\\Mozilla Firefox",
">", "c:\\Programme\\Mozilla Firefox\\dirout.txt"};
TestProcess process = new TestProcess();
/*
process.setInput(new FileInputStream("source.txt"));
process.setOutput(new FileOutputStream("dest.txt"));
process.setError(new FileOutputStream("error.log"));
*/
process.setTimeout(0L);
int status = -1;
try {
status = process.execute(cmd) ;
System.out.println("La commande retourne : " + status);
} catch (IOException ioe) {
// TODO Auto-generated catch block
ioe.printStackTrace();
}
System.exit(status);
}
} |
Partager