Java invoke shell script

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.