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.