Java Programming

Run Linux Command

Java programming example for run Linux command

10/25/2021
0 views
trun-linux-command.javaJava
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
Java TutorialRun Linux CommandRun Command

Related Examples