Pablo Trujillo wrote:
>
> I need to execute linux commands from code java. Help
>
if you want to execute shell commands, you can use the Runtime class (in
java.lang.*), with the exec(String command), it returns a new process
executing the system command specified, with the methods
getInputStream() and getOutputStream(), you can get the return of the
program: here is a little bit of code executing 'ls':
String temp = "";
BufferedReader input;
try {
Process p = Runtime.getRuntime().exec("/bin/ls");
input = new BufferedReader(new
InputStreamReader(p.getInputStream()));
while ((temp = input.readLine()) != null) {
System.out.println(temp);
}
} catch (IOException exception) { // you have to catch the IOException
exception.printStackTrace();
}
If your application sends back an exit code, you can wait for it using
waitFor(), this code starts an xterm and waits for it to exit, then goes
on:
try {
Process p = Runtime.getRuntime().exec("xterm");
System.out.println("Exit code: "+ p.waitFor());
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) { // another exception to catch
e.printStackTrace();
}
I hope this helps a bit further,
Urs
----------------------------------------------------------------------
To UNSUBSCRIBE, email to [EMAIL PROTECTED]
with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]