I have to do this Create and run a process (Process) of a program with a graphical interface, such as Firefox for both Windows and GNU/Linux. Use the Runtime class. According to what I have seen, it was worth putting start chrome in the command console, so I have made this little program but it gives an error.
String comando;
String s = null;
comando = "chrome.exe";
Process p = Runtime.getRuntime().exec(comando);
BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
System.out.println("Ésta es la salida standard del comando:\n");
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
}
// Leemos los errores si los hubiera
System.out.println("Ésta es la salida standard de error del comando (si la hay):\n");
while ((s = stdError.readLine()) != null) {
System.out.println(s);
}
System.exit(0);
}
}
You must indicate to your variable
comando
the path where the process is located, in this casechrome.exe
. For example:C:\Program Files (x86)\Google\Chrome\Application\chrome.exe
.Also, you should wrap your code in a
try/catch
to encapsulate errors that might occur.Your example would look like this: