afsac
asked on
System Commands From JAVA
I would like to know how to perform system commands and use the results with JAVA.
For example in Perl, I can use
system("showrev -p >/tmp/patches.dat");
Then open (via perl) /tmp/patches.dat and use the results in my code.
Can the same thing be done with Java?
Thanx
Don
For example in Perl, I can use
system("showrev -p >/tmp/patches.dat");
Then open (via perl) /tmp/patches.dat and use the results in my code.
Can the same thing be done with Java?
Thanx
Don
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Yes you can do it using the runtime.exec(String ) method . This executes the string as a commandline, in the environment. I shall try to provide a concrete example soon.
ASKER
Thank you for your quick response!
Thanks, afsac! Just to be complete: the exec() methods all return an instance of a java.lang.Process object that give you more control on the sub-process. Example:
Runtime runtime = Runtime.getRuntime();
try {
Process process = runtime.exec("showrev -p >/tmp/patches.dat");
process.waitFor(); // waits for the subprocess to complete.
} catch (IOException e) {
// error message here...
} catch (InterruptedException ie) {
// error message here...
}
Take a look at the doc. for more info.
Runtime runtime = Runtime.getRuntime();
try {
Process process = runtime.exec("showrev -p >/tmp/patches.dat");
process.waitFor(); // waits for the subprocess to complete.
} catch (IOException e) {
// error message here...
} catch (InterruptedException ie) {
// error message here...
}
Take a look at the doc. for more info.