This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
public class cmdExecute2 { | |
public static void main(String[] args) { | |
String osName = System.getProperty("os.name"); | |
String rawVersion = null; | |
StringBuffer outputReport = new StringBuffer(); | |
String cmdToExecute = "awk '/Service.*DisplayGroupName.*Name.*ProductID/' /usr/local/services.conf"; | |
if (osName != null && osName.indexOf("Windows") == -1 | |
&& osName.indexOf("SunOS") == -1) { | |
Runtime rt = Runtime.getRuntime(); | |
BufferedReader is = null; | |
try { | |
// execute the RPM process | |
Process proc = rt.exec(new String[]{"sh","-c",cmdToExecute}); | |
// read output of the rpm process | |
is = new BufferedReader(new InputStreamReader(proc.getInputStream())); | |
String tempStr = ""; | |
while ((tempStr = is.readLine()) != null ) { | |
outputReport.append(tempStr.replaceAll(">", "/>\n")); | |
tempStr = ""; | |
} | |
int inBuffer ; | |
while ((inBuffer = is.read()) != -1) { | |
outputReport.append((char) inBuffer); | |
} | |
// rawVersion = is.readLine(); | |
// response.append(rawVersion); | |
is.close(); | |
proc.destroy(); | |
} catch (IOException ioe) { | |
System.out.println("Exception executing command " + cmdToExecute + "\n" + ioe); | |
} finally { | |
try { | |
is.close(); | |
} catch (final IOException ioe) { | |
System.out.println("Cannot close BufferedStream" + ioe); | |
} | |
} | |
} | |
System.out.println(outputReport.toString()); | |
} // end of main | |
} // end of class |