Re: Runtime.getRuntime().exec(command) ne donne pas de résultat
Bonjour,
Voici un exemple de code qui lance un exécutable quelconque. Ca devrait te donner le moyen de résoudre ton problème
package programmez.fred.processesdemo;
import java.util.*;
import java.io.*;
class StdHandler extends Thread {
InputStream is;
String type;
StdHandler(InputStream is, String type) {
this.is = is;
this.type = type;
}
public void run() {
try {
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String ligne=null;
while ( (ligne = br.readLine()) != null)
System.out.println(type + "> " + ligne);
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
public class Demo4 {
public static void main(String[] args) throws Throwable {
if (args.length <= 0) {
System.err.println("Donnez une commande a lancer");
System.exit(-1);
}
System.out.printf("\nSortie de la commande %s: \n",
Arrays.toString(args));
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec(args);
StdHandler stderr = new
StdHandler(process.getErrorStream(), "stderr");
StdHandler stdout = new
StdHandler(process.getInputStream(), "stdout");
stderr.start();
stdout.start();
int status = process.waitFor();
System.out.println("Valeur de retour du sous proc: " + status);
}
}
Bonjour,
Voici un exemple de code qui lance un exécutable quelconque. Ca devrait te donner le moyen de résoudre ton problème
package programmez.fred.processesdemo; import java.util.*; import java.io.*; class StdHandler extends Thread { InputStream is; String type; StdHandler(InputStream is, String type) { this.is = is; this.type = type; } public void run() { try { InputStreamReader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr); String ligne=null; while ( (ligne = br.readLine()) != null) System.out.println(type + "> " + ligne); } catch (IOException ioe) { ioe.printStackTrace(); } } } public class Demo4 { public static void main(String[] args) throws Throwable { if (args.length <= 0) { System.err.println("Donnez une commande a lancer"); System.exit(-1); } System.out.printf("\nSortie de la commande %s: \n", Arrays.toString(args)); Runtime runtime = Runtime.getRuntime(); Process process = runtime.exec(args); StdHandler stderr = new StdHandler(process.getErrorStream(), "stderr"); StdHandler stdout = new StdHandler(process.getInputStream(), "stdout"); stderr.start(); stdout.start(); int status = process.waitFor(); System.out.println("Valeur de retour du sous proc: " + status); } }