Java calls external commands

xiaoxiao2021-03-06  40

Java calls external command public class execcommond {public execcommond () {}

/ ** * Perform a command * @Param execStr string command string * @Return String information when the command is incorrect. * / Public static string exec (String Excstr) {runtime runtime = runtime.getime (); get current runtime object string outinfo = ""; // Export information Try {string [] args = new string [] { "sh", "-c", execStr}; // executes commands under Linux // executes commands under Windows // String [] args = new string [] {"cmd", "-c", execStr}; Process proc = runtime.exec (args); // Start another process to execute the command inputStream in = proc.geterrorStream (); // Get error message output. BufferedReader Br = New BufferedReader (in); string line = ""; while ((line = br.readline ())! = Null) {outinfo = Outinfo line "/ n"; system.out. Println (OutInfo); // checks if the command failed.

Try {if (Proc.WaitFor ()! = 0) {system.err.println ("exit value =" proc.exitvalue ());}}}}}} catch (interruptedException e) {system.err.print (e); E.PrintStackTrace ();}} catch (ooException e) {flag = false; system.out.println ("Exec Error:" E.GetMessage ()); E.PrintStackTrace ();} finally {return Outinfo;} }

转载请注明原文地址:https://www.9cbs.com/read-79325.html

New Post(0)