Running system commands through Java
Try the below code (for Linux OS) & you’ll get the listing of files & folders from current directory
If you want to open & close CD rom infinitely,
Just replace ‘ls’ command with the cd ROM opening cmd
Also for CD ROM closing …. Enclose them in infinite loop … thats all……
(You have to maintain a boolean flag for open & close)
try {
Process p = Runtime.getRuntime().exec(“ls”); //Line to talk with H/W
BufferedReader stdInput = new BufferedReader(new
InputStreamReader(p.getInputStream()));
BufferedReader stdError = new BufferedReader(new
InputStreamReader(p.getErrorStream()));
// read the output from the command
System.out.println(“Here is the standard output of the command:\n”);
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
}
// read any errors from the attempted command
System.out.print(“Here is the standard error of the command (if any): “);
while ((s = stdError.readLine()) != null) {
System.out.println(s);
}
System.exit(0);
}
catch (IOException e) {}

Tags: Running system command through Java