//Lancement d'une application par une classe Java /* LanceAppli.java Lancement d'une application unix ou windows récupérer l'environnement (Runtime) création du processus externe (Process) récupération de sa sortie standard (getInputStream) */ package app; import java.io.IOException; import java.io.File; import java.io.InputStreamReader; import java.io.BufferedReader; public class LanceAppli { /** Creates a new instance of LanceAppli */ public LanceAppli() { } public static void main(String[] args) { String nomSex=System.getProperty("os.name").toLowerCase(); String chU="ls -ald *"; String chW="C:\\Windows\\explorer.exe"; // String chW="C:\\Windows\\command.com /C set"; String cmd=""; // Que va-t-on exécuter ? if(nomSex.indexOf("windows")>-1) cmd=chW; else if(nomSex.indexOf("unix")>-1) cmd=chU; else System.exit(1); BufferedReader clavier = new BufferedReader( new InputStreamReader(System.in)); // Objet environnement d'exécution Runtime r = Runtime.getRuntime(); try { Process p = r.exec(cmd); InputStreamReader flotC = new InputStreamReader(p.getInputStream()); BufferedReader flot=new BufferedReader(flotC); String ch; int i=0; ch=flot.readLine(); while(ch != null && ch.length()>=0) { i++; System.out.println(" "+i+": "+ch); if(i%10==0) ch=clavier.readLine(); ch=flot.readLine(); } System.out.println("fin "+cmd+", code="+p.exitValue()); } catch(IOException e){ System.out.println(""+e); } } } //fin LanceAppli.java