Linux Commands in Java

04/07/2009

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

int ch;
String MemStr = “”;

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

catch (IOException anIOException)
{
System.out.println(anIOException);
}

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

No related posts.

Tags: ,

Leave a Reply

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

*