Java invoke the Linux shell script method _java

Source: Internet
Author: User

First, we need to increase the user's ability to execute the script, that is,

Copy Code code as follows:

String cmdstring = "chmod a+x test.sh";
Process proc = Runtime.getruntime (). exec (cmdstring);
Proc.waitfor (); Block until the above command is done
cmdstring = "Bash test.sh"; This can be ksh, too.
proc = Runtime.getruntime (). exec (cmdstring);
Note the following actions
String Ls_1;
BufferedReader BufferedReader = new BufferedReader (New InputStreamReader (Proc.getinputstream ());
while ((Ls_1=bufferedreader.readline ())!= null);
Bufferedreader.close ();
Proc.waitfor ();

Why do you have the above operation?

The reason: the output of the executable program may be more, and the running window of the output buffer is limited, will cause waitfor blocking. The solution is to use the Getinputstream,geterrorstream method provided by the Java-provided process class to have the Java Virtual machine intercept the standard output of the invoked program, the error output, and read the contents of the output buffer before the waitfor () command.

I hope you can enjoy this article, please leave a message for me.

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.