Skip to main content

Linux Commands in Java

You can use getRuntime method to run linux shell commands from java. Try this example to get memory statistics:

int ch;
String MemStr = "";
String[] LinuxCommand = {"/bin/sh","-c","cat /proc/meminfo"};
Process LinuxProcess = Runtime.getRuntime().exec(LinuxCommand);
InputStreamReader CSR = new InputStreamReader(LinuxProcess.getInputStream());
while ((ch = != -1)
MemStr = MemStr+(char)ch;

catch (IOException anIOException)

InputStreamReader is used for getting results from LinuxProcess. Now we may got results to the MemStr.


Popular posts from this blog