Java: Running Linux Commands from your Class


import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Terminal {

public Terminal() {
}

public void doCommand(String termCommand) {
try {

Runtime rt = Runtime.getRuntime();

Process pr = rt.exec(termCommand);

BufferedReader input =
new BufferedReader(new InputStreamReader(pr.getInputStream()));
String line = null;

while ((line = input.readLine()) != null) {
System.out.println(line);
}

} catch (Exception e) {

System.out.println(e.toString());
e.printStackTrace();

}
}
}

Gulshan Yashveer Beejan

Gulshan holds a BSc(Hons) Computer Science with Distributed systems from the City University of London. He is currently working at DSOGroup(Mauritius) as a Linux Administrator on SaaS Solutions. More about Gulshan

Leave a Reply

Your email address will not be published. Required fields are marked *