For example:
/** Run the Linux Command and return the execution result. */Public static String exec (String [] args) {String result = ""; ProcessBuilder processBuilder = new ProcessBuilder (args); Process process = null; InputStream errIs = null; inputStream inIs = null; try {ByteArrayOutputStream baos = new ByteArrayOutputStream (); int read =-1; process = processBuilder. start (); errIs = process. getErrorStream (); while (read = errIs. read ())! =-1) {baos. write (read);} baos. write ('n'); inIs = process. getInputStream (); while (read = inIs. read ())! =-1) {baos. write (read);} byte [] data = baos. toByteArray (); result = new String (data);} catch (IOException e) {e. printStackTrace ();} catch (Exception e) {e. printStackTrace ();} finally {try {if (errIs! = Null) {errIs. close ();} if (inIs! = Null) {inIs. close () ;}catch (IOException e) {e. printStackTrace () ;}if (process! = Null) {process. destroy () ;}} return result ;}
You can call the above function as needed:
String [] args = {"chmod", "777", "(file path)"}; exec (args );