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 = "";
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.

Comments

Popular posts from this blog