You can use getRuntime method to run linux shell commands from java. Try this example to get memory statistics:
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;
}
}
int ch;
String MemStr = "";
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);
}
{
System.out.println(anIOException);
}
InputStreamReader is used for getting results from LinuxProcess. Now we may got results to the MemStr.
Comments
Post a Comment