Mon Oct 25 2021

Run Linux Command

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

We use cookies to improve your experience on our site and to show you personalised advertising. Please read our cookie policy and privacy policy.