Mon Oct 25 2021
Run Linux Command
Java Programming2817 views
File Name: trun-linux-command.java
import java.io.*;
public class linux_command {
public static void main(String args[]) {
String s = null;
try {
/* Run the Linux command "ls" using the Runtime exec method: */
Process p = Runtime.getRuntime().exec("ls");
BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
/* Read the output from the command */
System.out.println("Output of the command:");
while ((s = stdInput.readLine()) != null)
System.out.println(s);
}
catch (IOException e) {
System.out.println("Invalid Command!");
}
}
}
/* Output */
Output of the command:
armstrong.java
arraysum.java
weekdayname.java
Author:Geekboots