Run Linux Command


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) 
		catch (IOException e) {
			System.out.println("Invalid Command!");

/* Output */
Output of the command:

Comments (0)

  • To add your comment please or

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

Got It!