Java calls execute cmd command

Source: Internet
Author: User

without permission, no reprint!!!

 Packagepractice;ImportJava.io.BufferedReader;ImportJava.io.File;Importjava.io.IOException;ImportJava.io.InputStream;ImportJava.io.InputStreamReader; Public classcmdadb { Public voidexecutecmdconsole (String cmd) {//This method prints the log to the console!!!!!!!!!!!! //This method runs successfully!!! System.out.println ("Enter in cmd" +cmd);        Process p; Try{p=runtime.getruntime (). exec (CMD); System.out.println ("::::::::::::::::::"::::::::::::::::::::::>>>>>> "start in the console print log"); //p.waitfor ();BufferedReader breader=NewBufferedReader (NewInputStreamReader (P.getinputstream (), "GBK")); String Line=NULL;  while((Line=breader.readline ())! =NULL) System.out.println (line); } Catch(IOException e) {e.printstacktrace (); }     }             PublicString Executecmdfile (string[] cmmands, String logtofile, String dirtodocmd)throwsIOException {//This method outputs the log to the specified folder!!!!!!!!!!!! //This method runs successfully!!! //if string Cmmand string cmmand = "adb logcat-v time > D:/adb.log"; //string[] cmmands so String commands[] = {"adb", "Logcat", "-V", "Time"}; //String Logtofile Save the journal to Logtofile//String dirtodocmd execute cmd command in Dirtodocmd//because the log is output to the file, you can no longer send the journal to the console                Try{Processbuilder Builder=NewProcessbuilder (cmmands); if(Dirtodocmd! =NULL) Builder.directory (NewFile (dirtodocmd)); Builder.redirecterrorstream (true); Builder.redirectoutput (NewFile (logtofile)); Process Process=Builder.start ();            Process.waitfor (); //get the result after the command executesInputStream is =Process.getinputstream (); BufferedReader Buffer=NewBufferedReader (NewInputStreamReader (IS, "GBK")); String Line=NULL; StringBuffer Sbbuffer=NewStringBuffer ();  while(line = Buffer.readline ())! =NULL) {sbbuffer.append (line);            } is.close (); returnsbbuffer.tostring (); } Catch(Exception e) {e.printstacktrace (); }        return NULL; }

Public Static voidMain (string[] args)throwsIOException {//String cmd= "d:/android/android-sdk-windows/platform-tools/adb logcat-v time"; //String cmd2= "adb devices"; //String cmd3= "adb logcat-v time"; //String cmd4= "adb logcat-v time > D:/adb.log";CMDADB ADBC =Newcmdadb (); Adbc.executecmdconsole ("ADB logcat-v Time"); String commands[]= {"adb", "Logcat", "-V", "time"}; Adbc.executecmdfile (Commands,"D:/adb.logs", "c:/users/wb-cjz286752"); //System.out.println (Result), because the log is output to the file, you can no longer log out to the console }}

Java calls execute cmd command

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.