There are several places in the recent new project where you need to invoke the shell script, which records a simple shell script invocation method. The code is as follows:
private void Callsh () {
InputStreamReader STDISR = null;
InputStreamReader ERRISR = null;
Process process = NULL;
Script and path to call
String command = "/home/mw/weblogic/test.sh";
try {
Process = Runtime.getruntime (). exec (command);
BufferedReader STDBR = new BufferedReader (New InputStreamReader (Process.getinputstream ()));
BufferedReader ERRBR = new BufferedReader (New InputStreamReader (Process.geterrorstream ()));
String line = "";
while (line = Stdbr.readline ()) = null) {
System.out.println ("STD line:" + line);
}
while (line = Errbr.readline ()) = null) {
System.out.println ("ERR line:" +line);
}
} catch (Exception e) {
throw new Businessexception ("execute script failed = =" +e);
}finally{
if (STDBR! = null) {
Stdbr.close ();
}
if (ERRBR! = null) {
Errbr.close ();
}
if (process! = null) {
Process.destroy ();
}
}
}
This code only applies to General shell script calls, if the shell script content is more, syntax is more complex, because there is no good fault tolerance mechanism, the use of this method may cause problems. Read an article here, can learn from:
Http://blog.csdn.net/lance_wyvern/article/details/50456903#comments
Java invoke shell script