Tuesday, November 01, 2005

run a command-line OR execute a system command in java

import java.io.*;

class Cli{
public static void main(String Argv[]) {
try {
String ls_str;

Process ls_proc = Runtime.getRuntime().exec("ls -l");

// get its output (your input) stream

DataInputStream ls_in = new DataInputStream(
ls_proc.getInputStream());

try {
while ((ls_str = ls_in.readLine()) != null) {
System.out.println(ls_str);
}
} catch (IOException e) {
System.exit(0);
}
} catch (IOException e1) {
System.err.println(e1);
System.exit(1);
}

System.exit(0);
}
}

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home