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

Find and replace with sed command in Linux

Find and replace feature is always handy. It can turn into a torture when it comes to change or delete a simple constant string in a text file. There is a handy tool in linux for doing these kind of tihngs. Actually sed is not a text editor but it is used outside of the text file to make changes.