Get Website Learn Coding Tech Story

Java Programming Examples

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